Date and time picker
A recipe, not a package namespace. A date and a time in one value are typed into a Field.DateTimeInput, and picked in a Field.Picker that holds a Calendar beside an OptionList of times. There is no DateTimePicker.* export.
On this page
Pick a day and a time in either order, or type both into the input.
import { Calendar, Field, Label, OptionList, timeOptions } from '@tounsoo/todae';
import { useId, useState } from 'react';
import styles from './DateTimePicker.module.css';
// A day or a time picked while the other half is missing, held until the other is picked too.
type Halves = { date: Temporal.PlainDate | null; time: Temporal.PlainTime | null };
const NONE: Halves = { date: null, time: null };
export default function DateTimePicker({ locale }: { locale: string }) {
const id = useId();
const [starts, setStarts] = useState<Temporal.PlainDateTime | null>(null);
const [held, setHeld] = useState<Halves>(NONE);
const date = starts?.toPlainDate() ?? held.date;
const time = starts?.toPlainTime() ?? held.time;
// A pick keeps the other half, and makes the value, and closes the picker, once both are set.
function pick(next: Halves, close: () => void) {
const day = next.date ?? date;
const at = next.time ?? time;
if (day && at) {
setStarts(day.toPlainDateTime(at));
setHeld(NONE);
close();
} else {
setHeld({ date: day, time: at });
}
}
return (
<Field.Group>
<Label.Root htmlFor={id}>Meeting starts</Label.Root>
<Field.Root id={id} name="starts">
<Field.DateTimeInput value={starts} onValueChange={setStarts} locale={locale} />
{/* A half picked and then left is dropped when the picker closes. */}
<Field.Picker
closeOnPick={false}
onOpenChange={(open) => {
if (!open) setHeld(NONE);
}}
>
{({ close }) => (
<div className={styles.panel}>
<Calendar.Root
aria-label="Day"
value={date}
onValueChange={(day) => pick({ date: day, time: null }, close)}
locale={locale}
>
<Calendar.Header />
<Calendar.Grid />
</Calendar.Root>
<div className={styles.times}>
<OptionList.Root
aria-label="Times"
value={time?.toString() ?? null}
onValueChange={(next) => pick({ date: null, time: Temporal.PlainTime.from(next) }, close)}
>
{timeOptions({ step: 15, value: time, locale }).map((option) => (
<OptionList.Option key={option.value} value={option.value}>
{option.label}
</OptionList.Option>
))}
</OptionList.Root>
</div>
</div>
)}
</Field.Picker>
</Field.Root>
</Field.Group>
);
}
/* The calendar and the times side by side, the times as tall as the calendar. */
.panel {
display: flex;
align-items: flex-start;
gap: var(--todae-space-stack-gap);
}
/* The times scroll in their own box. The padding and scroll padding leave room
for the list's focus ring, which the box would otherwise cut. */
.times {
--_times-height: calc(6 * var(--todae-space-12));
align-self: stretch;
max-block-size: var(--_times-height);
overflow-y: auto;
padding: calc(var(--todae-focus-ring-width) + var(--todae-focus-ring-offset));
scroll-padding: calc(var(--todae-focus-ring-width) + var(--todae-focus-ring-offset));
}
/* On a narrow screen the times go under the calendar, in a shorter box. */
@media (width < 32rem) {
.panel {
flex-direction: column;
align-items: stretch;
}
.times {
--_times-height: calc(3 * var(--todae-space-12));
}
}
Picking both halves
The picker has closeOnPick={false}, so a pick in the calendar or the list doesn't close it, and its child is a function that gets close. Picking a day keeps the time already set, and picking a time keeps the day. Once both are set, the pick makes the value, day.toPlainDateTime(time), and calls close(). A half picked before the other is held beside the value until the other is picked, and dropped when the picker closes, so a day picked and then left changes nothing.
timeOptions({ step: 15, value: time, locale }) lists the times, with a typed time between two steps, such as 9:07, in its place. The list gets time.toString() as its value, and a pick is read back with Temporal.PlainTime.from.
Layout
The calendar and the list sit side by side with the space.stack.gap token between them. The list scrolls in its own box, at most six times space.12 tall, with padding and scroll padding that leave room for its focus ring. On a narrow screen the list goes under the calendar, in a box half as tall. The stylesheet is the example's own.
Bounds
min and max on the Field.DateTimeInput check the value in a form. To keep picks in range too, pass the dates of min and max to the calendar, and on the first and last of those days pass their times to timeOptions. The calendar's isDateDisabled does not check a typed value; set customValidity on the Field.Root for that.
Accessible composition
Todae handles: The input is the field's control, and the picker's button is named from the dateTimePicker string in StringsProvider. The popover is a role="dialog" with that name. Open, focus goes to the calendar's day; Tab moves to the list of times. The calendar is keyed as on Calendar, and in the list arrow keys, Home and End move and Enter or Space picks. Escape closes the popover and returns focus to where it was before it opened.
You supply: Names for the calendar and the list, with aria-label. The value, passed to the input, and its halves, passed to the calendar and the list.
Watch for: Closing on the first pick: the other half would be lost, and people who pick the time first would never reach the day. A day picked while the list shows a time out of range for it: check the pair before you make the value.
When to use
When: A date and a time people pick from a month and a list, such as when a meeting starts.
When not: A date alone → Field.DateInput with a Calendar. A time alone → Field.TimeInput with a list of times. A date and time people type → Field.DateTimeInput on its own.