按下下一步时如何关注下一个无状态输入

时间:2017-12-31 16:42:30

标签: react-native react-redux

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

我尝试在renderInput()内部的输入中使用onSubmitEditing并结合访问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;

1 个答案:

答案 0 :(得分:0)

TextInput 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>