gpt4 book ai didi

javascript - 似乎无法聚焦下一个 TextInput(React-Native)

转载 作者:行者123 更新时间:2023-12-02 21:22:53 25 4
gpt4 key购买 nike

我对 React 有一些经验,但仍在尝试弄清楚 React-Native。我正在开发一个小项目,并且正在制作登录表单。我希望这样当您按键盘上的“下一步”时,它就会移至下一个输入字段。我制作了一个自定义 TextInput 组件,因为我将在几个不同的地方使用它。

import React, { Component } from 'react';
import { Animated, TextInput, View } from 'react-native';
import { Easing } from 'react-native-reanimated';
import { primaryColor, whiteColor } from '../../../../assets/theme';

class FloatingLabelInput extends Component {
constructor(props) {
super(props);
this.state = {
isFocused: false,
}
this.animatedText = new Animated.Value(this.props.value === '' ? 0 : 1);
this.animatedTextField = new Animated.Value(0);
}

handleFocus = () => {
this.setState({
isFocused: true
});
}

handleBlur = () => {
this.setState({
isFocused: false
});
}

componentDidUpdate = () => {
Animated.timing(this.animatedText, {
toValue: (this.state.isFocused || this.props.value !== '') ? 1 : 0,
duration: 200,
easing: Easing.linear
}).start();
Animated.timing(this.animatedTextField, {
toValue: this.state.isFocused ? 1 : 0,
duration: 200,
easing: Easing.linear
}).start();
}

render() {
const { label, ...props } = this.props;
const labelStyle = {
backgroundColor: whiteColor,
color: this.animatedText.interpolate({
inputRange: [0, 1],
outputRange: ['#aaa', '#000']
}),
fontSize: this.animatedText.interpolate({
inputRange: [0, 1],
outputRange: [20, 14]
}),
left: 5,
paddingHorizontal: 5,
position: 'absolute',
top: this.animatedText.interpolate({
inputRange: [0, 1],
outputRange: [26, 8]
}),
zIndex: 999
};

return (
<View style={{ paddingTop: 18 }}>
<Animated.Text style={labelStyle}>{label}</Animated.Text>
<Animated.View style={{
borderColor: this.animatedTextField.interpolate({
inputRange: [0, 1],
outputRange: ['#555', primaryColor]
}),
borderRadius: 4,
borderWidth: 1,
}}>
<TextInput
onBlur={this.handleBlur}
onFocus={this.handleFocus}
{...props}
style={{
color: '#000',
fontSize: 14,
height: 45,
paddingLeft: 10
}}
/>
</Animated.View>
</View>
);
}
}

export default FloatingLabelInput;

然后在登录页面,那么实现是这样的:

<View>
<FloatingLabelInput
blurOnSubmit={false}
editable={true}
keyboardType={'email-address'}
label="Email"
onChangeText={this.handleEmailChange}
onSubmitEditing={() => this.passwordInput && this.passwordInput.focus()}
ref={(input) => { this.emailInput = input; }}
returnKeyType="next"
value={this.state.email}
/>
<FloatingLabelInput
editable={true}
label="password"
onChangeText={this.handlePasswordChange}
onSubmitEditing={() => this.login()}
ref={(input) => { this.passwordInput = input; }}
value={this.state.password}
/>
</View>

我的理解是,通过引用下一个输入并拥有 .focus(),那么它应该可以工作,但是当我这样做时,它会抛出一个错误,指出 this.passwordInput.focus() 未定义

最佳答案

您的 ref 不是 TextInput,而是 FloatingLabelInput 并且没有 focus 方法。您正在尝试关注 FloatingLabelInput,它不是 TextInput,而是包含 TextInput 的组件。

您需要在此处使用forwardRef

示例来自 Facebook

const FancyButton = React.forwardRef((props, ref) => (
<button ref={ref} className="FancyButton">
{props.children}
</button>
));

// You can now get a ref directly to the DOM button:
const ref = React.createRef();
<FancyButton ref={ref}>Click me!</FancyButton>;

关于javascript - 似乎无法聚焦下一个 TextInput(React-Native),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60805268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com