FlatList问题中带有TextInput的动态形式

问题描述 投票:0回答:1

实际行为:

我正在FlatList中加载动态表单,并且应该根据条件在TextInputs中添加几行。例如,我有一个复选框。当我选中复选框时,我必须添加一个下拉列表和两个TextInput,而当我取消选中该复选框时,必须删除这三个。我的问题是,当我选中复选框并在TextInputs中输入一些值,然后取消选中复选框时,最后一个TextInput中的值将分配给与这些条件无关的另一个TextInput。

预期的行为:

最后一个TextInput值在取消选中复选框时不应分配给与复选框条件无关的另一个TextInput。请提出建议。预先感谢。

其他资源:

这是我的TextInput代码:

 _onChangeText(e, item, index) {
    console.log("abc123 ", item);
    let count = "false";
    let note;
    if (this.state.inputsValues.length === 0 && item.blocks.question !== undefined) {
        if (item.blocks.question.prompt !== "Notes") {
            note = {
                note: e.nativeEvent.text.replace(/\n/g, ''),
                id: item.blocks.question.prompt
            };
        } else {
            note = {
                note: e.nativeEvent.text.replace(/\n/g, ''),
                id: item.blocks.block_pos
            };
        }
        this.setState({ inputsValues: [...this.state.inputsValues, note] }, () => {
            console.log("length === 0", this.state.inputsValues);
        });
    } else {
        this.state.inputsValues.map((res, idx) => {
            if (item.blocks.question !== undefined && res.id === item.blocks.question.prompt) {
                res.note = e.nativeEvent.text.replace(/\n/g, '');
                count = "true";
            }
        });
        if (count === "false") {
            if (item.blocks.question.prompt !== "Notes") {
                note = {
                    note: e.nativeEvent.text.replace(/\n/g, ''),
                    id: item.blocks.question.prompt
                };
            } else {
                note = {
                    note: e.nativeEvent.text.replace(/\n/g, ''),
                    id: item.blocks.block_pos
                };
            }
            this.setState({ inputsValues: [...this.state.inputsValues, note] }, () => {
                // console.log("gvrtgrtgt", this.state.inputsValues);
            });
        }
    }
}
<NBTextField
itemStyles={[styles.itemStyles, { height: width / 6 }]}
value={inputsValues[index] !== undefined && inputsValues[index].note}
onEndEditing={e => this._onChangeText(e, item)}
onChangeText={this.getInputValue}
onSubmitEditing={() => Keyboard.dismiss()}
inputStyles={[styles.inputStyles, { height: width / 6 }]}
placeholder="Enter here"
/>

环境:

本机:0.61.1

截图:

enter image description here

react-native react-native-flatlist textinput
1个回答
0
投票

下面的代码在mycase中对我有用

 _onChangeText(e, item, index) {
    if (item.blocks.block_type === 'question') {
      this.state.mainData[index].answer = e.nativeEvent.text;
      this.setState({
        mainData: this.state.mainData,
      });
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.