-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_number.tsx
43 lines (35 loc) · 1.06 KB
/
basic_number.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
import { useId } from 'react';
import { useAtom } from 'jotai';
import { z } from 'zod';
import { atomWithSchema } from 'jotai-decode-form';
import { ErrorText } from '../utils/ErrorText';
import { NumberFromString } from '../utils/NumberFromString';
import { DisplayInternalValue } from '../utils/DisplayInternalValues';
const Age = z.number().min(0).max(100);
type Age = z.infer<typeof Age>;
const widthAtom = atomWithSchema<Age>({
schema: {
toView: String,
fromView: NumberFromString.pipe(Age).safeParse,
},
});
export const Field: React.FC = () => {
const id = useId();
const [field, onChange] = useAtom(widthAtom);
return (
<div className="field_wrap">
<div className="field">
<label htmlFor={id}>age</label>
<input
id={id}
value={field.exValue}
onChange={e => onChange(e.target.value)}
/>
</div>
{field.state.isDirty && field.state.error != null && (
<ErrorText error={field.state.error} />
)}
<DisplayInternalValue value={field.value} />
</div>
);
};