Open History to see the last commit
1import * as React from 'react'
2import { createRoot } from 'react-dom/client'
3import {
4 getCountries,
5 getCountryCallingCode,
6 isValidPhoneNumber,
7 formatPhoneNumberIntl,
8} from 'react-phone-number-input'
9import en from 'react-phone-number-input/locale/en'
10import { z } from 'zod'
11β
12import { Form } from './Form'
13import { Select } from './Select'
14β
15function Username({ useStore }) {
16 const submissionAttempts = useStore((state) => state.submissionAttempts)
17β
18 function checkUsernameTaken(username) {
19 return new Promise<void>((resolve, reject) => {
20 const isTaken = [
21 'NoobMaster69',
22 'RickAstley',
23 'Michael',
24 'Tanner',
25 ].includes(username)
26 if (isTaken) {
27 reject(`The username ${username} is already taken`)
28 } else {
29 resolve()
30 }
31 })
32 }
33β
34 return (
35 <Form.Field
36 name="username"
37 validators={
38 submissionAttempts !== 0 && {
39 onChange: z
40 .string()
41 .min(3, "Username can't be less than 3 characters"),
42 onChangeAsyncDebounceMs: 500,
43 onChangeAsync: async ({ value }) => {
44 await checkUsernameTaken(value)
45 },
46 }
47 }
48 children={(field) => (
49 <div className="relative">
50 <Form.Field.Text
51 field={field}
52 hasErrorField
53 label="Username"
54 autoComplete="name"
55 maxLength={30}
56 />
57 <Form.Field.Error field={field} />
58 </div>
59 )}
60 />
61 )
62}
63β
64function Phone({ useStore }) {
65 const submissionAttempts = useStore((state) => state.submissionAttempts)
66 const countryCode = getCountryCallingCode(
67 useStore((state) => state.values.country),
68 )
69 return (
70 <div className="relative flex">
71 <Form.Field
72 name="country"
73 children={(field) => (
74 <Form.Field.Select className="rounded-r-none w-20" field={field}>
75 <Select.OptionGroup label="Country code" />
76 {getCountries().map((country) => (
77 <Select.Option
78 key={country}
79 value={country}
80 displayValue={'+' + getCountryCallingCode(country)}
81 >
82 {en[country]} +{getCountryCallingCode(country)}
83 </Select.Option>
84 ))}
85 </Form.Field.Select>
86 )}
87 />
88 <Form.Field
89 name="phoneNumber"
90 validators={
91 submissionAttempts !== 0 && {
92 onChange: z
93 .string()
94 .refine(
95 (value) => isValidPhoneNumber('+' + countryCode + value),
96 'Invalid phone number',
97 ),
98 }
99 }
100 children={(field) => (
101 <div className="relative w-full">
102 <Form.Field.Text
103 className="rounded-l-none"
104 field={field}
105 hasErrorField
106 label="Phone Number"
107 type="tel"
108 autoComplete="tel"
109 maxLength={20}
110 onChange={(e) => {
111 const phoneNumberValue = e.target.value.replace(
112 /[^0-9\s()+-]/g,
113 '',
114 )
115 field.handleChange(phoneNumberValue)
116 }}
117 />
118 <Form.Field.Error field={field} />
119 </div>
120 )}
121 />
122 </div>
123 )
124}
125β
126function Password({ useStore }) {
127 const submissionAttempts = useStore((state) => state.submissionAttempts)
128 const showPassword = useStore((state) => state.values.showPassword)
129 return (
130 <>
131 <Form.Field
132 name="password"
133 validators={
134 submissionAttempts !== 0 && {
135 onChange: z
136 .string()
137 .min(9, "Password can't be less than 9 characters")
138 .max(20, "Password can't be more than 20 characters")
139 .refine(
140 (value) => /[A-Z]/.test(value),
141 'Password must include a capital letter',
142 )
143 .refine(
144 (value) => /\d/.test(value),
145 'Password must include a number',
146 ),
147 }
148 }
149 children={(field) => (
150 <div className="relative">
151 <Form.Field.Text
152 field={field}
153 hasErrorField
154 label="Password"
155 autoComplete="password"
156 type={showPassword ? 'text' : 'password'}
157 />
158 <Form.Field.Error field={field} />
159 </div>
160 )}
161 />
162 <Form.Field
163 name="showPassword"
164 children={(field) => (
165 <Form.Field.Checkbox field={field} label="Show Password" />
166 )}
167 />
168 </>
169 )
170}
171β
172export default function App() {
173 return (
174 <div className="items-center justify-center flex h-full w-full">
175 <Form
176 className="space-y-6 w-72"
177 formOptions={{
178 defaultValues: {
179 username: '',
180 country: 'US',
181 phoneNumber: '',
182 password: '',
183 showPassword: false,
184 },
185 onSubmit: (form) => {
186 const submitValues = {
187 username: form.value.username,
188 phone: formatPhoneNumberIntl(
189 '+' +
190 getCountryCallingCode(form.value.country) +
191 form.value.phoneNumber,
192 ),
193 password: form.value.password,
194 }
195 console.log(submitValues)
196 alert(JSON.stringify(submitValues, null, 2))
197 },
198 }}
199 >
200 {(useStore) => (
201 <>
202 <Username useStore={useStore} />
203 <Phone useStore={useStore} />
204 <Password useStore={useStore} />
205 <div className="flex justify-between items-center">
206 <Form.ResetButton>Reset</Form.ResetButton>
207 <Form.SubmitButton>Submit</Form.SubmitButton>
208 </div>
209 </>
210 )}
211 </Form>
212 </div>
213 )
214}
215β
216const rootElement = document.getElementById('root')!
217β
218createRoot(rootElement).render(<App />)