Date Picker
UI Coding
Hard
60 min
accessibility
dates
keyboard
focus-management
design-system
Build a date picker: a text input for the date plus a Choose date button that opens a calendar dialog. Follow the WAI-ARIA Date Picker Dialog pattern.
The starter renders the input, the button and a static month grid.
API
export function DatePicker({
value, // 'YYYY-MM-DD' or ''
onChange, // (isoString) => void
label = 'Date',
today = new Date(), // injectable for tests
locale = 'en-US',
})
Requirements
Input
- The input shows the value as
YYYY-MM-DDwith a visible format hint. Typing a valid date and leaving the field (or pressing <kbd>Enter</kbd>) commits it. An invalid date showsEnter a date as YYYY-MM-DDand setsaria-invalid. Reject impossible dates like2026-02-30.
Dialog
- Choose date opens a dialog (
role="dialog",aria-modal="true") labelled by its month heading, for exampleMarch 2026. The button's accessible name includes the current selection:Choose date, selected date is Sunday, March 15, 2026. - The dialog has Previous year, Previous month, Next month and Next year buttons and a Cancel button. Clicking the month buttons changes the month without moving focus.
- The days are a
role="grid"table with weekday column headers (Su–Sa, with full names asabbr). Each day cell is named by its full date (Monday, March 16, 2026). The selected day hasaria-selected="true"and today hasaria-current="date". - When the dialog opens, focus goes to the selected date, or to today if nothing is selected. The grid uses a roving tabindex: only the focused day is tabbable.
Grid keyboard
| Key | Moves focus to |
|---|---|
| <kbd>←</kbd>/<kbd>→</kbd> | Previous / next day (crossing months as needed) |
| <kbd>↑</kbd>/<kbd>↓</kbd> | Same weekday in the previous / next week |
| <kbd>Home</kbd>/<kbd>End</kbd> | First (Sunday) / last (Saturday) day of the week |
| <kbd>Page Up</kbd>/<kbd>Page Down</kbd> | Same day in the previous / next month, clamped (Jan 31 → Feb 28) |
| <kbd>Shift</kbd>+<kbd>Page Up</kbd>/<kbd>Page Down</kbd> | Same day in the previous / next year |
| <kbd>Enter</kbd>/<kbd>Space</kbd> | Select the date, close, and return focus to the button |
- <kbd>Escape</kbd> or Cancel closes without changing the value and returns focus to the button. <kbd>Tab</kbd> stays inside the dialog (focus trap). Clicking outside closes it.
- All date maths uses local calendar dates. No time-zone off-by-one bugs.
Follow-up
- Add
min/maxand disabled dates. - Localise: first day of week, month names, right-to-left layouts.
- Why not just use
<input type="date">?
Spotted a mistake or a rights concern? Report an issue