从父组件

时间:2018-03-21 10:39:50

标签: react-native input native-base

我有一个组件,它将子组件呈现在一个数组中,子组件将具有Native Base的Input组件。我想验证输入组件是否为空或空。但是如果子数组有多个子节点,则下面的代码不起作用。

class Parent extends Component {
    validateForms = () => {
      this.child.onSubmitValidate();
    }

    render() {     
       const childArray = stopsArray.map((item, key) => {
          return (
            <View key={key}>
              <Child
                ref={instance => { this.child = instance; }}
                itemIndex={key}
              />
            </View>
          );
        });

        <View style={styles.container}>
          <View style={{ flex: 1 }}>
            {
              childArray
            }
          </View>

          <Button
             onPressAction={() => this.validateForms()}
             buttonText={'PAYMENT'}
          />
        </View>
    }
}

儿童

import { Input } from 'native-base';

class Child extends Component {
    onSubmitValidate = () => {
      let error = false;

      ['buildingHouseNo', 'contactName', 'contactPhone']
        .forEach((name, key) => {
          const value = this.state[name];

          const nameValid = `${name}Valid`;

          if (!value || value.trim() === '' || value === null) {
            error = true;

            this.setState({
              [nameValid]: false
            });
          }
        });

      if (!error) {
        this.props.togglePayment();
      }
    }

    handleInput = (title, itemIndex, value) => {
      if (title === 'BuildingHouseNo') {
        this.setState({ buildingHouseNo: value });
      } else if (title === 'RecipientName') {
        this.setState({ contactName: value });
      } else if (title === 'RecipientPhone') {
        this.setState({ contactPhone: value });
      }

      this.validate(title, value);

      this.props.getRecipientInfo({
        index: itemIndex,
        title,
        value
      });
    }

    validate = (title, value) => {
      if (title === 'BuildingHouseNo') {
        if (!value || value.trim() === '' || value === null) {
          this.setState({ buildingHouseNoValid: false });
        } else {
          this.setState({ buildingHouseNoValid: true });
        }
      } else if (title === 'RecipientName') {
        if (!value || value.trim() === '' || value === null) {
          this.setState({ contactNameValid: false });
        } else {
          this.setState({ contactNameValid: true });
        }
      } else if (title === 'RecipientPhone') {
        if (!value || value.trim() === '' || value === null) {
          this.setState({ contactPhoneValid: false });
        } else {
          this.setState({ contactPhoneValid: true });
        }
      }
    }

    render() {
      const itemIndex = this.props.index;

      <View style={styles.container}>
        <View style={styles.borderInput}>
          <Input
            ref={(ref) => { this.buildingHouseNo = ref; }}
            key={itemIndex}
            clearButtonMode='while-editing'
            autoCapitalize='none'
            autoCorrect={false}
            style={styles.inputSearchBorder}
            placeholder='Building Name / House Number'
            onSubmitEditing={() => this.addr2._root.focus()}
            returnKeyType={'next'}
            onChangeText={this.handleInput.bind(this, 'BuildingHouseNo', itemIndex)}
          />
        </View>

        <View style={styles.borderInput}>
          <Input
            key={itemIndex}
            ref={(ref) => { this.contactName = ref; }}
            clearButtonMode='while-editing'
            autoCapitalize='none'
            autoCorrect={false}
            style={styles.inputSearch}
            placeholder='Name'
            value={contactName}
            onSubmitEditing={() => this.contactNo._root.focus()}
            returnKeyType={'next'}
            onChangeText={this.handleInput.bind(this, 'RecipientName', itemIndex)}
          />
        </View>

        <View style={styles.borderInput}>
          <Input
            key={itemIndex}
            ref={(ref) => { this.contactNo = ref; }}
            clearButtonMode='while-editing'
            autoCapitalize='none'
            autoCorrect={false}
            style={styles.inputSearch}
            placeholder='Contact Number'
            value={contactPhone}
            onSubmitEditing={() => this.notesToBunny._root.focus()}
            returnKeyType={'next'}
            onChangeText={this.handleInput.bind(this, 'RecipientPhone', itemIndex)}
          />
        </View>
      </View>
    }
}

1 个答案:

答案 0 :(得分:2)

每次创建新的ref组件时,您都会覆盖Child。您可以创建一个ref值数组并循环遍历它们以验证每个值。

constructor() {
  super();

  this.child = [];
{

const childArray = stopsArray.map((item, key) => {
  return (
    <View key={key}>
      <Child
        ref={instance => { this.child[key] = instance; }}
        itemIndex={key}
      />
    </View>
  );
});

//...

validateForms = () => {
  this.child.forEach(child => {
    child.onSubmitValidate();
  })
}