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

Methods

Available methods on TimepickerUI instance

create()

Initialize the timepicker. Must be called before using other methods. If clock.disabledTime is invalid, create() reports it through the error event and the picker does not open.

index.ts
const picker = new TimepickerUI(input);
picker.create();

open(callback?)

Open the timepicker programmatically. It emits open, not show.

index.ts
picker.open();
picker.open(() => {
console.log('Picker opened');
});

close(update?, callback?)

Close the timepicker. close(true) commits the current selection like clicking OK: the input is updated and confirm is emitted. Nothing is committed while OK is disabled. With true, the callback runs once the picker has closed. Without it, the selection is discarded and the callback runs at once.

index.ts
picker.close();
picker.close(true, () => {
console.log('Value committed and picker closed');
});

getValue()

Get the current time. seconds is present only with clock.showSeconds, and time then includes it. degreesHours and degreesMinutes are deprecated and will be removed in 5.0. Use hour and minutes instead.

index.ts
const value = picker.getValue();
// 12h
{
hour: '10',
minutes: '30',
type: 'AM',
time: '10:30 AM',
degreesHours: 300, // deprecated
degreesMinutes: 180 // deprecated
}
// 12h with clock.showSeconds
{
hour: '10',
minutes: '30',
seconds: '15',
type: 'AM',
time: '10:30:15 AM',
degreesHours: 300, // deprecated
degreesMinutes: 180 // deprecated
}

setValue(time, updateInput?)

Set the time programmatically. Use HH:MM for 24h and H:MM AM or H:MM PM for 12h. With clock.showSeconds, HH:MM:SS and H:MM:SS AM/PM work too, and a value without seconds means :00. Without it, seconds in the string are dropped. A time in the other clock format is converted, so '2:30 PM' sets 14:30 in a 24h picker. A string that is not a time, such as '25:00' or 'abc', leaves the value as it was and emits error (callbacks.onError). In range mode (RangePlugin and range.enabled) it sets the end being edited, see getActivePart(), and with updateInput writes 'from - to' to the input.

index.ts
// clock: { type: '12h' }
picker.setValue('10:30 AM');
picker.setValue('14:30'); // converted, the input shows 02:30 PM
// clock: { type: '24h' }, input text left unchanged
picker.setValue('15:45', false);
// clock: { type: '24h', showSeconds: true }
picker.setValue('14:30:15');
// clock: { type: '12h', showSeconds: true }
picker.setValue('2:30:15 PM');

setOptions(options)

Change options at runtime. Groups you leave out keep their values. An open picker is re-rendered with the new options, and a selection that was not confirmed yet is reset to the input value. With a range, the active end (From or To) and the times already set are kept, and keeping To emits range:switch. The view (clock or keyboard) is kept too, unless the call changes ui.initialView: then an open picker switches to the new initial view and emits switch:view once, after the new view is shown. A closed picker emits nothing and opens in the new view. With a range, a time typed into the active end is committed first and emits range:change. When clock.type or clock.showSeconds changes, a valid time in the input, including a range (from - to), is rewritten in the new format without an update or confirm event. An empty or invalid input is left as it is. Options that fail validation are reported through the error event and callbacks.onError, and the old options stay. New in 4.5.0.

index.ts
picker.setOptions({
ui: {
theme: 'dark'
},
clock: {
type: '24h'
}
});

update({ options, create? }, callback?)

Deprecated and removed in 5.0. Use setOptions(options). It applies options the same way. create: true calls create() afterwards, and the callback runs only when the options were valid.

index.ts
// Deprecated
picker.update({ options: { ui: { theme: 'dark' } } });
// Use instead
picker.setOptions({ ui: { theme: 'dark' } });

setRange({ from?, to? }, updateInput?)

Set one or both ends of the range. Each time uses the setValue() format. An end you leave out keeps its value, and null or '' clears it. With range.linked (the default), clearing From while To is active switches back to From. updateInput defaults to true and writes 'from - to' to the input, with --:-- for an empty end, or nothing when both ends are empty. It works while the picker is open or closed and emits range:validation, but not range:change. When the end shown on the clock does not change, the dial and the typed fields are left alone. range:switch for the before-From limit fires only when From actually changed. On an open picker in wheel or compact-wheel mode the wheel moves to the active end quietly, without update or select:* events. In clock mode it still emits select:hour and update. Needs RangePlugin and range.enabled: without them it emits error and does nothing. A time that is not valid, or an object with neither end, emits error and leaves the range as it was.

index.ts
picker.setRange({ from: '09:00', to: '17:00' });
picker.setRange({ from: '9:00 AM', to: '5:00 PM' }, false);
picker.setRange({ from: '09:00' });
picker.setRange({ to: null });

getRange()

Read both ends of the range. Returns { from, to }, where each end is { hour, minutes, seconds?, type? } or null until it is complete. A time typed in a field counts as soon as it is applied on blur or Enter, or when you switch ends, so getRange() then returns it. Returns null, and emits error, without RangePlugin or range.enabled.

index.ts
const range = picker.getRange();
// { from: { hour: '09', minutes: '00' }, to: { hour: '17', minutes: '00' } }

setActivePart('from' | 'to')

Switch the clock or the wheel to one end of the range without moving focus. On an open picker, a time typed into the current end is committed first, and that emits range:change when the value of that end changes. Then it updates the tabs and emits range:switch. Returns true when the active end changed or was already that end. With the default range.linked, 'to' is refused until From is complete and the method returns false. In wheel and compact-wheel mode the wheel shows the end quietly, without update or select:* events. In clock mode it still emits select:hour and update. While the picker is closed, setActivePart('to') emits nothing, and the next open emits range:switch. When the end shown does not change, the dial stays as it is.

