File size: 1,787 Bytes
1e92f2d |
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 type { TextFieldProps } from '@mui/material/TextField';
import TextField from '@mui/material/TextField';
import React from 'react';
import type { FieldProps } from 'uniforms';
import { connectField, filterDOMProps } from 'uniforms';
/* istanbul ignore next */
const DateConstructor = (typeof global === 'object' ? global : window).Date;
const dateFormat = (value?: Date) => value && value.toISOString().slice(0, -8);
const dateParse = (timestamp: number, onChange: DateFieldProps['onChange']) => {
const date = new DateConstructor(timestamp);
if (date.getFullYear() < 10000) {
onChange(date);
} else if (isNaN(timestamp)) {
onChange(undefined);
}
};
export type DateFieldProps = FieldProps<
Date,
TextFieldProps,
{ labelProps?: Record<string, string> }
>;
function Date({
disabled,
error,
errorMessage,
helperText,
InputLabelProps,
inputRef,
label,
labelProps,
name,
onChange,
placeholder,
readOnly,
showInlineError,
value,
...props
}: DateFieldProps) {
return (
<TextField
disabled={disabled}
error={!!error}
fullWidth
helperText={(error && showInlineError && errorMessage) || helperText}
label={label}
InputLabelProps={{ shrink: true, ...labelProps, ...InputLabelProps }}
inputProps={{ readOnly, ...props.inputProps }}
margin="dense"
name={name}
onChange={(event) =>
// FIXME: `valueAsNumber` is not available in `EventTarget`.
disabled || dateParse((event.target as any).valueAsNumber, onChange)
}
placeholder={placeholder}
ref={inputRef}
type="datetime-local"
value={dateFormat(value) ?? ''}
{...filterDOMProps(props)}
/>
);
}
export default connectField<DateFieldProps>(Date, { kind: 'leaf' });
|