gpt4 book ai didi

reactjs - 在下次按下时 react native 焦点文本输入

转载 作者:行者123 更新时间:2023-12-05 01:13:48 24 4
gpt4 key购买 nike

注意:我知道有很多类似的问题,我已经尝试对它们实现答案,但没有成功。

在我的 React Native 应用程序中,我有一个接受有限数量的自定义文本输入组件的表单。当用户按下返回按钮时,我正在尝试找到一种解决方案来移动到下一个输入。我尝试了一些软件包,但似乎没有一个与 Android 配合得非常好。我也尝试过使用 refs,但似乎运气不佳(注意表单是一个功能组件)。当我尝试在 float 标签组件中访问它时,它一直返回未定义。

自定义组件:

<FloatLabelTextInput
value={billingFirstName}
onChangeText={newText => setBillingFirstName(newText)}
autoCorrect={false}
style={globalStyles.textInput}
label="Billing First Name"
/>


float 标签文本输入组件呈现文本输入(带有基于 Material 的 float 标签)。


import React, { Component } from 'react';
import {
Alert,
Button,
View,
TextInput,
Animated,
Image,
TouchableOpacity,
} from 'react-native';
import colors from '../utils/colors';

export default class FloatLabelTextInput extends Component<Props> {
static defaultProps = {
editable: true,
showHelp: false,
};

state = {
isFocused: false,
};



componentDidUpdate() {
Animated.timing(this.animatedIsFocused, {
toValue: this.state.isFocused || this.props.value !== '' ? 1 : 0,
duration: 200,
}).start();
}

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

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

focus() {
this.ref.focus();
}

blur() {
this.ref.blur();
}

updateCursorPosition() {
this.ref.setNativeProps({
selection: { start: 0, end: 0 },
});
this.ref.setNativeProps({
selection: {
start: this.props.value.length,
end: this.props.value.length,
},
});
}

showAlert = helpText => {
Alert.alert(helpText.title, helpText.body, [{ text: helpText.button }], {
cancelable: helpText.cancelable,
});
};

render() {
const { label, style, isShowingRightAccessory, ...props } = this.props;
const labelStyle = {
position: 'absolute',
left: 0,
top: this.animatedIsFocused.interpolate({
inputRange: [0, 0.9],
outputRange: [15, 0],
}),
fontSize: this.animatedIsFocused.interpolate({
inputRange: [0, 1],
outputRange: [18, 14],
}),
};
return (
<View
style={[
this.props.style,
{ paddingTop: 18, opacity: this.props.editable ? 1 : 0.5 },
]}>
<Animated.Text
style={[
labelStyle,
{
color: colors.lightBlue,
},
]}
allowFontScaling={false}>
{label}
</Animated.Text>
<TextInput
{...props}
caretHidden={false}
underlineColorAndroid="transparent"
ref={c => {
this.ref = c;
}}
selectionColor={colors.lightBlue}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
blurOnSubmit
allowFontScaling={false}
/>
{this.props.showHelp && (
<TouchableOpacity
style={{
marginTop: 20,
position: 'absolute',
alignSelf: 'flex-end',
}}
onPress={() => this.showAlert(this.props.helpText)}>
<Image
style={{
height: 15,
width: 15,
}}
source={require('../../assets/icon_Tooltip.png')}
/>
</TouchableOpacity>
)}

<View style={{ height: 1, width: '100%', backgroundColor: 'white' }} />
</View>
);
}
}

我的猜测是我想实现一个基于 ref 的解决方案,我正在寻找有关如何使用自定义文本组件来实现这一点的建议

最佳答案

好的,所以我能够通过以下方式使用 useRef 实现此目的:

const billingFirstNameInput = useRef(null);

<FloatLabelTextInput
value={billingFirstName}
onChangeText={newText => setBillingFirstName(newText)}
autoCorrect={false}
style={globalStyles.textInput}
label="Billing First Name"
ref={billingFirstNameInput}
onSubmitEditing={() => {
billingLastNameInput.current.focus()
}}
/>

关于reactjs - 在下次按下时 react native 焦点文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59685588/

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