timeOptions
timeOptions({ step, min, max, value, locale }) returns the times for an OptionList of times: a time every step minutes from midnight, 30 by default, between min and max. Each one is an object with an ISO value such as 09:30:00 for an OptionList.Option, a label as formatTime writes it in locale, and the Temporal.PlainTime itself as time.
On this page
import { OptionList, timeOptions } from '@tounsoo/todae';
import { useState } from 'react';
// Office hours, one slot an hour; the list keys each option by its ISO value, not its label.
export default function TimeOptionsList({ locale }: { locale: string }) {
const [time, setTime] = useState<Temporal.PlainTime | null>(null);
const options = timeOptions({
step: 60,
min: Temporal.PlainTime.from('09:00'),
max: Temporal.PlainTime.from('17:00'),
value: time,
locale,
});
return (
<OptionList.Root
aria-label="Call times"
value={time?.toString() ?? null}
onValueChange={(next) => setTime(Temporal.PlainTime.from(next))}
style={{ maxInlineSize: '16rem' }}
>
{options.map((option) => (
<OptionList.Option key={option.value} value={option.value}>
{option.label}
</OptionList.Option>
))}
</OptionList.Root>
);
}
Picking from the list
Give the OptionList.Root the picked time's value.toString() as its value and read a pick back with Temporal.PlainTime.from. Pass the same time as value to timeOptions: a time between two steps, such as 9:07 typed into a Field.TimeInput, is then in the list in its place, and stays picked. A value outside min and max is left out.
Inside a Field.Picker beside a Field.TimeInput, the list picks the time and closes the picker. Field.TimeInput shows it.
Overnight ranges
A min later than max is an overnight range, as a native time input reads it: the list runs from min to midnight and from midnight to max, in order from midnight. With min at 22:00 and max at 02:00, it lists the times up to 2:00 AM first and 10:00 PM onwards last. For the times of one day beside a calendar, keep min before max.
Accessible composition
Todae handles: Text for each time in the locale you pass, or the runtime's own when you pass none. OptionList handles the list's keys and selection.
You supply: A name for the list, with aria-label or aria-labelledby on OptionList.Root. The same locale, min and max as the input the list sits beside.
Watch for: Keying options by their labels: two locales write a time differently, so key them by value. On a server with no locale, the labels use the server's locale.
When to use
When: A list of times to pick from, in an OptionList.Root inside a Field.Picker or on the page.
When not: A few times that aren't evenly spaced, such as the slots a shop has free → your own OptionList.Options.
Props
| Name | Type | Description |
|---|---|---|
step | number | Minutes between the times, from midnight. Default: 30 |
min | Temporal.PlainTime | The earliest time. Later than max, it makes an overnight range, from min through midnight to max, as a native time input reads it. |
max | Temporal.PlainTime | The latest time. |
value | Temporal.PlainTime | null | The picked time, listed in its place when it is in range but between two steps, such as 9:07. |
locale | string | The locale for the labels, such as "ko". The runtime's own when not set. |