Playground

Time field

Hour, minute and AM/PM segments you step with arrow keys or type into.

  • Built on native elements
  • A named group of spin buttons (Hour, Minute, AM or PM), each with aria-valuenow, aria-valuemin, aria-valuemax and aria-valuetext.
  • ArrowUp/ArrowDown step a segment (minutes by the step prop), PageUp/PageDown step further, Home/End jump to the limits, Backspace clears.
  • Typing digits fills a segment and moves to the next; A and P set AM and PM.
  • Styles in one file: 03-form-fields.css

Import

import '@monoline/css';
import {TimeField} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add time-field

Usage

import {Field, TimeField} from '@monoline/react';

<Field label="Start time">
  <TimeField defaultValue="09:30" />
</Field>

Examples

12-hour

24-hour in 15 minute steps

Props

TimeFieldProps

PropTypeDefaultDescription
valuestring–The time as 24-hour `"HH:mm"` when controlled; `""` means none. Pair with `onValueChange`.
defaultValuestring–The initial time as `"HH:mm"` when uncontrolled. Default none.
onValueChange((value: string) => void)–Called with `"HH:mm"` once hour and minute are both set, or `""` when either is cleared.
hourCycle12 | 2412Show hours 1-12 with an AM/PM segment (`12`) or 0-23 (`24`). Default 12.
stepnumber1Minutes the arrow keys move by. Default 1.
disabledboolean–Stops all input.
invalidboolean–Marks the field as having an error: sets `aria-invalid` and shows the error border.
namestring–Form field name; a hidden input submits the 24-hour `"HH:mm"` value.
aria-labelstring–Names the group when it is not inside a Field. Default "Time".

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[aria-checked="true"]03-form-fields.css
[aria-invalid="true"]03-form-fields.css
[aria-pressed="true"]03-form-fields.css
[data-disabled]time-field.css
[data-empty]time-field.css

CSS classes

ClassStylesheet
.field03-form-fields.css
.field-hint03-form-fields.css
.input03-form-fields.css
.input-bare03-form-fields.css
.input-end03-form-fields.css
.input-lg03-form-fields.css
.input-sm03-form-fields.css
.input-wrap03-form-fields.css
.muted03-form-fields.css
.seg03-form-fields.css
.seg-opt03-form-fields.css
.select03-form-fields.css
.setting03-form-fields.css
.switch03-form-fields.css
.textarea03-form-fields.css
.time-fieldtime-field.css
.time-segmenttime-field.css
.time-septime-field.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.field {
  border-color: var(--accent);
}
.field[aria-checked="true"] {
  background: var(--wash);
}

Accessibility

  • A named group of spin buttons (Hour, Minute, AM or PM), each with aria-valuenow, aria-valuemin, aria-valuemax and aria-valuetext.
  • ArrowUp/ArrowDown step a segment (minutes by the step prop), PageUp/PageDown step further, Home/End jump to the limits, Backspace clears.
  • Typing digits fills a segment and moves to the next; A and P set AM and PM.
  • ArrowLeft/ArrowRight move between segments and are mirrored in right-to-left.

Keyboard

KeysAction
ArrowUp / ArrowDownStep a segment (minutes by the step prop)
PageUp / PageDownStep further
Home / EndJump to the limits
BackspaceClears
ArrowLeft / ArrowRightMove between segments and are mirrored in right-to-left