简体   繁体   English

提交后如何让 React Native TextInput 保持焦点?

[英]How do I get a React Native TextInput to maintain focus after submit?

I could explain what I am trying to do, but this ReactJS example is a walkthrough of exactly what I want.我可以解释我想要做什么,但是这个 ReactJS 示例正是我想要的演练。 The problem is I can't figure out what the equivelant would be for react native.问题是我无法弄清楚 react native 的等效项是什么。

Basically, when I press return in the TextInput, I want the text cleared and focus maintained.基本上,当我在 TextInput 中按回车键时,我希望清除文本并保持焦点。

Any thoughts?有什么想法吗?

I've submitted a PR with a blurOnSubmit property.我已经提交了一个带有blurOnSubmit属性的 PR。

Set it to false and the TextInput never blurs, onSubmitEditing still fires though.将其设置为false并且TextInput永远不会模糊,但onSubmitEditing仍然会触发。

Hopefully it gets merged.希望它合并。 :) :)

https://github.com/facebook/react-native/pull/2149 https://github.com/facebook/react-native/pull/2149

I came out with following (working) solution:我提出了以下(工作)解决方案:

var NameInput = React.createClass({
  getInitialState() {
    return {
      textValue: ''
    }
  },

  clearAndRetainFocus: function(evt, elem) {
    this.setState({textValue: elem.text});
    setTimeout(function() {
      this.setState({textValue: this.getInitialState().textValue});
      this.refs.Name.focus();
    }.bind(this), 0);
  },

  render() {
    return(
      <TextInput
        ref='Name'
        value={this.state.textValue}
        onEndEditing={this.clearAndRetainFocus} />
    )
  }
});

So, basically when we end editing, we will set the textValue state to the value of the TextInput and right after that (in setTimeout ), we switch it back to default (empty) and retain focus on the element.所以,基本上当我们结束编辑时,我们将textValue状态设置为TextInput的值,然后(在setTimeout ),我们将其切换回默认值(空)并保持对元素的关注。

I don't know how to trigger blurOnSubmit but if you do and it works you should do that.我不知道如何触发 blurOnSubmit 但如果你这样做并且它有效你应该这样做。 Another thing I found that works with a functional react component in a chat application i am making is this:我发现在我正在制作的聊天应用程序中与功能性反应组件一起使用的另一件事是:

... import statments

const ChatInput = props => {
const textIn = React.useRef(null) //declare ref
useEffect(()=>textIn.current.focus()) //use effect to focus after it is updated

const textInputChanged = (text) =>{
    props.contentChanged(text);
}

const submitChat = () =>{
    const txt = props.content.trim()
    txt.length >0 ?  props.sendChat(txt, props.username) : null;
}

const keyPressEvent = (e) =>{
   return e.key == 'Enter'? submitChat() : null;
}

return (
     
        <TextInput 
        style={styles.textInput}
        keyboardType={props.keyboardType}
        autoCapitalize={props.autoCapitalize}
        autoCorrect={props.autoCorrect}
        secureTextEntry={props.secureTextEntry}
        value={props.content}
        onChangeText={textInputChanged}  
        onKeyPress={keyPressEvent}
        autoFocus={true}  //i don't think you need this since we are using useEffect
        ref={textIn} //make it so this is the ref
    />
   )}
... export default react-redux connect stuff

if you have more inputs you can probably do some sort of ref choosing logic in the useEffect hook如果您有更多输入,您可能可以在 useEffect 钩子中执行某种参考选择逻辑

this is the article that helped me figure it out, it's almost the same thing: https://howtocreateapps.com/how-to-set-focus-on-an-input-element-in-react-using-hooks/这是帮助我弄清楚的文章,它几乎是一样的: https : //howtocreateapps.com/how-to-set-focus-on-an-input-element-in-react-using-hooks/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM