Events API Reference
Every event of @pglejzer/datepicker-ui, the companion of timepicker-ui, with its payload and when it fires.
Subscribe with picker.on(event, handler) or with the matching callbacks.on* option. Both receive the same payload. on() returns a function that unsubscribes. See Methods.
All events
#| Event | Callback | Payload | Fires |
|---|---|---|---|
open | callbacks.onOpen | { mode: "docked" | "modal" | "inline" } | The picker opens, or an inline picker is mounted by create(). |
close | callbacks.onClose | {} | The picker starts closing. |
cancel | callbacks.onCancel | { reason: "button" | "escape" | "outside" | "toggle" } | The picker is dismissed without committing. reason says how: the Cancel or close button, Escape, a click outside, or "toggle" when ui.toggleButton is clicked on an open picker. |
confirm | callbacks.onConfirm | { value: PickerValue | null, autoCommit: boolean } | A value is committed or cleared. autoCommit is true for closeOnSelect, commitOnChange and typed input. |
update | callbacks.onUpdate | { options: DatepickerOptions } | setOptions() applied a patch. |
error | callbacks.onError | { code, message, input?, option?, reason? } | A typed date is invalid or unavailable, calendar.validate returned a message, or an option is rejected. |
select:day | callbacks.onSelectDay | { value: DateValue, selected: boolean } | A day is picked. selected is false when a click in multiple mode removes it. |
select:month | callbacks.onSelectMonth | { year: number, month: number } | A month is chosen in the month menu. month is 1-12. |
select:year | callbacks.onSelectYear | { year: number } | A year is chosen in the year menu or the year grid. |
navigate | callbacks.onNavigate | { year, month, direction: -1 | 0 | 1, source, from, to } | The days shown change, and once when the picker opens or an inline picker is mounted. |
range:start | callbacks.onRangeStart | { value: DateValue } | The first day of a range is picked. |
range:end | callbacks.onRangeEnd | { start: DateValue, end: DateValue } | A range is completed. |
switch:view | callbacks.onSwitchView | { view: "calendar" | "input" } | The picker switches between the calendar and the text fields, for example by the switch button or setView(). |
clear | callbacks.onClear | { previousValue: PickerValue | null } | clear() or the Clear button empties the value or the draft. previousValue is the value before clearing, or the draft when only the draft was cleared. |
import { DatepickerUI } from "@pglejzer/datepicker-ui";const picker = new DatepickerUI("#date", { calendar: { mode: "range" } });picker.on("range:start", ({ value }) => {console.log("Start", value.value);});picker.on("range:end", ({ start, end }) => {console.log("Range", start.value, end.value);});picker.on("confirm", ({ value, autoCommit }) => {console.log(value, autoCommit);});picker.create();
The same handlers as options:
new DatepickerUI("#date", {callbacks: {onConfirm: ({ value }) => console.log(value),onError: ({ code, message }) => console.warn(code, message),},}).create();
Navigate sources
#navigate fires when the days shown change, and once when the picker opens or an inline picker is mounted. source tells these apart and is one of the values below. year and month are the first month shown. from and to are the first and last day shown, the same as getVisibleRange() returns. direction is -1 or 1 when the first month moves back or forward, and 0 when it stays, as on opening.
| Source | Meaning |
|---|---|
drag | The user dragged or swiped between months. |
wheel | The user paged with the mouse wheel. |
button | The user pressed a previous or next button. |
keyboard | The user navigated with the keyboard. |
menu | The user chose a month or year in a menu. |
open | The picker opened, or an inline picker was mounted. |
api | setValue(), setOptions(), goTo(), or a value the picker sets itself, such as a range preset or a typed date. |
resize | A window resize changed the months shown. |
scroll | The month list of a range picker rendered more months. |
Month and year precision
#With calendar.precision set to "month" or "year", select:day carries the picked period. value.value is YYYY-MM or YYYY. A preset click emits range:end without select:day.
Error codes
#The error payload has a code and a human readable message. Which of input, option and reason are set depends on the code.
| Code | Meaning |
|---|---|
invalid-date | Typed text is not a date, setValue() got a value that does not fit calendar.mode, or goTo() got a value that is not a date. input holds the text. |
unavailable-date | A typed date is disabled or out of range. input holds the text and reason says why. |
invalid-value | calendar.validate returned a message. |
invalid-option | An option value was rejected and replaced by its default. option holds the path, such as ui.theme. |
picker.on("error", ({ code, input, option, reason }) => {if (code === "unavailable-date") {console.log(input, reason);}if (code === "invalid-option") {console.log(option);}});
Unavailable reasons
#reason is set with unavailable-date. Its type is UnavailableReason, and the values are listed in the order the rules are checked.
| Reason | Meaning |
|---|---|
min | The date is before calendar.minDate. |
max | The date is after calendar.maxDate. |
disabled | The date is excluded by disabledDates, enabledDates or isDateDisabled. |
weekday | The weekday is in calendar.disabledWeekdays. |
range-length | The range breaks calendar.range.minLength or maxLength. |
multiple-max | The selection would exceed calendar.multiple.max. |
Validation messages
calendar.validate also sets aria-invalid and a custom validity message on the input, and adds dp-ui--error to the host. The same happens for typed text that is not a date.