【发布时间】:2021-08-05 01:58:35
【问题描述】:
我是用 expo 做出原生反应的新手,我创建了一个功能组件,它试图做类似于 react-native-paper 的 TextInput 行为的事情,包括动画等等。
问题是我在同一个屏幕上有这个组件的几个实例,当我按下第一个写入它时,其余的也会得到关注。如果我在第一个中填写任何文本,第二个会触发通知而不会被触摸。
我正在使用 formik、Yup 和 typescript。
这就是我在表单中调用组件的方式:
...
<ScrollView>
<FloatingLabelInput
label="Ciudad"
marginBottom={20}
onChangeText={text => {
formik.setFieldValue('city', text)
}}
value={formik.values.city}
error={formik.errors.city}
/>
<FloatingLabelInput
label="Calle principal"
marginBottom={20}
onChangeText={text => {
formik.setFieldValue('mainStreet', text)
}}
value={formik.values.mainStreet}
error={formik.errors.mainStreet}
/>
</ScrollView>
....
组件定义:
...
export default function FloatingLabelInput(props: FloatingLabelInputProps) {
const { label, style, onChangeText, value, error, containerStyle, border } = props
useEffect(() => {
if (value === '') {
resetViewTransition.start()
resetLabelTransition.start()
}
}, [value])
const handleFocus = () => {
animateViewTransition.start()
animateLabelTransition.start()
}
const handleBlur = () => {
if (value === '') {
resetViewTransition.start()
resetLabelTransition.start()
}
}
const handleTextChange = (text: string) => {
if (text.length > 0) {
animateViewTransition.start()
animateLabelTransition.start()
}
onChangeText && onChangeText(text)
}
return (
<View style={styles(marginBottom).mainContainer}>
<Animated.View style={[styles().container, error ? styles().containerError : border !== undefined && { borderColor: viewAnimationBorderColor(border) }, containerStyle]}>
<Animated.Text style={[styles().labelStyle, { top: labelAnimationTop, fontSize: labelAnimationFontSize, color: labelAnimationColor }]}>{label}</Animated.Text>
{secureTextEntry && <Icon name={passwordShown ? 'eye-outline' : 'eye-off-outline'} size={25} style={styles().eye} onPress={() => setPasswordShown(!passwordShown)} />}
<TextInput
selectionColor={error ? colors.fireEngineRed : border ? border : colors.nightRider}
underlineColorAndroid="transparent"
style={[styles().input, { color: error ? colors.fireEngineRed : border ? border : colors.nightRider }, style]}
onChangeText={handleTextChange}
value={value}
onFocus={handleFocus}
onBlur={handleBlur}
keyboardType={defineKeyBoardType}
maxLength={maxLength}
secureTextEntry={secureTextEntry && !passwordShown}
keyboardAppearance="dark"
/>
</Animated.View>
{error && (
<HelperText type="error" style={styles().errorText}>
{error}
</HelperText>
)}
</View>
)
}
【问题讨论】:
标签: typescript react-native expo react-functional-component