import { clsx } from 'clsx'; import { createRef, PureComponent, KeyboardEvent } from 'react'; import Calendar from '../Calendar'; import type { DateChangeMeta, DisabledRules, OnDateSelectParams } from '../Calendar/Calendar.types'; import { Size, MonthFormat, Position, type SizeSmall, type SizeMedium, type SizeLarge, } from '../common'; import ResponsivePanel from '../common/ResponsivePanel'; import { WithInputAttributesProps, withInputAttributes } from '../Inputs/contexts'; import { OverlayIdProvider } from '../Provider/overlay/OverlayIdProvider'; import DateTrigger from './DateTrigger'; export interface DateLookupProps { /** The currently selected date, or `null` for no selection. */ value: Date | null; /** Called when an enabled date is confirmed. Receives `null` when the value is cleared. */ onChange: (date: Date | null) => void; /** HTML `id` applied to the trigger button, used for label association. */ id?: string; /** Earliest selectable date (inclusive). Dates before this are interactive but not selectable, and show a boundary prompt. */ min?: Date | null; /** Latest selectable date (inclusive). Dates after this are interactive but not selectable, and show a boundary prompt. */ max?: Date | null; /** Size of the trigger input. Defaults to `'md'`. */ size?: SizeSmall | SizeMedium | SizeLarge; /** Label text rendered above the trigger input. */ label?: string; /** ID of an external element that labels the trigger, for use inside custom label contexts. */ 'aria-labelledby'?: string; /** Placeholder text shown in the trigger when no date is selected. */ placeholder?: string; /** Month name format in the calendar header. Defaults to `'long'` (e.g. "January"). */ monthFormat?: `${MonthFormat}`; /** When `true`, the trigger and calendar are non-interactive. */ disabled?: boolean; /** When `true`, a clear button is shown in the trigger to reset the value to `null`. */ clearable?: boolean; /** Rules for disabling specific weekdays, individual dates, or date ranges. The required `reason` is shown in an inline prompt and linked to disabled cells via `aria-describedby` for screen reader accessibility. */ disabledRules?: DisabledRules; /** Called whenever any date is focused or clicked, including disabled and out-of-range dates. `isEnabled` is `false` when the date is not selectable. */ onDateSelect?: (params: OnDateSelectParams) => void; /** Called when the calendar panel opens. */ onFocus?: () => void; /** Called when the calendar panel closes. */ onBlur?: () => void; } type DateLookupPropsWithInputAttributes = DateLookupProps & Partial; interface DateLookupState { originalDate: Date | null; open: boolean; } class DateLookup extends PureComponent { declare props: DateLookupPropsWithInputAttributes & Required>; static defaultProps = { value: null, min: null, max: null, size: Size.MEDIUM, placeholder: '', label: '', monthFormat: MonthFormat.LONG, disabled: false, clearable: false, } satisfies Partial; element = createRef(); constructor(props: DateLookup['props']) { super(props); this.state = { originalDate: null, open: false, }; } componentDidUpdate(previousProps: DateLookupPropsWithInputAttributes) { if (this.props.value?.getTime() !== previousProps.value?.getTime() && this.state.open) { this.focusOn('.active'); } } open = () => { const { onFocus } = this.props; this.setState({ open: true }); if (onFocus) { onFocus(); } }; discard = () => { const { originalDate } = this.state; if (originalDate !== null) { this.props.onChange(originalDate); } this.close(); }; close = () => { const { onBlur } = this.props; this.setState({ open: false, originalDate: null }); if (onBlur) { onBlur(); } }; handleKeyDown = (event: KeyboardEvent) => { const { open, originalDate } = this.state; switch (event.key) { case 'ArrowLeft': case 'ArrowUp': case 'ArrowRight': case 'ArrowDown': if (!open) { this.open(); event.preventDefault(); } break; case 'Escape': if (originalDate !== null) { this.props.onChange(originalDate); } this.close(); event.preventDefault(); break; default: break; } }; focusOn = (preferredElement: string, fallbackElement?: string) => { const element = this.element.current?.querySelector(preferredElement) as HTMLElement | null; if (element) { element.focus(); } else if (fallbackElement) { this.focusOn(fallbackElement); } }; handleDateChange = (date: Date | null, meta: DateChangeMeta) => { const { onDateSelect } = this.props; if (date && onDateSelect) { onDateSelect({ date, isEnabled: meta.isEnabled }); } }; handleDateSelect = (date: Date | null) => { if (!date) return; this.setState( (state) => { const originalDate = state.originalDate ?? this.props.value; return { originalDate }; }, () => { this.props.onChange(date); this.close(); this.focusOn('.btn'); }, ); }; getCalendar = () => { const { value, min, max, monthFormat, placeholder, disabledRules } = this.props; return (
); }; handleClear = () => { this.props.onChange(null); this.focusOn('.np-date-trigger'); }; render() { const { open } = this.state; const { inputAttributes, id: idProp, 'aria-labelledby': ariaLabelledByProp, size, placeholder, label, monthFormat, disabled, clearable, value, } = this.props; const id = idProp ?? inputAttributes?.id; const ariaLabelledBy = ariaLabelledByProp ?? inputAttributes?.['aria-labelledby']; return (
{this.getCalendar()}
); } } export const DateLookupWithoutInputAttributes = DateLookup; const WrappedDateLookup = withInputAttributes( DateLookup as React.ComponentType, { nonLabelable: true }, ); WrappedDateLookup.displayName = 'DateLookup'; export default WrappedDateLookup;