Time field
Hours and minutes in segments, on a 12- or 24-hour clock by locale.
Demo
Usage
import { Field, TimeField } from '@tuttn/slant'
// Uncontrolled, in a Field: the label names the group, and a name submits "HH:mm".
<Field>
<Field.Label>Start</Field.Label>
<TimeField defaultValue="14:30" name="start" />
</Field>
// Controlled: the value is a 24-hour "HH:mm" string, or null until the hour,
// the minute and (on a 12-hour clock) the day period are all filled.
const [start, setStart] = useState<string | null>(null)
<TimeField value={start} onValueChange={setStart} label="Start" />
// The clock and the segments' order come from the locale; hourCycle overrides the clock.
<TimeField locale="en-GB" minuteStep={15} label="Reminder" />
<TimeField locale="en-US" hourCycle={24} label="Departure" />
// Limits: a time outside them is still reported, and marks the field invalid.
const late = start !== null && (start < '09:00' || start > '17:00')
<Field invalid={late}>
<Field.Label>Delivery</Field.Label>
<TimeField value={start} onValueChange={setStart} min="09:00" max="17:00" />
<Field.Error>Pick a time between 9 AM and 5 PM.</Field.Error>
</Field>Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | null | — | The time as a 24-hour `"HH:mm"` string (`"14:30"`), or null while it is incomplete. Pass it to control the field; leave it out to let the field keep its own. |
defaultValue | string | null | null | The time an uncontrolled field starts at, as `"HH:mm"`. |
onValueChange | (value: string | null) => void | — | Called with the new `"HH:mm"` time once the hour, the minute and (on a 12-hour clock) the day period are all filled, and with null when one of them is cleared. |
locale | string | the document's lang, else 'en' | The locale that sets the segments' order, their separators, the day-period names and the default clock. |
hourCycle | 12 | 24 | the locale's | A 12-hour clock (hours 1 to 12 and a day period) or a 24-hour one (hours 0 to 23). |
minuteStep | number | 1 | How many minutes the arrow keys step by. Stepping snaps to multiples of it, and End goes to the last one in the hour. |
min | string | — | The earliest valid time, as `"HH:mm"`. An earlier time is still reported, and marks the field invalid. With `max` before `min`, the range runs across midnight. |
max | string | — | The latest valid time, as `"HH:mm"`. A later time is still reported, and marks the field invalid. |
label | string | 'Time' | The group's accessible name, unless a Field's label or `aria-labelledby` names it. From the root's `messages.time`. |
name | string | — | Submits the time with a form: a hidden input with this name carries `"HH:mm"`, or `''` while the time is incomplete. |
form | string | — | The id of the form the hidden input belongs to, when the field sits outside it. |
size | 'sm' | 'md' | 'lg' | 'md' | Height on the control scale: 28, 34 or 40px at regular density. |
invalid | boolean | — | Marks every segment invalid (aria-invalid) and draws a danger border. Inside a Field, the field's `invalid` does this. |
disabled | boolean | — | Dims the field and takes its segments out of the tab order. A disabled time is not submitted with its form. |
readOnly | boolean | — | Keeps the segments focusable and readable, but no key changes them. |
required | boolean | — | Marks every segment required (aria-required). |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref reach the root: the role="group" element drawn as the field. |
Keyboard
Each segment is a tab stop, so Tab and Shift+Tab move through them; Left and Right move between them too, in the order they sit on screen, and stop at the ends. Digits type into the focused segment and move on once no further digit could fit: on a 12-hour clock "1" waits for a second digit (10 to 12) and "3" moves on at once, on a 24-hour clock "2" waits and "3" moves on, and a minute takes two digits, or one from 6 up. A pair too big for the segment starts again from the last digit, and on a 12-hour clock 0 reads as 12. Up and Down step the segment and wrap (minutes by minuteStep, snapping to its multiples); in a blank segment the first step takes the current time's. Home and End set the lowest and highest. On the day period, the first letter of either name (A or P in English) sets it and Up or Down switches it. Backspace and Delete clear the segment, and Backspace in a blank segment steps back to the one before. A press on the field around the segments focuses the nearest one.
Accessibility
The field is a role="group" named by a Field's label through aria-labelledby, else by label (messages.time by default), and described by the field's description and error. Each segment is a spinbutton named from messages.hour, messages.minute and messages.dayPeriod, with its range, its number as aria-valuenow, and aria-valuetext as shown ("02", "PM"), or messages.blank while it is empty. Invalid, required, read-only and disabled reach every segment as ARIA. The separators are hidden from assistive technology.
Locale and direction
The segments' order, their separators and the day-period names come from Intl.DateTimeFormat in locale (the document's lang by default): en-US reads "02:30 PM", en-GB "14:30", and ko-KR puts the day period first. The digits are always Latin. Under <Slant dir="rtl"> the field sits at the inline start, but the time keeps its own left-to-right order, and Left and Right still follow the screen.
Value and forms
The value changes only when every segment is filled, as a 24-hour "HH:mm" string; clearing any segment reports null, and the other segments keep what was typed. A controlled value updates the segments, and null clears them. With min and max, a time outside them is still reported but marks the field invalid (data-invalid, aria-invalid on the segments); a max before the min is a range across midnight. With a name, a hidden input carries the time, or '' while it is incomplete; a disabled field submits nothing. Read-only, the segments stay focusable and readable but no key changes them.