Files
ox_lib/web/src/features/dialog/components/fields/date.tsx
BerkieBb 3c0136afd8 feat(web/input): returnString for date inputs (#411)
* feat(interface/input): useFormat field

* feat(interface/input): useFormat field

* feat(types/dialog): useFormat field

* feat(components/date): format date conversion

* tweak(components/date): forgot other data type

* chore(interface/input): more descriptive name

* chore(interface/input): more descriptive name

* chore(types/dialog): more descriptive name

* tweak(components/date): reset things

* feat(dialog/input): date conversion with returnString

* refactor(web/input): remove unnecessary state

---------

Co-authored-by: Luke <39926192+LukeWasTakenn@users.noreply.github.com>
2023-09-11 23:17:35 +02:00

72 lines
2.7 KiB
TypeScript

import { IDateInput } from '../../../../typings/dialog';
import { Control, useController } from 'react-hook-form';
import { FormValues } from '../../InputDialog';
import { DatePicker, DateRangePicker } from '@mantine/dates';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
interface Props {
row: IDateInput;
index: number;
control: Control<FormValues>;
}
const DateField: React.FC<Props> = (props) => {
const controller = useController({
name: `test.${props.index}.value`,
control: props.control,
rules: { required: props.row.required, min: props.row.min, max: props.row.max },
});
return (
<>
{props.row.type === 'date' && (
<DatePicker
value={controller.field.value ? new Date(controller.field.value) : controller.field.value}
name={controller.field.name}
ref={controller.field.ref}
onBlur={controller.field.onBlur}
// Workaround to use timestamp instead of Date object in values
onChange={(date) => controller.field.onChange(date ? date.getTime() : null)}
label={props.row.label}
description={props.row.description}
placeholder={props.row.format}
disabled={props.row.disabled}
inputFormat={props.row.format}
withAsterisk={props.row.required}
clearable={props.row.clearable}
icon={props.row.icon && <FontAwesomeIcon fixedWidth icon={props.row.icon} />}
minDate={props.row.min ? new Date(props.row.min) : undefined}
maxDate={props.row.max ? new Date(props.row.max) : undefined}
/>
)}
{props.row.type === 'date-range' && (
<DateRangePicker
value={
controller.field.value
? controller.field.value[0]
? controller.field.value.map((date: Date) => new Date(date))
: controller.field.value
: controller.field.value
}
name={controller.field.name}
ref={controller.field.ref}
onBlur={controller.field.onBlur}
onChange={(dates) => controller.field.onChange(dates.map((date: Date | null) => date ? date.getTime() : null))}
label={props.row.label}
description={props.row.description}
placeholder={props.row.format}
disabled={props.row.disabled}
inputFormat={props.row.format}
withAsterisk={props.row.required}
clearable={props.row.clearable}
icon={props.row.icon && <FontAwesomeIcon fixedWidth icon={props.row.icon} />}
minDate={props.row.min ? new Date(props.row.min) : undefined}
maxDate={props.row.max ? new Date(props.row.max) : undefined}
/>
)}
</>
);
};
export default DateField;