/** * Datepicker component. * * @augments IvComponent */ export class Datepicker extends IvComponent { /** @type {Readonly} */ static defaults: Readonly; /** * Returns the datepicker instance attached to `el`, if any. * * @param {Element} el Host element. * @returns {Datepicker|undefined} The instance or `undefined`. */ static get(el: Element): Datepicker | undefined; /** * Returns the existing instance or creates one. * * @param {Element} el Host element. * @param {Partial} [options] Options passed in JavaScript. * @returns {Datepicker} The instance. */ static getOrCreate(el: Element, options?: Partial): Datepicker; /** * Instantiates every `[data-iv-component="datepicker"]` inside `root`. * * @param {ParentNode} [root] Subtree to scan. * @returns {Datepicker[]} Newly created instances. */ static initAll(root?: ParentNode): Datepicker[]; /** * @param {Element} el A `
` element. * @param {Partial} [options] Options passed in JavaScript. * @throws {IvError} `invalid-element` when there is no date input to enrich. */ constructor(el: Element, options?: Partial); /** @type {HTMLInputElement} The native input, kept as the source of truth. */ _input: HTMLInputElement; /** @type {HTMLElement} The box holding the input, the toggle and the popover. */ _control: HTMLElement; /** @type {HTMLButtonElement} The generated toggle. */ _toggle: HTMLButtonElement; /** @type {HTMLElement} The calendar dialog. */ _popover: HTMLElement; /** @type {HTMLElement} The month and year heading. */ _title: HTMLElement; /** @type {HTMLButtonElement} The previous month button. */ _prev: HTMLButtonElement; /** @type {HTMLButtonElement} The next month button. */ _next: HTMLButtonElement; /** @type {HTMLElement} The calendar grid. */ _grid: HTMLElement; /** @type {HTMLElement} The body of the grid, rebuilt on every repaint. */ _body: HTMLElement; /** @type {HTMLButtonElement} The "today" button. */ _todayButton: HTMLButtonElement; /** @type {HTMLButtonElement} The "clear" button. */ _clearButton: HTMLButtonElement; /** @type {Map>} Attributes to restore. */ _saved: Map>; /** @type {string[]} Attribute names of the input, in their original order. */ _inputAttributes: string[]; /** @type {boolean} Whether the calendar replaced the native picker. */ _enhanced: boolean; /** @type {boolean} Whether the calendar is open. */ _open: boolean; /** @type {Date} First day of the painted month. */ _view: Date; /** @type {Date} The day that owns the single tab stop of the grid. */ _cursor: Date; /** @type {number} Resolved first column of the grid. */ _firstDay: number; /** @type {string|undefined} Resolved locale. */ _locale: string | undefined; /** @type {boolean} Guard against the synthetic `input`/`change` events. */ _silent: boolean; /** @type {EventListener|null} Outside pointer listener while open. */ _outside: EventListener | null; /** @type {EventListener|null} Viewport resize listener while open. */ _resize: EventListener | null; /** @type {Intl.DateTimeFormat} Month and year, for the heading. */ _monthFormat: Intl.DateTimeFormat; /** @type {Intl.DateTimeFormat} Full date, for the label of a day. */ _dayFormat: Intl.DateTimeFormat; /** * Resolved options. * * @returns {Readonly} The options. */ get options(): Readonly; /** * Whether the calendar is open. Always `false` while the native picker is in * charge. * * @returns {boolean} `true` when the popover is visible. */ get isOpen(): boolean; /** * The ISO value of the input, or an empty string when it holds no date. * * @returns {string} `YYYY-MM-DD` or `""`. */ get value(): string; /** * The value as a local `Date` at midnight. * * @returns {Date|null} The date, or `null` when the field is empty. */ get date(): Date | null; /** * Whether this instance draws its own calendar. `auto` steps aside on coarse * pointers, where the platform control is better than anything drawn here. * * @returns {boolean} `true` when the calendar replaces the native picker. */ _useCalendar(): boolean; /** * The calendar icon of the toggle, as inline SVG. * * @param {Document} doc Owner document. * @returns {SVGElement} The icon. */ _icon(doc: Document): SVGElement; /** * Builds one of the two month navigation buttons. * * @param {Document} doc Owner document. * @param {number} dir `-1` for the previous month, `1` for the next one. * @param {string} label Accessible name. * @returns {HTMLButtonElement} The button. */ _navButton(doc: Document, dir: number, label: string): HTMLButtonElement; /** * Remembers the current value of an attribute, once per element. * * @param {Element} el Element to remember. * @param {string} name Attribute name. * @returns {void} */ _remember(el: Element, name: string): void; /** * Sets a managed attribute, remembering its previous value. * * @param {Element} el Target element. * @param {string} name Attribute name. * @param {string} value Attribute value. * @returns {void} */ _set(el: Element, name: string, value: string): void; /** * Removes a listener registered with `_listen` before `destroy` runs. * * @param {EventTarget} target Event target. * @param {string} type Event type. * @param {EventListener} handler Listener. * @returns {void} */ _unlisten(target: EventTarget, type: string, handler: EventListener): void; /** * Lower bound of the field, when `min` holds a real date. * * @returns {Date|null} The first selectable day. */ get _min(): Date | null; /** * Upper bound of the field, when `max` holds a real date. * * @returns {Date|null} The last selectable day. */ get _max(): Date | null; /** * Whether a day may be selected: inside `min` and `max`, and on the grid * that `step` draws. For a date input the step counts days from `min`, or * from 1970-01-01 when there is no lower bound. * * @param {Date} date Candidate day. * @returns {boolean} `true` when the day is selectable. */ _allowed(date: Date): boolean; /** * Pulls a date inside `min` and `max`. * * @param {Date} date Candidate day. * @returns {Date} The nearest selectable day. */ _clamp(date: Date): Date; /** * Reads the input and points the calendar at the month it names: the value * when it holds one, today otherwise. Typing a date by hand and opening the * calendar therefore lands on that month. * * @returns {void} */ _syncFromInput(): void; /** * Repaints the heading, the navigation state and every cell of the grid. * The table itself is reused; only the rows are rebuilt. * * @returns {void} */ _render(): void; /** * The button painted for a given day, when the grid shows it. * * @param {Date} date The day. * @returns {HTMLButtonElement|null} Its button, or `null`. */ _dayButton(date: Date): HTMLButtonElement | null; /** * Moves the single tab stop of the grid, repaints when the month changes and * optionally takes the focus with it. Dates outside `min` and `max` are * pulled back in, so the keyboard never walks out of the allowed range. * * @param {Date} date Requested day. * @param {boolean} [moveFocus] Whether focus follows the cursor. * @returns {void} */ _setCursor(date: Date, moveFocus?: boolean): void; /** * Steps the painted month without moving the focus out of the header. * * @param {number} dir `-1` or `1`. * @returns {void} */ _shiftView(dir: number): void; /** * Places the popover below the control, or above it when it does not fit * below and does fit above. When neither side has room it stays below and * the height is capped, so the calendar is always reachable. * * @returns {void} */ _place(): void; /** * Opens the calendar. Emits the cancelable `iv:open` first. * * @param {DatepickerReason} reason Why the calendar is opening. * @returns {void} */ _openPopover(reason: DatepickerReason): void; /** * Closes the calendar. Emits the cancelable `iv:close` first. * * @param {DatepickerReason} reason Why the calendar is closing. * @returns {void} */ _closePopover(reason: DatepickerReason): void; /** * Writes a value: `iv:change` (cancelable) → `input.value` → native * `input`/`change` → repaint → `iv:changed`. A selection also closes. * * @param {string} iso New ISO value, or `""` to empty the field. * @param {DatepickerReason} reason Why the value changed. * @returns {boolean} `true` when the value was written. */ _commit(iso: string, reason: DatepickerReason): boolean; /** * Toggle click: opens or closes with the `trigger` reason. * * @returns {void} */ _toggleOpen(): void; /** * Down arrow on the toggle opens the calendar, the way a combobox does. * * @param {Event} event The keydown event. * @returns {void} */ _onToggleKeydown(event: Event): void; /** * Alt + down arrow opens the calendar from the input. A bare arrow is left * to the browser, which steps the date segment under the caret. * * @param {Event} event The keydown event. * @returns {void} */ _onInputKeydown(event: Event): void; /** * An external change — typing, autofill, a script — repaints the calendar. * * @returns {void} */ _onInputChange(): void; /** * Selects the day under the pointer. * * @param {Event} event The click event. * @returns {void} */ _onGridClick(event: Event): void; /** * The "today" button: selects today, which the footer only offers while * today is inside `min` and `max`. * * @returns {void} */ _onToday(): void; /** * Keyboard of the dialog: the grid moves the cursor, Escape closes from * anywhere inside. * * @param {Event} event The keydown event. * @returns {void} */ _onPopoverKeydown(event: Event): void; /** * Moves the cursor from a key, keeping the browser out of it. * * @param {Event} event The keydown event. * @param {Date} date Requested day. * @returns {void} */ _move(event: Event, date: Date): void; /** * A pointer outside the component closes the calendar. * * @param {Event} event The pointer event. * @returns {void} */ _onDocumentPointer(event: Event): void; /** * Opens the calendar. * * @returns {void} */ open(): void; /** * Closes the calendar. * * @param {DatepickerReason} [reason] Why it is closing. * @returns {void} */ close(reason?: DatepickerReason): void; /** * Writes a date. Values that are not ISO dates, and dates outside `min` and * `max`, are ignored; `""` empties the field. * * @param {string} iso ISO `YYYY-MM-DD` value, or `""`. * @returns {void} */ setValue(iso: string): void; /** * Empties the field. * * @returns {void} */ clear(): void; } export type DatepickerOptions = { /** * Keep the native picker: `auto` does on coarse pointers. */ native: "auto" | "on" | "off"; /** * BCP 47 tag for month and weekday names; empty means the document language. */ locale: string; /** * First column of the grid, `0` Sunday to `6` Saturday; `-1` asks the locale. */ firstDay: number; /** * Accessible name of the toggle button. */ openText: string; /** * Accessible name of the previous month button. */ prevText: string; /** * Accessible name of the next month button. */ nextText: string; /** * Label of the "today" button. */ todayText: string; /** * Label of the "clear" button. */ clearText: string; /** * Accessible name of the calendar dialog. */ dialogText: string; }; /** * Why the calendar opened or closed. */ export type DatepickerReason = "trigger" | "escape" | "external" | "api" | "select"; /** * The shape `Intl.Locale` exposes in browsers that ship week information. * Neither member is in the ES2022 type library yet. */ export type WeekInfoCarrier = { /** * Newer, method form. */ getWeekInfo?: (() => { firstDay?: number; }) | undefined; /** * Older, property form. */ weekInfo?: { firstDay?: number; } | undefined; }; import { IvComponent } from "../core/component.js";