首页 > 解决方案 > 我如何处理多个复选框和显示和隐藏输入字段的基础并在本机反应中更新到服务器

问题描述

在我的代码中有 2 个下拉菜单、4 个复选框、4 个输入字段和一个按钮。基本上我必须在按钮单击时更新这些字段。

卡箱问题。我必须根据条件制作复选框“checked={true/false}”,并且我必须获取这些值并更新服务器:

1 [] Emal [] sms [] fax (all are unchecked )

假设我单击电子邮件,应检查电子邮件并应出现一个电子邮件输入字段。如果我再次取消选中该复选框,则输入字段应该被隐藏。

如果选中该值,则当我按下下面的按钮时,如果未选中,则 Y 值应转到服务器,否则为 N 值。

基本上有多个复选框,我必须将它们的值更新为 server 。下面是 UX 链接,请看一次。

https://xd.adobe.com/view/2b0336f6-6ff6-40ae-6653-f71e080dd0da-5d32/?fullscreen

在此处输入图像描述

import React, { Component } from 'react';
    import { ImageBackground, ScrollView, TouchableOpacity, View, Platform, Image } from 'react-native';
    import { Button, Text, Item, Input, Icon, Form, ListItem, CheckBox, Body, List } from 'native-base';
    import Header from '../../ui/header';
    import TextFieldTypeClear from '../../ui/textFieldTypeClear';
    import SelectField from '../../ui/selectField';
    import { PrimaryBtn } from '../../ui/buttons';
    import BG from '../../../images/bg.jpg';
    import styles from '../../simSwap/SimSwap.style';
    import { RegularText, SmallText } from '../../ui/text';
    import { ACCOUNT_OWNER,ADDRESS,CYCLE,EMAIL,PHONE,PERIODICITY,CURRENCY,LANGUAGE,EDIT,MAIL,FAX,POST,SMS,WHATSAPP } from '../../../images';
    import _ from 'lodash';

    const styless = {
      icon:{
        marginRight:5, marginTop:3
      },
      label:{
        fontSize:14, color:'grey'
      }
    }

    const Label = ({img, textLabel}) =>{
      return (
        <View style={{flexDirection:'row'}}>
          <Image style={styless.icon} source={img}/>
          <Text style={styless.label}>{textLabel}</Text>
        </View>
      );
    }

    class UpdateBillPreferences extends Component {
      constructor(props) {
        super(props);
        const {navigation,clmmasterData} =this.props;
        this.state = {
          title: 'Update Bill Preferences',
          mobile: navigation.state.params.customer.service.serviceNumber,
          icon: 'sim',
          email: '',
          smsNum: '',
          faxNum: '',
          isBillByEmail : navigation.state.params.customerInfo[0].billingPreferenceDetails.isBillByEmail,
          isBillBySms : navigation.state.params.customerInfo[0].billingPreferenceDetails.isBillByFax,
          isBillByFax : navigation.state.params.customerInfo[0].billingPreferenceDetails.isBillBySms,
          languageAndCurrecny:{
            preferredLanguage: navigation.state.params.customerInfo[0].billingPreferenceDetails.presentationLanguageCode,
          },
          currencyChangedValue:{
            preferredCurrency: navigation.state.params.customerInfo[0].billingPreferenceDetails.preferedCurrencyCode,

          }

        };
      }

      componentDidMount() {

      }

      OnButtonClick = async (preferredLanguage, preferredCurrency,email,smsNum,faxNum) => {
        const { OnButtonClick } = this.props;
        await OnButtonClick(preferredLanguage, preferredCurrency,email,smsNum,faxNum);
        this.setState({
          preferredCurrency:'',
          preferredLanguage:'',
          email :'',
          smsNum :'',
          faxNum :''

        })
      }
      languageChanged = (key, val) => {
        this.handleChange({ field: "preferredLanguage" }, val);
      };

      handleChange = (props, e) => {
        let tempObj = this.state.languageAndCurrecny;
        tempObj[props.field] = e;
        this.setState({ preferredLanguage: tempObj });
      };

      currencyChanged = (key, val) => {
        this.handleChange2({ field: "preferredCurrency" }, val);
      };

      handleChange2 = (props, e) => {
        let tempObj = this.state.currencyChangedValue;
        tempObj[props.field] = e;
        this.setState({ preferredCurrency: tempObj });
      };

      handleChange1 = () => {
        let isBillByEmail=this.state.isBillByEmail;
        console.log("-----------------------clicked-------");
        this.setState(previousState => {
          return { isBillByEmail: !previousState.checked };
        })
        console.log("----------isBillByEmail--------------------",isBillByEmail);

      }

      render() {
        let { title, mobile, icon,languageAndCurrecny,currencyChangedValue,isBillByEmail } = this.state;
        const { navigation,clmmasterData} = this.props;
        const {billingAddressDetails,billingPreferenceDetails} = navigation.state.params.customerInfo[0];
       const {masterData , language} = clmmasterData;
        let submitBtn = { label: 'Submit', OnSubmit: this.onSubmit };

        let currencyData=[];
        masterData.preferredCurrency.map(({ code: value, name: label }) => {
          currencyData.push({ value, label });
        });

        let languageData=[];
        masterData.language.map(({ code: value, name: label }) => {
          languageData.push({ value, label });
        });

        return (
          <ImageBackground source={BG} style={styles.imgBG}>
          <ScrollView>
            <View style={styles.container}>
              <View>
                <Header title={title} subtitle={mobile} icon={icon} navigation={navigation}/>
              </View>

                <View style={styles.contentContainer}>
                  <View style={{ padding: 20 }}>
                    <Form style={{ width: '100%' }}>
                      <SelectField
                        label="Presentation Language"
                        node="presentationLanguage"
                        options={languageData}
                        value={languageAndCurrecny.preferredLanguage}
                        onChange={this.languageChanged}
                        that={this}
                        setIcon={true}
                        img="LANGUAGE"
                      />

                      <SelectField
                        label="Preferred Currency"
                        options={currencyData}
                        value={currencyChangedValue.preferredCurrency}
                        node="preferredCurrency"
                        onChange={this.currencyChanged}
                        that={this}
                        setIcon={true}
                        img="CURRENCY"
                      />
                      <View style={{flexDirection:'column', marginBottom:15}}>
                        <View>
                          <Text style={{ color: 'grey', marginBottom: 15, marginTop:10, fontSize:14 }}>Preference</Text>
                        </View>
                        <View style={{flexDirection:'row', marginLeft:-10}}>
                          <View style={{flex:1}}>
                          <CheckBox color="#00678f" checked={billingPreferenceDetails.isBillByPost === "Y" ? true : false}/>
                          </View>
                          <View style={{flex:1}}>
                            <Text style={{fontSize:14}}>Post</Text>
                          </View>
                          <View style={{flex:1}}>
                            <CheckBox color="#00678f" checked={isBillByEmail==='Y'?true : false} onPress={() =>this.handleChange1()}/>
                          </View>
                          <View style={{flex:1}}>
                            <Text style={{fontSize:14}}>Email</Text>
                          </View>
                          <View style={{flex:1}}>
                            <CheckBox color="#00678f" checked={true} onPress={() =>this.handleChange()}/>
                          </View>
                          <View style={{flex:1}}>
                            <Text style={{fontSize:14}}>SMS</Text>
                          </View>
                          <View style={{flex:1}}>
                            <CheckBox color="#00678f" checked={true} onPress={() =>this.handleChange()}/>
                          </View>
                          <View style={{flex:1}}>
                            <Text style={{fontSize:14}}>FAX</Text>
                          </View>

                        </View>
                      </View>

                      <View style={{flexDirection:'column', marginBottom:15}}>
                        <View style={{marginTop:10, marginBottom:10, marginLeft:-3}}>
                          <Label img={ADDRESS} textLabel="Address"/>
                        </View>
                        <View>
                        <RegularText style={{ fontWeight: 'normal' }} text={`${billingAddressDetails.address1}, ${billingAddressDetails.address2}, ${billingAddressDetails.cityName}, ${billingAddressDetails.state}, ${billingAddressDetails.country}`} textColor="black" />

                        </View>
                      </View>

                      <View style={{marginBottom:15}}>
                      {billingPreferenceDetails.isBillByEmail === 'Y' &&
                        <View>
                          <Label img={EMAIL} textLabel="Email"/>
                          <Item style={{borderColor: '#00fff', borderBottomWidth:1, marginLeft:0}}>
                            <Input
                              value={this.state.email}
                              onChangeText={(text) => this.setState({email:text})}
                            />
                          </Item>
                        </View>}
                        {billingPreferenceDetails.isBillBySms === 'Y' &&
                        <View>
                          <Label img={EMAIL} textLabel="SMS"/>
                          <Item style={{borderColor: '#00fff', borderBottomWidth:1, marginLeft:0}}>
                            <Input
                              value={this.state.smsNum}
                              onChangeText={(text) => this.setState({smsNum:text})}
                            />
                          </Item>
                        </View>}
                        {billingPreferenceDetails.isBillByFax === 'Y' &&
                        <View>
                          <Label img={EMAIL} textLabel="FAX"/>
                          <Item style={{borderColor: '#00fff', borderBottomWidth:1, marginLeft:0}}>
                            <Input
                              value={this.state.faxNum}
                              onChangeText={(text) => this.setState({faxNum:text})}
                            />
                          </Item>
                        </View>}
                      </View>

                      <View style={{ marginTop: 50 }}>
                        <PrimaryBtn label={'submit'} disabled={false} onPress={()=> this.OnButtonClick(this.state.preferredLanguage,this.state.preferredCurrency,
                          this.state.email,this.state.smsNum,this.state.faxNum)}/>
                      </View>
                    </Form>
                  </View>
                </View>

            </View>
            </ScrollView>
          </ImageBackground>
        );
      }
    }

    export default UpdateBillPreferences;

