main
ts 26 lines 1.24 KB
Raw
1 import { DateTimePicker } from '@mui/x-date-pickers'
2 import dayjs from 'dayjs'
3 import { FieldProps } from '@hfs/mui-grid-form'
4 import { createElement as h } from 'react'
5 import { Box } from '@mui/material'
6 import { isTimestampString } from './misc'
7 import _ from 'lodash'
8 import { mergeSx } from './mui'
9
10 export function DateTimeField({ onChange, error, helperText, ...rest }: FieldProps<Date>) {
11 return h(Box, {},
12 h(DateTimePicker, {
13 ..._.mapValues(rest, x => isTimestampString(x) || x && x instanceof Date ? dayjs(x) : (x ?? null)), // null to not be considered uncontrolled
14 sx: mergeSx({ width: '100%', color: 'error.main' }, rest.sx),
15 onChange(v: any) {
16 onChange(v && new Date(v), { was: rest.value, event: undefined })
17 },
18 slotProps: {
19 // keep the same field look as StringField (filled variant) for visual consistency
20 textField: { variant: 'filled', error, helperText },
21 // under 400, not all buttons fit, so we sacrifice 'cancel' as you can still tap outside the dialog
22 actionBar: { actions: ['clear', ...(window.innerWidth < 400 ? [] : ['cancel'] as const), 'today', 'accept'] }
23 }
24 }),
25 )
26 }