import { forwardRef } from 'react'; import DatePicker from 'react-datepicker'; import 'react-datepicker/dist/react-datepicker.css'; import { formatDateInput, formatDateTimeLocalInput, parseDateInput, parseDateTimeLocalInput } from '../../lib/localDateTime'; type BaseProps = { label?: string; description?: string; disabled?: boolean; className?: string; /** Use `div` when this field sits inside another label (e.g. course builder repeaters). */ container?: 'label' | 'div'; }; type DateOnlyProps = BaseProps & { kind: 'date'; value: string; onChange: (next: string) => void; placeholder?: string; }; type DateTimeProps = BaseProps & { kind: 'datetime'; value: string; onChange: (next: string) => void; placeholder?: string; }; type Props = DateOnlyProps | DateTimeProps; const Input = forwardRef>(function Input(props, ref) { const { className = '', ...rest } = props; return ( ); }); /** * Shared date / datetime picker field using `react-datepicker` (no HTML5 date inputs). * Stores values in the same string formats we previously used: * - date: YYYY-MM-DD * - datetime: YYYY-MM-DDTHH:mm */ export function DateTimePickerField(props: Props) { const { label, description, disabled = false, className = '', container = 'label', placeholder = props.kind === 'date' ? 'Select a date…' : 'Select date & time…', } = props; const selected = props.kind === 'date' ? parseDateInput(props.value) : parseDateTimeLocalInput(props.value); const onPick = (d: Date | null) => { if (props.kind === 'date') { props.onChange(formatDateInput(d)); } else { props.onChange(formatDateTimeLocalInput(d)); } }; const Shell = container === 'div' ? 'div' : 'label'; return ( {label ? {label} : null} {description ? {description} : null}
} wrapperClassName="w-full" popperPlacement="bottom-start" />
); }