react-native - 按下一步时如何聚焦下一个无状态输入

标签 react-native react-redux

我正在用 React Native 开发一个应用程序。在我的登录页面上,我有一个带有 Redux 的表单。在电子邮件输入字段上点击“下一步”后,我想集中输入密码。

我尝试在 renderInput() 内的 Input 上使用 onSubmitEditing 并结合访问 refs,但不幸的是没有成功(refs 不可用,因为元素是无状态的)。

我的代码:

class LoginForm extends React.Component<Props, State> {
    textInput: any;

    constructor(props) {
        super(props);
        this.state = {
            email: '',
            password: '',
            isLoading: false,
        }
    }

    renderInput({ input, disabled, label, type, meta: { touched, error, warning } }) {
        return (
            <Item error={error && touched}>
                <Icon active name={input.name === "email" ? "person" : "unlock"} />
                <Input
                    ref={c => (this.textInput = c)}
                    placeholder={input.name === "email" ? "Email" : "Password"}
                    secureTextEntry={input.name === "password"}
                    editable={!disabled}
                    // autoFocus={input.name === "email"}
                    returnKeyType={input.name === "email" ? "next" : "send"}
                    keyboardType={input.name === "email" ? "email-address" : "default"}
                    autoCapitalize="none"
                    blurOnSubmit={false}
                    {...input}
                />
            </Item>
        );
    }

    render() {
        const { isLoading } = this.state;
        const form = (
            <Form>
                <Field
                    name="email"
                    disabled={isLoading}
                    component={this.renderInput}
                    validate={[email, required]}
                    onChange={(event, value) => this.setState({email: value})}
                />
                <Field
                    name="password"
                    disabled={isLoading}
                    component={this.renderInput}
                    validate={[minLength8, maxLength15, required]}
                    onChange={(event, value) => this.setState({password: value})}
                />
                {isLoading && (
                    <ActivityIndicator style={styles.loading} size="large" animating={true} />
                )}
                <View style={{padding: 10}}>
                    <Button block} disabled={this.state.isLoading}>
                        <Text>Login</Text>
                    </Button>
                </View>
            </Form>
        );
        return <Login navigation={this.props.navigation} loginForm={form} />;
    }
}
const LoginContainer = reduxForm({
    form: "login",
})(LoginForm);
export default LoginContainer;

最佳答案

使用 文本输入 onSubmitEditing 你也应该使用 KeyboardAvoidingView 例如:

         <KeyboardAvoidingView>
                <TextInput
                    placeholder="Enter your Username"
                    returnKeyType="next"
                    onSubmitEditing={() => this.passwordInput.focus()}
                    keyboardType="email-address"
                    autoCapitalize="none"
                    autoCorrect={false}
                    style={styles.input}
                />
                <TextInput
                    placeholder="Enter your Password"
                    returnKeyType="go"
                    secureTextEntry
                    style={styles.input}
                    ref={(input) => this.passwordInput = input}
                />
       </KeyboardAvoidingView>

关于react-native - 按下一步时如何聚焦下一个无状态输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48044205/

相关文章:

javascript - 使用 Redux 时,将事件从演示组件传递到容器组件是否是反模式?

javascript - 从 Redux reducer 中的对象复制原型(prototype)函数

reactjs - 单击按钮关闭模式 react native

react-native - 如何在 React Native Navigation v3 中返回页面

https - React-native 0.40+ 获取自签名证书

reactjs - Redux-saga 与 Internet Explorer

javascript - 将 redux 存储传递给 react-dom/server renderToString() 方法

android - 如何自定义react native的按钮

ios - 框架未找到 EXLocation,生成存档时 OpenSSL react 原生 ios xcode

javascript - 表示 react-redux 连接组件的 UML 图