forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.tsx
85 lines (79 loc) · 2.25 KB
/
input.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import React from 'react';
import { Platform, ScrollView, TextInput, View, Text } from 'react-native';
import Animated, { FadeInDown, FadeOut } from 'react-native-reanimated';
import { Input } from '~/components/universal-ui/input';
import { Label, LabelText } from '~/components/universal-ui/label';
import { cn } from '~/lib/utils';
export default function InputScreen() {
const inputRef = React.useRef<TextInput>(null);
const [value, setValue] = React.useState<string>('');
const [err, setErr] = React.useState<string | null>(null);
function handleOnLabelPress() {
if (!inputRef.current) {
return;
}
if (inputRef.current.isFocused()) {
inputRef.current?.blur();
} else {
inputRef.current?.focus();
}
}
function onChangeText(text: string) {
if (err) {
setErr(null);
}
setValue(text);
}
function onSubmitEditing() {
setErr('Write more stuff to remove this error message.');
}
return (
<ScrollView contentContainerClassName='flex-1 justify-center items-center p-6'>
<View className='web:max-w-xs w-full'>
<Label onPress={handleOnLabelPress}>
<LabelText
className={cn(err && 'text-destructive', 'pb-2 native:pb-1 pl-0.5')}
nativeID='inputLabel'
>
Label
</LabelText>
</Label>
<Input
ref={inputRef}
placeholder='Write some stuff...'
value={value}
onChangeText={onChangeText}
onSubmitEditing={onSubmitEditing}
aria-labelledbyledBy='inputLabel'
aria-errormessage='inputError'
/>
{err && <ErrorMessage msg={err} />}
<View className='h-20' />
</View>
</ScrollView>
);
}
function ErrorMessage({ msg }: { msg: string }) {
if (Platform.OS === 'web') {
return (
<Text
className='text-destructive text-sm native:px-1 py-1.5 web:animate-in web:zoom-in-95'
aria-invalid='true'
id='inputError'
>
{msg}
</Text>
);
}
return (
<Animated.Text
entering={FadeInDown}
exiting={FadeOut.duration(275)}
className='text-destructive text-sm native:px-1 py-1.5'
aria-invalid='true'
id='inputError'
>
{msg}
</Animated.Text>
);
}