Todae

Todae

Field.TimeInput

Field.TimeInput is a text input inside a Field that people type a time of day into, in the locale's own way. Its value is a Temporal.PlainTime or null. It reads the text when it is committed, on blur, on Enter or when its form is submitted, rewrites it as formatTime writes the time and calls onValueChange with it. Typing alone calls nothing. Without onValueChange, the input is read-only. It works like Field.DateInput and takes the same props where they mean the same thing.

On this page
import { Field, Label, OptionList, timeOptions } from '@tounsoo/todae';
import { useId, useState } from 'react';

// The input and the list share one value, so a time typed between the steps, such as 9:07, shows in the list too.
export default function FieldTimeInputPicker({ locale }: { locale: string }) {
  const id = useId();
  const [time, setTime] = useState<Temporal.PlainTime | null>(null);
  return (
    <Field.Group>
      <Label.Root htmlFor={id}>Start time</Label.Root>
      <Field.Root id={id} name="start">
        <Field.TimeInput value={time} onValueChange={setTime} locale={locale} />
        <Field.Picker>
          <OptionList.Root
            aria-label="Start times"
            value={time?.toString() ?? null}
            onValueChange={(next) => setTime(Temporal.PlainTime.from(next))}
          >
            {timeOptions({ step: 15, value: time, locale }).map((option) => (
              <OptionList.Option key={option.value} value={option.value}>
                {option.label}
              </OptionList.Option>
            ))}
          </OptionList.Root>
        </Field.Picker>
      </Field.Root>
    </Field.Group>
  );
}

Here a Field.Picker beside the input opens an OptionList of times in a Popover, made with timeOptions. The input and the list share one time, so a typed time between two steps, such as 9:07, is in the list in its place. A pick closes the popover. Beside a Field.TimeInput, the picker's button shows a clock glyph and is named from the timePicker string.

Typed times

formatTime writes a time in the locale's short time style, such as 9:30 PM in en-US and 21:30 in de, and parseTime reads one typed with or without its minutes and the locale's word for the part of the day, such as 9:30 pm or 2130. formatTime and parseTime says what each writes and reads.

Field.TimeInput reads its text with parseTime and writes it with formatTime, in its locale. Empty text commits null. Text that is not a time stays as it was typed, onValueChange is not called, and the field is marked invalid once a commit has tried to read it; until the text is a time or empty, the form can't be submitted.

On its own

On its own, Field.TimeInput is the field's control, with no button. Any time can be typed, between the steps of a list too.

import { Code, Field, Label, Stack } from '@tounsoo/todae';
import { useId, useState } from 'react';

// The value changes when the text is committed: on blur, on Enter, or when the form is submitted.
export default function FieldTimeInputAlone({ locale }: { locale: string }) {
  const id = useId();
  const [opens, setOpens] = useState<Temporal.PlainTime | null>(null);
  return (
    <Stack.Root gap="condensed">
      <Field.Group>
        <Label.Root htmlFor={id}>Opens at</Label.Root>
        <Field.Root id={id} name="opens">
          <Field.TimeInput value={opens} onValueChange={setOpens} locale={locale} />
        </Field.Root>
      </Field.Group>
      <Code.Root>{opens ? opens.toString() : 'null'}</Code.Root>
    </Stack.Root>
  );
}
null

The time goes with a form in a native <input type="time"> beside the text input, under the field's name, or the input's own name, the way HTML writes a time, such as 09:30, or 09:30:15 with seconds. The text input sends nothing, and the native input is hidden from sight and from assistive technology and is not a tab stop. required makes a form with no time invalid, and min and max make a form with an earlier or later time invalid. A min later than max is an overnight range, as in a native time input: the form accepts the times from min to midnight and from midnight to max, and timeOptions lists the same; a time in the gap between max and min is out of range, and the message names min, such as "Enter 10:00 PM or later". The browser checks all three on the native input, and text that is not a time blocks the submit too. Inside a Form.Root, a problem shows in the field's ErrorMessage.Root in the page's language, with min or max written as formatTime writes a time, and focus goes to the text input. Outside a Form.Root the browser blocks the submit and focus lands on the text input too.

Temporal

Todae ships no Temporal polyfill. The input's value, formatTime, parseTime and timeOptions use Temporal.PlainTime, so where the browser has no Temporal, the app loads a polyfill (see Native platform). Todae's types need TypeScript 6 or later, which declares Temporal.

Accessible composition

Todae handles: The text input takes id, aria-label, aria-labelledby and aria-describedby from Field.Root, and native disabled and aria-invalid from the Root or a surrounding Fieldset. It gets aria-invalid once a commit can't read its text, and aria-required with required. Beside a Field.Picker, the input stays the field's control, and the picker's button is named from the timePicker string in StringsProvider and shows the clock glyph from GlyphsProvider. The popover is a role="dialog" named from the same string. Open, focus goes to the list, where arrow keys, Home and End move through the times and Enter or Space picks one.

You supply: A name, through a Label whose htmlFor is the Root id or aria-label / aria-labelledby on the Root. A name for the list of times, with aria-label on OptionList.Root. The same time for the input and the list, and the same locale, min and max for the input and timeOptions.

Watch for: With a value, the server writes the time in its own locale, so pass locale when the input renders on a server, and to timeOptions too. A list of times keyed by its labels: two locales write a time differently, so key options by their value.

When to use

Field.TimeInput

When: A time of day, such as opening hours, a daily reminder or when a meeting starts.

When not: A date and time in one value → Field.DateTimeInput. A duration → Field.NumberInput with an Affix for the unit.

Props

Field.TimeInput props
NameTypeDescription
value RequiredTemporal.PlainTime | null

The time, or null for none.

onValueChange(value: Temporal.PlainTime | null) => void

Fires on a person's commit, on blur, Enter or a submit of the form, with the time the text says, read with parseTime, or null for an empty text. Typing alone fires nothing, and text that is no time stays, invalid. Without it the input is read-only.

minTemporal.PlainTime

The earliest time. A form with an earlier time is invalid. Later than max, it makes an overnight range, from min through midnight to max.

maxTemporal.PlainTime

The latest time. A form with a later time is invalid.

requiredboolean

A form with no time is invalid.

localestring

The locale the text is read and written in, such as "ko". The browser's own when not set, so pass it when the input renders on a server.

placeholderstring

Native placeholder.