2023-01-13 19:09:07 +01:00
|
|
|
import { IDateInput } from '../../../../interfaces/dialog';
|
|
|
|
|
import { Control, useController } from 'react-hook-form';
|
|
|
|
|
import { FormValues } from '../../InputDialog';
|
2023-01-14 14:15:38 +01:00
|
|
|
import { DatePicker, DateRangePicker } 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,
|
2023-01-14 14:15:38 +01:00
|
|
|
defaultValue:
|
|
|
|
|
props.row.default === true
|
|
|
|
|
? new Date()
|
|
|
|
|
: Array.isArray(props.row.default)
|
|
|
|
|
? props.row.default.map((date) => new Date(date))
|
|
|
|
|
: props.row.default && new Date(props.row.default),
|
2023-01-13 19:09:07 +01:00
|
|
|
rules: { required: props.row.required },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
2023-01-14 14:15:38 +01:00
|
|
|
<>
|
|
|
|
|
{props.row.type === 'date' ? (
|
|
|
|
|
<DatePicker
|
|
|
|
|
value={controller.field.value}
|
|
|
|
|
name={controller.field.name}
|
|
|
|
|
ref={controller.field.ref}
|
|
|
|
|
onBlur={controller.field.onBlur}
|
|
|
|
|
onChange={controller.field.onChange}
|
|
|
|
|
label={props.row.label}
|
|
|
|
|
description={props.row.description}
|
|
|
|
|
placeholder={props.row.placeholder}
|
|
|
|
|
disabled={props.row.disabled}
|
|
|
|
|
inputFormat="DD/MM/YYYY"
|
|
|
|
|
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}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<DateRangePicker
|
|
|
|
|
value={controller.field.value}
|
|
|
|
|
name={controller.field.name}
|
|
|
|
|
ref={controller.field.ref}
|
|
|
|
|
onBlur={controller.field.onBlur}
|
|
|
|
|
onChange={controller.field.onChange}
|
|
|
|
|
label={props.row.label}
|
|
|
|
|
description={props.row.description}
|
|
|
|
|
placeholder={props.row.placeholder}
|
|
|
|
|
disabled={props.row.disabled}
|
|
|
|
|
inputFormat="DD/MM/YYYY"
|
|
|
|
|
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-13 19:09:07 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default DateField;
|