2023-02-15 13:00:09 +01:00
|
|
|
import { IDateInput } from '../../../../typings/dialog';
|
2023-01-13 19:09:07 +01:00
|
|
|
import { Control, useController } from 'react-hook-form';
|
|
|
|
|
import { FormValues } from '../../InputDialog';
|
2023-01-31 12:41:42 -05:00
|
|
|
import { DatePicker, DateRangePicker, TimeInput } from '@mantine/dates';
|
2023-01-14 13:56:05 +01:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2023-01-13 19:09:07 +01:00
|
|
|
|
|
|
|
|
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 },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
2023-01-14 14:15:38 +01:00
|
|
|
<>
|
2023-01-31 12:41:42 -05:00
|
|
|
{props.row.type === 'date' && (
|
2023-01-14 14:15:38 +01:00
|
|
|
<DatePicker
|
2023-01-14 19:02:25 +01:00
|
|
|
value={controller.field.value ? new Date(controller.field.value) : controller.field.value}
|
2023-01-14 14:15:38 +01:00
|
|
|
name={controller.field.name}
|
|
|
|
|
ref={controller.field.ref}
|
|
|
|
|
onBlur={controller.field.onBlur}
|
2023-01-14 19:02:25 +01:00
|
|
|
// Workaround to use timestamp instead of Date object in values
|
|
|
|
|
onChange={(date) => controller.field.onChange(date ? date.getTime() : null)}
|
2023-01-14 14:15:38 +01:00
|
|
|
label={props.row.label}
|
|
|
|
|
description={props.row.description}
|
2023-02-14 11:17:39 +01:00
|
|
|
placeholder={props.row.format}
|
2023-01-14 14:15:38 +01:00
|
|
|
disabled={props.row.disabled}
|
2023-02-14 11:17:39 +01:00
|
|
|
inputFormat={props.row.format}
|
2023-01-14 14:15:38 +01:00
|
|
|
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}
|
|
|
|
|
/>
|
2023-02-14 10:54:53 +01:00
|
|
|
)}
|
2023-01-31 12:41:42 -05:00
|
|
|
{props.row.type === 'date-range' && (
|
2023-02-14 10:54:53 +01:00
|
|
|
<DateRangePicker
|
|
|
|
|
value={
|
|
|
|
|
controller.field.value
|
|
|
|
|
? controller.field.value[0]
|
|
|
|
|
? controller.field.value.map((date: Date) => new Date(date))
|
2023-01-14 19:02:25 +01:00
|
|
|
: controller.field.value
|
2023-02-14 10:54:53 +01:00
|
|
|
: 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}
|
2023-02-14 11:17:39 +01:00
|
|
|
placeholder={props.row.format}
|
2023-02-14 10:54:53 +01:00
|
|
|
disabled={props.row.disabled}
|
2023-02-14 11:17:39 +01:00
|
|
|
inputFormat={props.row.format}
|
2023-02-14 10:54:53 +01:00
|
|
|
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}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2023-01-14 14:15:38 +01:00
|
|
|
</>
|
2023-01-13 19:09:07 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default DateField;
|