82 lines
1.9 KiB
TypeScript
82 lines
1.9 KiB
TypeScript
import React, { useEffect } from 'react';
|
|
import { View, Animated, StyleSheet, StyleProp, ViewStyle } from 'react-native';
|
|
import TextFieldBase, { TextFieldBaseProps } from './TextFieldBase';
|
|
import { Text } from 'native-base';
|
|
import { Warning2 } from 'iconsax-react-native';
|
|
|
|
interface TextFormFieldProps extends TextFieldBaseProps {
|
|
style?: StyleProp<ViewStyle>;
|
|
error: string | null;
|
|
}
|
|
|
|
const ERROR_HEIGHT = 25;
|
|
const ERROR_PADDING_TOP = 8;
|
|
|
|
const TextFormField: React.FC<TextFormFieldProps> = ({ error, style, ...textFieldBaseProps }) => {
|
|
const fadeAnim = React.useRef(new Animated.Value(0)).current;
|
|
const heightAnim = React.useRef(new Animated.Value(0)).current;
|
|
const paddingTopAnim = React.useRef(new Animated.Value(0)).current;
|
|
|
|
useEffect(() => {
|
|
Animated.parallel([
|
|
Animated.timing(fadeAnim, {
|
|
toValue: error ? 1 : 0,
|
|
duration: 150,
|
|
useNativeDriver: false,
|
|
}),
|
|
Animated.timing(heightAnim, {
|
|
toValue: error ? ERROR_HEIGHT : 0,
|
|
duration: 250,
|
|
useNativeDriver: false,
|
|
}),
|
|
Animated.timing(paddingTopAnim, {
|
|
toValue: error ? ERROR_PADDING_TOP : 0,
|
|
duration: 250,
|
|
useNativeDriver: false,
|
|
}),
|
|
]).start();
|
|
}, [error]);
|
|
|
|
return (
|
|
<View style={[styles.wrapper, style]}>
|
|
<TextFieldBase iconColor={error ? '#f7253d' : undefined} {...textFieldBaseProps} />
|
|
<Animated.View
|
|
style={{
|
|
...styles.errorContainer,
|
|
opacity: fadeAnim,
|
|
height: heightAnim,
|
|
paddingTop: paddingTopAnim,
|
|
}}
|
|
>
|
|
<Warning2 size="16" color="#f7253d" variant="Bold" />
|
|
<Text
|
|
isTruncated
|
|
maxW={'100%'}
|
|
fontSize={styles.errorText.fontSize}
|
|
color={styles.errorText.color}
|
|
>
|
|
{error}
|
|
</Text>
|
|
</Animated.View>
|
|
</View>
|
|
);
|
|
};
|
|
|
|
const styles = StyleSheet.create({
|
|
wrapper: {
|
|
width: '100%',
|
|
},
|
|
errorContainer: {
|
|
display: 'flex',
|
|
gap: 8,
|
|
flexDirection: 'row',
|
|
paddingLeft: 12,
|
|
},
|
|
errorText: {
|
|
color: '#f7253d',
|
|
fontSize: 12,
|
|
},
|
|
});
|
|
|
|
export default TextFormField;
|