timepicker-ui
4.7.0GitHub
timepicker-ui
API Reference / Events

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

#
EventCallbackPayloadFires
opencallbacks.onOpen{ mode: "docked" | "modal" | "inline" }The picker opens, or an inline picker is mounted by create().
closecallbacks.onClose{}The picker starts closing.
cancelcallbacks.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.
confirmcallbacks.onConfirm{ value: PickerValue | null, autoCommit: boolean }A value is committed or cleared. autoCommit is true for closeOnSelect, commitOnChange and typed input.
updatecallbacks.onUpdate{ options: DatepickerOptions }setOptions() applied a patch.
errorcallbacks.onError{ code, message, input?, option?, reason? }A typed date is invalid or unavailable, calendar.validate returned a message, or an option is rejected.
select:daycallbacks.onSelectDay{ value: DateValue, selected: boolean }A day is picked. selected is false when a click in multiple mode removes it.
select:monthcallbacks.onSelectMonth{ year: number, month: number }A month is chosen in the month menu. month is 1-12.
select:yearcallbacks.onSelectYear{ year: number }A year is chosen in the year menu or the year grid.
navigatecallbacks.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:startcallbacks.onRangeStart{ value: DateValue }The first day of a range is picked.
range:endcallbacks.onRangeEnd{ start: DateValue, end: DateValue }A range is completed.
switch:viewcallbacks.onSwitchView{ view: "calendar" | "input" }The picker switches between the calendar and the text fields, for example by the switch button or setView().
clearcallbacks.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.
index.ts
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:

index.ts
new DatepickerUI("#date", {
callbacks: {
onConfirm: ({ value }) => console.log(value),
onError: ({ code, message }) => console.warn(code, message),
},
}).create();
#

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.

SourceMeaning
dragThe user dragged or swiped between months.
wheelThe user paged with the mouse wheel.
buttonThe user pressed a previous or next button.
keyboardThe user navigated with the keyboard.
menuThe user chose a month or year in a menu.
openThe picker opened, or an inline picker was mounted.
apisetValue(), setOptions(), goTo(), or a value the picker sets itself, such as a range preset or a typed date.
resizeA window resize changed the months shown.
scrollThe 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.

CodeMeaning
invalid-dateTyped 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-dateA typed date is disabled or out of range. input holds the text and reason says why.
invalid-valuecalendar.validate returned a message.
invalid-optionAn option value was rejected and replaced by its default. option holds the path, such as ui.theme.
index.ts
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.

ReasonMeaning
minThe date is before calendar.minDate.
maxThe date is after calendar.maxDate.
disabledThe date is excluded by disabledDates, enabledDates or isDateDisabled.
weekdayThe weekday is in calendar.disabledWeekdays.
range-lengthThe range breaks calendar.range.minLength or maxLength.
multiple-maxThe selection would exceed calendar.multiple.max.

Validation messages

A message from 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.
Esc