Time picker
For when only the time matters: store hours, delivery slot, shift. It's the
date picker's columns without the calendar, with the same mask, the same keyboard and the same
Apply button. The form gets 14:30, exactly what an <input type="time"> would post.
Examples
Under each field is the value the server receives.
Limits
data-min="08:00" and data-max="18:00": anything outside shows disabled in the columns.
Every 15 minutes
data-minute-step="15" leaves only 00, 15, 30 and 45 in the column.
With seconds
data-seconds="true" adds the third column, and the value comes out as 14:30:15.
Now button
data-now="true" adds "Agora" (now) to the footer: it applies the current time, on the minute step, and closes.
Without Apply
data-auto-apply="true": every click in a column counts right away.
24-hour clock
data-locale="pt-BR" shows 14:30; on an English page the field shows 02:30 PM. The POST is 14:30 either way.
With an error
aria-invalid="true" on the field — Django 5 already writes it.
Closing must be after opening.
How to use
Mark the <input> and it sets itself up on load and on every htmx:afterSwap.
<input type="text" name="opening" data-tuc-timepicker>
<input type="text" name="shift" data-tuc-timepicker data-mode="range">
<input type="text" name="slot" data-tuc-timepicker data-min="08:00" data-max="18:00" data-minute-step="15">| Attribute | Default | What for |
|---|---|---|
data-mode | single | range for start and end side by side |
data-min data-max | — | Limits, as hh:mm |
data-minute-step | 5 | Step of the minutes column; 1 lists them all |
data-seconds | false | Seconds column |
data-auto-apply | false | true drops Apply: every click counts |
data-now | false | true adds the Now button, which applies the current time and closes — disabled when now is outside min and max |
data-locale | <html lang> | 12- or 24-hour clock |
data-iso-name | the name | name of the hidden field with the value |
data-placement | bottom-center | Side and alignment of the panel |
data-native | false | true or auto: the system's time picker |
In a Django form
class StoreForm(forms.ModelForm):
class Meta:
model = Store
fields = ["opens_at", "closes_at"]
widgets = {
"opens_at": forms.TextInput(attrs={"data-tuc-timepicker": ""}),
"closes_at": forms.TextInput(attrs={"data-tuc-timepicker": "", "data-max": "23:00"}),
}Don't use type="time" on the widget: the browser's picker would open on top of ours. TimeField reads
14:30 and 14:30:15 with no setup.
In JavaScript
const tp = new Tucano.TimePicker('#opening', {
mode: 'range',
min: '08:00',
max: '18:00',
minuteStep: 15,
onChange: (value, { iso }) => console.log(value, iso),
});
tp.setValue({ start: '09:00', end: '12:30' });
tp.getValue(); // { start: '09:00', end: '12:30' } — in single mode, '14:30' or null
tp.clear();
tp.destroy();Value and form
As in the date picker, an <input type="hidden"> next to it carries the name and the
value in 24-hour time. The visible field may show 02:30 PM; the server always gets 14:30.
POST opening = 14:30
POST exact = 14:30:15 # with data-seconds
POST shift = 09:00,17:30 # start and end, comma-separatedThe value is text, not a Date: a time without a day isn't an instant, and a Date would force us to
make up a day and a time zone. getValue() and e.detail.value return the same '14:30' as the POST.
Typing
The mask is hh:mm, and the colon goes in on its own. It also reads 1430, 14h30,
14h and 2:30 pm.
The value is confirmed on leaving the field, or with Enter while the panel is open. A time outside min
and max isn't accepted, nor pulled to the limit: the field goes back to the previous value. In the columns, though,
picking 6 PM with 30 minutes and a max of 18:00 lands on 18:00. In a range, the end you didn't touch follows the other one
instead of leaving the start after the end.
Keyboard and accessibility
The same as the date picker's time columns. Arriving by Tab doesn't open the panel.
| Key | Where | Action |
|---|---|---|
↓ | Field | Opens the panel with focus on the hours column |
Space | Empty field | Opens the panel |
Enter | Field, panel open | Confirms what was typed and closes |
↑ ↓ | Column | Previous and next value, skipping disabled ones |
Home End | Column | First and last value that counts |
Enter Space | Column | Picks the value |
Tab | Inside the panel | From one column to the next and to the buttons; each column is a single stop |
Esc | Field or panel | Closes, discards what wasn't applied and returns focus to the field |
API
Generated from the code on every build — if something is not here, it does not exist.
[data-tuc-timepicker]new Tucano.TimePicker(alvo, opcoes)data-auto-apply data-iso-name data-locale data-max data-min data-minute-step data-mode data-native data-now data-placement data-secondsgetValue setValue clear open close toggle destroytucano:changeOptions
The notes in this table come from comments in the source code, which are written in Portuguese.
| Option | Default | What for |
|---|---|---|
mode | 'single' | 'single' | 'range' (inicio e fim lado a lado) |
seconds | false | |
minuteStep | 5 | |
locale | undefined | default: locale do documento/navegador; decide 24 h ou AM/PM |
min | null | '08:00': as horas antes ficam desativadas nas colunas |
max | null | '18:00' |
autoApply | false | true: cada clique numa coluna ja vale, sem o Aplicar |
clearable | true | |
now | false | botao "Agora" no rodape: aplica a hora atual e fecha. Opt-in |
placement | 'bottom-center' | |
appendTo | undefined | |
isoName | undefined | name do input hidden com o valor HH:MM |
native | false | true ou 'auto': o seletor de hora do sistema no celular |
onChange | null | |
onOpen | null | |
onClose | null |