Time picker

A time of day, typed or picked from a list. What posts is always HH:MM, 24 hours; what shows may be 12-hour.

Form control. It takes input in a form, so it is drawn on the Every input type block, beside every other control, as it starts and with an error where it has one.

Anatomy

Start
┌──────────────────────────┬──┐
│ 9:30 AM               ✕  │◷ │   ← text field (combobox), clear, clock
└──────────────────────────┴──┘
┌─────────────────────────────┐
│ 9:15 AM                     │   ← listbox of times at `step`
│ 9:30 AM                  ✓  │
│ 9:45 AM                     │
└─────────────────────────────┘
Office hours.
  • The field is ds::input's, from the field recipe, with room at the trailing edge for the clock and, with clearable, the ✕. Visually paired with ds::date-picker: the same label, height, border, radius and a trailing icon in the same place, so the two sit side by side as one row.
  • The list is the listbox recipe ds::combobox draws with (partials/listbox.php): the same panel, the same options, the same arrow-key handler, placed by x-ds-anchor with the same absolute fallback. Every time from min (midnight without one) to max (23:59) at step, rendered by PHP. Under a 5-minute step the list counts in the smallest multiple of the step that is at least 5; typing still takes every allowed minute.
  • The hidden input posts. Rendered with no name; the script gives it the field's name when it takes the field over.
  • size: md or sm, as ds::input.

Two modes

Without the scriptWith it
The fieldA native <input type="time"> with the same name, min, max and step (in seconds)A text field, role="combobox"
PostsThe native field, HH:MMThe hidden input, HH:MM
PickingThe browser's own pickerThe list
Bounds and stepThe browser's own validationThe same, by setCustomValidity
ShownThe browser's own formatformat: 24 ("09:30") or 12 ("9:30 AM")

Every attribute the upgrade changes (type, name, role, the ARIA state, placeholder) is also bound to the behaviour's dsReady, so a Livewire morph, which rewrites attributes to the server's, keeps the upgraded field. The bindings read $data.dsReady, which is undefined and harmless where the script is missing.

Typing

Forgiving: 9, 930, 0930, 9:30, 9.30, 9 30, 9h30, 9:30:00, 9.30pm, 9:30 P.M., 21:30 all read; 12am is midnight, 12pm noon.

The text is settled on commit: Enter, leaving the component, a press on an option, or a change (the ✕ sends one).

Committed textShownPostsMarked
A time inside the bounds, on a stepAs the list shows itHH:MMvalid
A time outside min..max, or off the stepThe reader's text, keptHH:MMaria-invalid, and a validity message, so the form does not submit
Not a timeThe reader's text, keptnothingaria-invalid, and a validity message
Empty—nothingvalid (required is the browser's)

Never quietly turned into another time: "7pm" in an office-hours field stays "7pm", marked, rather than becoming 6:00 PM.

Behaviour (dsTimePicker)

InputResult
Click in the fieldOpens or closes the list
TypingOpens the list and scrolls it to the nearest time
ArrowDown, Alt+ArrowDownOpens the list, focus to the chosen time or the nearest to what is typed
ArrowUp (list open)Focus into the list
Arrows, Home, End in the listMove, wrapping (the listbox recipe)
Enter or Space on an option, clickPicks it, closes, focus back to the field
EscapeCloses the list, focus to the field; stopped only when the list was open
A character typed in the listGoes to the field
Enter in the fieldCommits; with the list open, closes it and does not submit
Leaving the componentCommits and closes
Host sets the valueShown, not announced; a value that is not a time is cleared and handed back

A committed change writes dsValue and dispatches input and change from the hidden input, bubbling.

Accessibility

RoleWithout the script, the native time field. With it, combobox with aria-expanded, aria-controls the listbox, aria-autocomplete="none" (the list is not filtered)
NameThe label, a <label for>; the list is labelled by it too
Optionsrole="option", aria-selected on the chosen time, a ✓ drawn from it
KeyboardAs the table above, the ARIA combobox pattern ds::combobox follows: focus moves into the list
FocusThe field's ring is the field recipe's; an option's is drawn inward, as the list scrolls
Errorserror sets aria-invalid and describes the field; it stays marked after the reader fixes the time, because the server's message is still there

Out of scope

  • Date and time in one field. Put a date picker and a time picker side by side; see the docs page.
  • Arbitrary disabled slots. min and max only.
  • A range across midnight. min must be earlier than max.

Do not

  • Do not post the shown text. It is 12-hour and in English; HH:MM is what the server gets, always.
  • Do not use it for a duration. "1:30" of work is not half past one.
  • Do not trust min, max and step. Validate on the server.

From the package repo: edit this page on GitHub.

Was this page helpful?