timepicker-ui
4.6.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" }The picker is dismissed without committing.
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 }The displayed month changes.
range:startcallbacks.onRangeStart{ value: DateValue }The first day of a range is picked.
range:endcallbacks.onRangeEnd{ start: DateValue, end: DateValue }A range is completed.
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 reports source as one of these values. It does not fire for changes made through the API, so setValue() and open() move the view silently. With several months, year and month are the first month shown.

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.

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, or setValue() got a value that does not fit calendar.mode. 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