我要自动校正。但是,当用户键入"@“时,我希望autoCorrect关闭,直到另有结果。
我试着把道具设为假[真]。但是,组件不会更改设置(当TextInput中有文本时)。
我怎么才能避开这一切?
(仅限iOS)
发布于 2019-05-07 18:51:02
演示


代码
checkTest函数
有关最重要的备注,请参见代码注释。
checkText(text){
//create a new regular expression
const regex = new RegExp("@");
//check if the string contains an @
const res = text.match(regex);
// if res is not null, we have a match!
if (res != null){
if (this.state.autoCorrect){
// disable auto correction if it's still enabled
this.input.blur();
// hacky part, we need to dismiss the keyboard first, then we can show it again.
this.setState({autoCorrect: false}, () => {
setTimeout(() => this.input.focus(), 60);
});
}
}else{
if (!this.state.autoCorrect){
this.input.blur();
// enable auto correction if no @ is detected anymore
this.setState({autoCorrect: true}, () => {
setTimeout(() => this.input.focus(), 60);
});
}
}
//update text in state
this.setState({ username: text});
}呈现函数
<View style={styles.container}>
<TextInput
value={this.state.username}
onChangeText={text => this.checkText(text)}
autoCorrect={this.state.autoCorrect}
/>
</View>完整的工作实例
https://snack.expo.io/Skta6BJ34
讨论
似乎,您需要“重新加载”键盘来影响autoCorrect属性的重新加载。我认为这仍然是一个bug,希望在未来的版本中得到解决。(参见此github问题)。
同时,您可以使用这个小的解决方法,或者对时间/regex等做一些微调。
编辑:
我找到了一个广泛的答案-- 这里,这个答案同样解决了这个问题。
https://stackoverflow.com/questions/55987149
复制相似问题