// This component is used to display a time range field in the form, using tailwindcss. It should import Field from "../field"; import { ArrowRightIcon, PlusIcon, TrashIcon } from "@heroicons/react/24/solid"; import Button from "../button"; import { useCallback } from "react"; import { useControlField, useFormContext } from "remix-validated-form"; import { cloneDeep, isEmpty, set } from "lodash-es"; import useStopPropagation from "~/utils/events"; import Show from "../show"; interface TimeRange { from: string; to: string; } // show a time range picker, with a start and end time. export default function TimeRangeField(p: { label: string; name: string; className?: string; fromHour?: string; toHour?: string; }) { const [value, setValue] = useControlField(p.name); const { fieldErrors } = useFormContext(); const stopPropagation = useStopPropagation(); const from = p.fromHour ?? "09:00"; const to = p.toHour ?? "20:00"; const handleToggleCheck = useCallback( (event: React.ChangeEvent) => { if (isEmpty(value)) { setValue([ { from, to, }, ]); } else { setValue([]); } }, [value, to, from, setValue], ); return ( <>
{p.label}
{value?.map((val, index) => (
{ setValue( set(cloneDeep(value), `${index}.from`, e.target.value), ); }} className="w-20" /> { setValue(set(cloneDeep(value), `${index}.to`, e.target.value)); }} className="w-20" />
))} 0}>
); }