Open History to see the last commit
1import {
2 Button,
3 SafeAreaView,
4 StyleSheet,
5 Text,
6 TextInput,
7 View,
8} from 'react-native'
9β
10import { type } from 'arktype'
11import { Schema as S } from 'effect'
12import * as v from 'valibot'
13import { z } from 'zod'
14β
15import { useForm } from '@tanstack/react-form'
16β
17import type { AnyFieldApi } from '@tanstack/react-form'
18β
19function FieldInfo({ field }: { field: AnyFieldApi }) {
20 return (
21 <View>
22 {field.state.meta.isTouched && !field.state.meta.isValid ? (
23 <Text style={styles.error}>
24 {field.state.meta.errors.map((err) => err.message).join(',')}
25 </Text>
26 ) : null}
27 {field.state.meta.isValidating ? <Text>Validating...</Text> : null}
28 </View>
29 )
30}
31β
32const ZodSchema = z.object({
33 firstName: z
34 .string()
35 .min(3, '[Zod] You must have a length of at least 3')
36 .startsWith('A', "[Zod] First name must start with 'A'"),
37 lastName: z.string().min(3, '[Zod] You must have a length of at least 3'),
38})
39β
40// eslint-disable-next-line @typescript-eslint/no-unused-vars
41const ValibotSchema = v.object({
42 firstName: v.pipe(
43 v.string(),
44 v.minLength(3, '[Valibot] You must have a length of at least 3'),
45 v.startsWith('A', "[Valibot] First name must start with 'A'"),
46 ),
47 lastName: v.pipe(
48 v.string(),
49 v.minLength(3, '[Valibot] You must have a length of at least 3'),
50 ),
51})
52β
53// eslint-disable-next-line @typescript-eslint/no-unused-vars
54const ArkTypeSchema = type({
55 firstName: 'string >= 3',
56 lastName: 'string >= 3',
57})
58β
59// eslint-disable-next-line @typescript-eslint/no-unused-vars
60const EffectSchema = S.standardSchemaV1(
61 S.Struct({
62 firstName: S.String.pipe(
63 S.minLength(3),
64 S.annotations({
65 message: () => '[Effect/Schema] You must have a length of at least 3',
66 }),
67 ),
68 lastName: S.String.pipe(
69 S.minLength(3),
70 S.annotations({
71 message: () => '[Effect/Schema] You must have a length of at least 3',
72 }),
73 ),
74 }),
75)
76β
77export default function App() {
78 const form = useForm({
79 defaultValues: {
80 firstName: '',
81 lastName: '',
82 },
83 validators: {
84 // DEMO: You can switch between schemas seamlessly
85 onChange: ZodSchema,
86 // onChange: ValibotSchema,
87 // onChange: ArkTypeSchema,
88 // onChange: EffectSchema,
89 },
90 onSubmit: async ({ value }) => {
91 // Do something with form data
92 console.log(value)
93 },
94 })
95β
96 return (
97 <SafeAreaView style={styles.container}>
98 <Text style={styles.title}>Standard Schema Form Example</Text>
99β
100 <View style={styles.field}>
101 <form.Field
102 name="firstName"
103 children={(field) => (
104 <>
105 <Text style={styles.label}>First Name:</Text>
106 <TextInput
107 style={styles.input}
108 value={field.state.value}
109 onBlur={field.handleBlur}
110 onChangeText={(text) => field.handleChange(text)}
111 autoCapitalize="none"
112 autoCorrect={false}
113 />
114 <FieldInfo field={field} />
115 </>
116 )}
117 />
118 </View>
119β
120 <View style={styles.field}>
121 <form.Field
122 name="lastName"
123 children={(field) => (
124 <>
125 <Text style={styles.label}>Last Name:</Text>
126 <TextInput
127 style={styles.input}
128 value={field.state.value}
129 onBlur={field.handleBlur}
130 onChangeText={(text) => field.handleChange(text)}
131 autoCapitalize="none"
132 autoCorrect={false}
133 />
134 <FieldInfo field={field} />
135 </>
136 )}
137 />
138 </View>
139β
140 <form.Subscribe
141 selector={(state) => [state.canSubmit, state.isSubmitting]}
142 children={([canSubmit, isSubmitting]) => (
143 <Button
144 title={isSubmitting ? '...' : 'Submit'}
145 disabled={!canSubmit}
146 onPress={() => form.handleSubmit()}
147 />
148 )}
149 />
150 </SafeAreaView>
151 )
152}
153β
154const styles = StyleSheet.create({
155 container: {
156 flex: 1,
157 padding: 16,
158 gap: 12,
159 },
160 title: {
161 fontSize: 20,
162 fontWeight: '600',
163 marginBottom: 8,
164 },
165 field: {
166 gap: 4,
167 },
168 label: {
169 fontSize: 14,
170 fontWeight: '500',
171 },
172 input: {
173 borderWidth: 1,
174 borderColor: '#ccc',
175 borderRadius: 6,
176 paddingHorizontal: 10,
177 paddingVertical: 8,
178 fontSize: 16,
179 },
180 error: {
181 color: '#c00',
182 fontSize: 13,
183 },
184})