|
| 1 | +import { DateTimePicker, DateTimePickerProps } from '@mui/x-date-pickers/DateTimePicker' |
| 2 | +import { |
| 3 | + Control, |
| 4 | + Controller, |
| 5 | + ControllerProps, |
| 6 | + FieldError, Path |
| 7 | +} from 'react-hook-form' |
| 8 | +import { TextField, TextFieldProps } from '@mui/material' |
| 9 | +import { FieldValues } from 'react-hook-form/dist/types/fields' |
| 10 | + |
| 11 | +export declare type ParseableDate<TDate> = |
| 12 | + | string |
| 13 | + | number |
| 14 | + | Date |
| 15 | + | null |
| 16 | + | undefined |
| 17 | + | TDate |
| 18 | + |
| 19 | +export type DateTimePickerElementProps<T, TInputDate, TDate = TInputDate> = Omit<DateTimePickerProps<TInputDate, TDate>, |
| 20 | + 'value' | 'onChange' | 'renderInput'> & { |
| 21 | + name: Path<T> |
| 22 | + required?: boolean |
| 23 | + isDate?: boolean |
| 24 | + parseError?: (error: FieldError) => string |
| 25 | + onChange?: (value: TDate, keyboardInputValue?: string) => void |
| 26 | + validation?: ControllerProps['rules'] |
| 27 | + parseDate?: (value: TDate, keyboardInputValue?: string) => TDate |
| 28 | + control?: Control<T> |
| 29 | + inputProps?: TextFieldProps |
| 30 | + helperText?: TextFieldProps['helperText'] |
| 31 | +} |
| 32 | + |
| 33 | +export default function DateTimePickerElement<TFieldValues extends FieldValues>({ |
| 34 | + isDate, |
| 35 | + parseError, |
| 36 | + name, |
| 37 | + required, |
| 38 | + parseDate, |
| 39 | + validation = {}, |
| 40 | + inputProps, |
| 41 | + control, |
| 42 | + ...rest |
| 43 | +}: DateTimePickerElementProps<TFieldValues, any, any>): JSX.Element { |
| 44 | + |
| 45 | + if (required && !validation.required) { |
| 46 | + validation.required = 'This field is required' |
| 47 | + } |
| 48 | + |
| 49 | + return ( |
| 50 | + <Controller |
| 51 | + name={name} |
| 52 | + rules={validation} |
| 53 | + control={control} |
| 54 | + render={({ |
| 55 | + field: { onChange, value }, |
| 56 | + fieldState: { error, invalid } |
| 57 | + }) => ( |
| 58 | + <DateTimePicker |
| 59 | + {...rest} |
| 60 | + value={value || ''} |
| 61 | + onChange={(value, keyboardInputValue) => { |
| 62 | + let newValue = undefined |
| 63 | + if (keyboardInputValue) { |
| 64 | + if (typeof parseDate === 'function') { |
| 65 | + newValue = parseDate(value, keyboardInputValue) |
| 66 | + } else { |
| 67 | + newValue = keyboardInputValue |
| 68 | + } |
| 69 | + } else { |
| 70 | + if (typeof parseDate === 'function') { |
| 71 | + newValue = parseDate(value) |
| 72 | + } else { |
| 73 | + newValue = value |
| 74 | + } |
| 75 | + } |
| 76 | + onChange(newValue, keyboardInputValue) |
| 77 | + if (typeof rest.onChange === 'function') { |
| 78 | + rest.onChange(newValue, keyboardInputValue) |
| 79 | + } |
| 80 | + }} |
| 81 | + renderInput={(params) => ( |
| 82 | + <TextField |
| 83 | + {...params} |
| 84 | + inputProps={{ |
| 85 | + ...params?.inputProps, |
| 86 | + ...(!value && { |
| 87 | + value: '' |
| 88 | + }) |
| 89 | + }} |
| 90 | + {...inputProps} |
| 91 | + required={!!required} |
| 92 | + error={invalid} |
| 93 | + helperText={ |
| 94 | + error |
| 95 | + ? typeof parseError === 'function' |
| 96 | + ? parseError(error) |
| 97 | + : error.message |
| 98 | + : inputProps?.helperText || rest.helperText |
| 99 | + } |
| 100 | + /> |
| 101 | + )} |
| 102 | + /> |
| 103 | + )} |
| 104 | + /> |
| 105 | + ) |
| 106 | +} |
0 commit comments