Components / Controls
TimeField
A time of day in a form: typed loosely, or with picker chosen from columns that roll under a lens, and reported as 24-hour HH:MM.
When to useLink to "When to use"
- A time of day in a form or filter: an opening hour, a reminder, a schedule.
Use something else when:
- it is a date: DateField
- it is a duration or a count of minutes: NumberField
UsageLink to "Usage"
import { TimeField } from "@arshad-shah/cynosure";
<TimeField label="Start time" value={start} onChange={setStart} min="09:00" max="17:30" step={15} picker />The value is HH:MM (or HH:MM:SS with seconds) or null. 930, 9:30 pm and 12am are understood; anything that is not a time is kept for correcting and explained.
The pickerLink to "The picker"
picker adds a clock button at the end of the field. It opens columns for hours and minutes; the chosen time sits in the lens and applies at once.
12-hour and 24-hourLink to "12-hour and 24-hour"
By default the field follows the person's own language and region: 9:30 PM where AM and PM are used, 21:30 elsewhere. locale names the region for them; hourCycle={12} or hourCycle={24} fixes the form for everyone. The value, min, max and what a form receives stay 24-hour.
With 12-hour display the picker gains an AM/PM switch.
Limits and the day trackLink to "Limits and the day track"
The strip under the columns is the whole day: click or drag to jump to a time. The hours outside min and max are hatched, and their rows are struck through.
Without the day track, with secondsLink to "Without the day track, with seconds"
dayTrack={false} keeps the columns without the strip; seconds adds a third column.
PropsLink to "Props"
| Prop | What it does | Type | Default |
|---|---|---|---|
labelRequired | The field's visible label, and its accessible name. | ReactNode | |
labelHidden | Hide the label visually; it still names the field. | boolean | |
description | Help text under the field, read with it by screen readers. | ReactNode | |
error | Your own error; it replaces the field's own messages. | ReactNode | |
value | HH:MM (or HH:MM:SS with seconds), 24-hour whatever hourCycle shows, or null when empty. | string | null | |
defaultValue | The starting time when value is not given, in the same form. | string | null | |
onChange | A valid time (or null when cleared), when typing is finished, the arrows step it or the picker chooses it. | ((time: string | null) => void) | |
min | The earliest time allowed, in the same form. Earlier times are explained and not reported. | string | |
max | The latest time allowed, in the same form. Later times are explained and not reported. | string | |
step | Minutes between the picker's minutes, and what the arrows move the minutes by. Default 1 (Shift: ten steps). | number | 1 (Shift: ten steps) |
seconds | Include seconds. | boolean | |
hourCycle | How the time is shown: 24 as 21:30, 12 as 9:30 PM (with an AM/PM switch in the picker), or "auto" to follow the person's own language and region. The value is 24-hour either way. Default "auto". | "auto" | 12 | 24 | "auto" |
locale | The language and region hourCycle="auto" follows, such as en-GB. Default: the browser's. | string | the browser's |
picker | Add a clock button that opens a picker: columns for hours and minutes that roll under one lens. Without it the field is only typed in. | boolean | |
dayTrack | With picker: the strip of the whole day under the columns, to click or drag to a time. Default true. | boolean | `true` |
name | Form field name. The form receives the 24-hour HH:MM value (never the text being typed), however it is shown. | string | |
form | The id of a form elsewhere on the page that the field belongs to. | string | |
required | The form cannot be submitted while the field is empty. | boolean | |
disabled | Cannot be focused, changed or submitted. | boolean | |
readOnly | Shown and submitted but not changed: no typing, no arrows and no picker button. | boolean | |
size | Height and text size of the field. Default "md". | Size | "md" |
labels | Replace the built-in English text: the two messages and the picker's names. | Partial<TimeFieldLabels> | |
className | Added to the root element, for layout. | string | |
style | Layout only. | CSSProperties | |
layout | stack puts the label above the control and the description and error below it. split puts the label and description at the start and the control at the end of one row (a settings row); when the field is too narrow for both, the control moves under the text. Default "stack". | "stack" | "split" | "stack" |
width | Width of the control, so the controls of a column of fields line up: "sm", "md" and "lg" are the --cy-field-control-width-* tokens (never wider than the field), "full" is the whole field. Default: the control's own width (the whole field in the stacked layout for TextField and the other fields). | FieldWidth |
Generated from the type definitions in @arshad-shah/cynosure 0.5.0.
Behaviour and accessibilityLink to "Behaviour and accessibility"
In the field:
| Key | Does |
|---|---|
| Enter | Finish typing |
| Up / Down | Step the part the caret is in: hours, minutes (by step, onto its grid), seconds, or AM/PM; Shift: ten times as far |
In the picker (with picker):
| Key | Does |
|---|---|
| Up / Down | Roll the focused column to the next allowed row; the time changes at once |
| Home / End | First and last allowed row |
| Left / Right | The column before or after |
| Digits | Seek in the column: 1 then 4 is 14 |
| A / P | Morning or afternoon, when the time is shown as 12-hour |
| Enter | Close the picker and return to the field |
| Escape | Close the picker |
| Arrows, Page Up / Down on the day track | Move by step, or by an hour |
- Errors are the field's description and set
aria-invalid. - Limits:
minandmaxstop the arrows and the day track; typed times outside them are explained; their rows in the picker are disabled. - The picker is a dialog named by
labels.open. Each column is a listbox named bylabels.hours,labels.minutesorlabels.seconds; the day track is a slider that reads out the time. - Scrolling a column chooses the row it stops on. With reduced motion the columns jump instead of gliding.
- Right to left: hours, minutes and seconds keep their order.
TouchLink to "Touch"
The field asks for the numeric keyboard (the full one when the time is shown as 12-hour, to type AM and PM). In the picker, rows are at least 44px tall on coarse pointers and a column is rolled with a finger.
ThemingLink to "Theming"
The field follows TextField, the popup Popover, the day track Slider and the AM/PM switch SegmentedControl. The picker's own tokens:
| Token | Does |
|---|---|
--cy-time-field-row-height | Height of a row in the columns (never below the touch hit area) |
--cy-time-field-column-width | Width of one column |
--cy-time-field-lens-background | Background of the band the chosen time sits in |
--cy-time-field-night | Colour of the day track at midnight |
--cy-time-field-day | Colour of the day track at midday |
RelatedLink to "Related"
These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.