index.ts
picker.open();
const switched = picker.setActivePart('to');

getActivePart()

Read the range end being edited. Returns 'from' or 'to'. Returns null after destroy(), and also without RangePlugin or range.enabled, where it emits error like getRange().

index.ts
if (picker.getActivePart() === 'from') {
picker.setActivePart('to');
}

setRangeCaptions({ from?, to? })

Show a second line under a range tab label, for example the date of that end. The text is plain text, not HTML, and becomes part of the tab's accessible name. A tab you leave out keeps its caption, and null or '' removes it. Works open or closed, survives setOptions() and Clear, and emits no event. Needs RangePlugin and range.enabled, otherwise it emits error.

index.ts
picker.setRangeCaptions({ from: 'Wed Oct 21', to: 'Thu Oct 22' });
picker.setRangeCaptions({ to: null });

clear()

Clear the picker like the Clear button: empties the input (unless ui.clearButton is { clearInput: false }), returns the clock or wheel to its neutral time and empties both range ends. It works open or closed, and without ui.clearButton. It emits clear and update like the button, makes no screen reader announcement and never moves focus. It does not emit range:change.

index.ts
picker.clear();

setView(view) / getView()

Show the clock face or the keyboard view, for example from your own switch button. Open: switches at once and emits switch:view. Closed: the next open starts in that view, and later opens start in ui.initialView again. Focus does not move, unless it was on the part that gets hidden: then it moves to the active time field. setView() returns true when that view is shown or will be at the next open. It returns false, without an error, in wheel and compact-wheel modes and after destroy(), and returns false and emits error for a value other than 'clock' or 'keyboard'. getView() returns the view shown now, or the view the next open starts in, and null in wheel modes or after destroy().

index.ts
picker.setView('keyboard');
const view = picker.getView();

refreshDisabledTime()

Apply clock.disabledTime and clock.isTimeDisabled again without a re-render. It clears the cached isTimeDisabled results and updates the disabled marks on the clock or wheel and the OK button. It does not write to the input. Safe to call while the picker is closed.

index.ts
let lunchBreak = false;
const picker = new TimepickerUI(input, {
clock: {
type: '24h',
isTimeDisabled: ({ hour }) => lunchBreak && hour === 12
}
});
picker.create();
lunchBreak = true;
picker.refreshDisabledTime();

on(event, handler) / once(event, handler)

Subscribe to an event. Both return a function that removes the handler, and calling it more than once does nothing. With once(), the function removes the handler before it runs. The event name is typed as TimepickerEventName, so the handler gets the payload type of that event. See Events for off().

index.ts
const unsubscribe = picker.on('confirm', (data) => {
console.log(data.hour, data.minutes);
});
unsubscribe();

destroy(options?)

Destroy the picker instance and clean up.

index.ts
picker.destroy();
picker.destroy({
keepInputValue: true,
callback: () => console.log('Destroyed')
});

getElement()

Get the root wrapper element.

index.ts
const wrapper = picker.getElement();

Utilities

#

Three helpers are exported from the package root. They need no picker instance and no DOM, so they also run on the server.

parseTime(text, options?)

Parse a time the way setValue() does. options is { clockType?: '12h' | '24h', showSeconds?: boolean }, with '12h' and no seconds by default. A valid time in the other clock format is converted, so '2:30 PM' gives 14:30 in 24h. Returns a TimeValue, or null when the text is not a time. It needs no picker and no DOM.

index.ts
import { parseTime } from 'timepicker-ui';
parseTime('2:30 PM', { clockType: '24h' });
// { hour: '14', minutes: '30' }
parseTime('abc');
// null

formatTime(value, options?)

Format a TimeValue as the picker writes it to the input. It takes the same options as parseTime().

index.ts
import { formatTime } from 'timepicker-ui';
formatTime({ hour: '2', minutes: '30', type: 'PM' });
// '02:30 PM'

createDisabledTimeCheck(options)

Build a check that tells whether a time is disabled by clock.disabledTime and clock.isTimeDisabled, with the same rules as a mounted picker. options takes type, disabledTime, isTimeDisabled and showSeconds from the clock group. The edges of disabledTime.ranges use the clock format of type. The check accepts a time string or a { hour, minutes, seconds } object with hour from 0 to 23 in both clock types, and returns true for text that is not a time. Results of isTimeDisabled are cached, so build a new check after the data behind it changes. It throws the same message a picker reports through error when disabledTime is invalid. It needs no picker and no DOM.

index.ts
import { createDisabledTimeCheck } from 'timepicker-ui';
const isDisabled = createDisabledTimeCheck({
type: '24h',
disabledTime: { ranges: ['12:00 - 13:00'] }
});
isDisabled('12:30'); // true
isDisabled({ hour: 9, minutes: 0, seconds: 0 }); // false

Static Methods

#

TimepickerUI.getById(id)

Get an instance by the ID set with behavior.id. Returns undefined when no live instance has that ID.

index.ts
const picker = new TimepickerUI(input, {
behavior: { id: 'my-picker' }
});
picker.create();
const instance = TimepickerUI.getById('my-picker');

TimepickerUI.getAllInstances()

Get all live instances.

index.ts
const allPickers = TimepickerUI.getAllInstances();
console.log(allPickers.length);

TimepickerUI.destroyAll()

Destroy every live instance.

index.ts
TimepickerUI.destroyAll();

TimepickerUI.isAvailable(selector)

Check whether a selector or element exists in the document. Always false outside a browser. It does not check that the element is, or contains, an input.

index.ts
if (TimepickerUI.isAvailable('#timepicker')) {
console.log('Element exists in DOM');
}
Esc