我有一个按钮,当按下它时,我希望它打开键盘并专注于文本输入组件。
对于一个最小的代码示例,我想做的是 -
<View>
<AddSomething
textChange={textInput => this.setState({ textInput })}
addNewItem={this.addItem.bind(this)}
textInput={this.state.textInput}
ref={not sure what goes here} //passing these as props to a text input
/>
<FloatingButton tapToAddHandler={this.onFloatingButtonPress.bind(this)} />
</View>
Run Code Online (Sandbox Code Playgroud)
然后是一些处理按钮按下的辅助函数(this.onFloatingButtonPress)
首先声明你的AddSomething如下:
const AddSomething = React.forwardRef((props, ref) => (
<TextInput
ref={ref}
//your other code
/>
));
Run Code Online (Sandbox Code Playgroud)
现在您可以使用ref并能够集中您的AddSomething组件,如下所示:
<View>
<AddSomething
textChange={textInput => this.setState({ textInput })}
addNewItem={this.addItem.bind(this)}
textInput={this.state.textInput}
ref={(ref) => { this.textInputField = ref }}
/>
<FloatingButton tapToAddHandler={this.onFloatingButtonPress.bind(this)} />
</View>
Run Code Online (Sandbox Code Playgroud)
这是你的onFloatingButtonPress方法:
onFloatingButtonPress() {
this.textInputField.focus();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8550 次 |
| 最近记录: |