-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.tsx
67 lines (58 loc) · 1.77 KB
/
select.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
import { useId } from 'react';
import { z } from 'zod';
import { useAtom } from 'jotai';
import { atomWithSchema, success } from 'jotai-decode-form';
import { DisplayInternalValue } from '../utils/DisplayInternalValues';
import { ErrorText } from '../utils/ErrorText';
type Theme = { mode: 'light' } | { mode: 'dark' };
type ThemeOption = z.infer<typeof ThemeOption>;
const ThemeOption = z.enum(['light mode', 'dark mode']);
const themeAtom = atomWithSchema<Theme, ThemeOption>({
schema: {
toView: theme => {
switch (theme.mode) {
case 'light':
return 'light mode';
case 'dark':
return 'dark mode';
default:
throw new Error(`${theme satisfies never}`);
}
},
fromView: value => {
const parsed = ThemeOption.safeParse(value);
if (!parsed.success) return parsed;
switch (parsed.data) {
case 'light mode':
return success({ mode: 'light' });
case 'dark mode':
return success({ mode: 'dark' });
default:
throw new Error(`${parsed.data satisfies never}`);
}
},
},
});
export const Field: React.FC = () => {
const id = useId();
const [field, onChange] = useAtom(themeAtom);
return (
<div className="field_wrap">
<div className="field">
<label htmlFor={id}>mode</label>
<select
id={id}
value={field.exValue}
onChange={e => onChange(e.target.value as ThemeOption)}
>
<option>light mode</option>
<option>dark mode</option>
</select>
</div>
{field.state.isDirty && field.state.error != null && (
<ErrorText error={field.state.error} />
)}
<DisplayInternalValue value={field.value} label="Theme type" />
</div>
);
};