-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.tsx
45 lines (38 loc) · 1.11 KB
/
date.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
import { useId } from 'react';
import { useAtom } from 'jotai';
import { atomWithSchema } from 'jotai-decode-form';
import { DisplayInternalValue } from '../utils/DisplayInternalValues';
import { ErrorText } from '../utils/ErrorText';
import {
UTC,
toJSTFormat,
datetimeLocalJST2UTC,
DatetimeLocalJST,
} from '../utils/date';
const dateAtom = atomWithSchema<UTC>({
schema: {
toView: utc => toJSTFormat(utc, 'YYYY-MM-DD HH:mm'),
fromView: DatetimeLocalJST.transform(datetimeLocalJST2UTC).safeParse,
},
});
export const Field: React.FC = () => {
const id = useId();
const [field, onChange] = useAtom(dateAtom);
return (
<div className="field_wrap">
<div className="field">
<label htmlFor={id}>date (JST)</label>
<input
id={id}
type="datetime-local"
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} label="UTC" />
</div>
);
};