// 我遇到问题的复选框部分。

Props Value 

isBillByEmail: "N"
isBillByFax: "Y"
isBillByPost: "Y"
isBillBySms: "N"

     <View style={{flexDirection:'column', marginBottom:15}}>
                        <View>
                          <Text style={{ color: 'grey', marginBottom: 15, marginTop:10, fontSize:14 }}>Preference</Text>
                        </View>
                        <View style={{flexDirection:'row', marginLeft:-10}}>
                          <View style={{flex:1}}>
                          <CheckBox color="#00678f" checked={billingPreferenceDetails.isBillByPost === "Y" ? true : false}/>
                          </View>
                          <View style={{flex:1}}>
                            <Text style={{fontSize:14}}>Post</Text>
                          </View>
                          <View style={{flex:1}}>
                            <CheckBox color="#00678f" checked={isBillByEmail==='Y'?true : false} onPress={() =>this.handleChange1()}/>
                          </View>
                          <View style={{flex:1}}>
                            <Text style={{fontSize:14}}>Email</Text>
                          </View>
                          <View style={{flex:1}}>
                            <CheckBox color="#00678f" checked={true} onPress={() =>this.handleChange()}/>
                          </View>
                          <View style={{flex:1}}>
                            <Text style={{fontSize:14}}>SMS</Text>
                          </View>
                          <View style={{flex:1}}>
                            <CheckBox color="#00678f" checked={true} onPress={() =>this.handleChange()}/>
                          </View>
                          <View style={{flex:1}}>
                            <Text style={{fontSize:14}}>FAX</Text>
                          </View>

                        </View>
                      </View>

                      <View style={{flexDirection:'column', marginBottom:15}}>
                        <View style={{marginTop:10, marginBottom:10, marginLeft:-3}}>
                          <Label img={ADDRESS} textLabel="Address"/>
                        </View>
                        <View>
                        <RegularText style={{ fontWeight: 'normal' }} text={`${billingAddressDetails.address1}, ${billingAddressDetails.address2}, ${billingAddressDetails.cityName}, ${billingAddressDetails.state}, ${billingAddressDetails.country}`} textColor="black" />

                        </View>
                      </View>

                      <View style={{marginBottom:15}}>
                      {billingPreferenceDetails.isBillByEmail === 'Y' &&
                        <View>
                          <Label img={EMAIL} textLabel="Email"/>
                          <Item style={{borderColor: '#00fff', borderBottomWidth:1, marginLeft:0}}>
                            <Input
                              value={this.state.email}
                              onChangeText={(text) => this.setState({email:text})}
                            />
                          </Item>
                        </View>}
                        {billingPreferenceDetails.isBillBySms === 'Y' &&
                        <View>
                          <Label img={EMAIL} textLabel="SMS"/>
                          <Item style={{borderColor: '#00fff', borderBottomWidth:1, marginLeft:0}}>
                            <Input
                              value={this.state.smsNum}
                              onChangeText={(text) => this.setState({smsNum:text})}
                            />
                          </Item>
                        </View>}
                        {billingPreferenceDetails.isBillByFax === 'Y' &&
                        <View>
                          <Label img={EMAIL} textLabel="FAX"/>
                          <Item style={{borderColor: '#00fff', borderBottomWidth:1, marginLeft:0}}>
                            <Input
                              value={this.state.faxNum}
                              onChangeText={(text) => this.setState({faxNum:text})}
                            />
                          </Item>
                        </View>}
                      </View>

                      <View style={{ marginTop: 50 }}>
                        <PrimaryBtn label={'submit'} disabled={false} onPress={()=> this.OnButtonClick(this.state.preferredLanguage,this.state.preferredCurrency,
                          this.state.email,this.state.smsNum,this.state.faxNum)}/>
                      </View>
                    </Form>
                  </View>
                </View>

请帮忙..谢谢

标签: javascriptreactjsreact-nativecheckbox

解决方案


推荐阅读