# DatePicker

> A segmented date field with a popup calendar dialog; month and year pickers, min and max, disabled days, custom day cells, and an ISO form value.

Canonical: https://salmonui.dev/docs/calendars/date-picker

```tsx
import { DatePicker } from '@salmonui/calendars/date-picker';
import '@salmonui/calendars/date-picker/styles.css';

<DatePicker label="Arrival" defaultValue="2026-11-23" min="2026-11-05" max="2026-12-20" name="arrival" />;
```

`DatePicker` is a segmented `DateField` (one spinbutton per segment), a
calendar button and a popup dialog that holds a `Calendar` (the WAI-ARIA APG
"Date Picker Dialog" pattern).

## Value

`value` / `defaultValue` accept a `PlainDate`-like object (`{ year, month,
day }`, which `Temporal.PlainDate` satisfies), an ISO string
(`'2026-11-23'`) or a `Date` (an instant, read in `timeZone`).
`onValueChange(value, details)` receives a frozen `PlainDate` (ISO
`toString()` / `toJSON()`) or `null`, and `details` with `source`
(`'pointer' | 'keyboard' | 'touch' | 'api'`), `reason` (`'input'`,
`'calendar'`, `'time'`, `'clear'`, `'reset'`, `'strict'`) and `iso`, the
form value. It fires for user changes only: a new `value` prop never
echoes.

## The popup calendar

The calendar props of `Calendar` are forwarded to the popup: `min`, `max`
(out-of-range days are disabled and navigation stops at the bounds),
`firstDayOfWeek`, `showWeekNumbers`, `weekRule`, `weekdayFormat`,
`calendar`, `showTodayButton`; `isDateDisabled`, `renderDay` and
`getDayProps` also decide validity: a
typed or pasted disabled date makes the field `aria-invalid` and shows
`errorMessage`. `view` / `onViewChange` and `focusedValue` /
`onFocusedValueChange` control the popup's navigation.

The trigger has `aria-haspopup="dialog"` and `aria-expanded`; the popup is
in the top layer, below the field, flipped above when there is no room and
shifted onto the screen. On open, focus moves to the selected day,
else `placeholderValue`, else today; Escape closes and returns focus to
where it was.

## Format and parsing

`format` takes `Intl.DateTimeFormatOptions` or a pattern of the Data Grid
pattern grammar (`'dd/MM/yyyy'`, `'yMMMd'`); the segments follow it.
Paste on any segment parses the whole text: ISO always, then the locale's
numeric order, then `inputFormats`, then your `parse(text, { locale })`.

## Month and year pickers

`granularity="month"` shows month and year segments and opens the year view
(a grid of months); `granularity="year"` a year segment and the decade view.
The value is a `PlainDate` on day 1 (January 1), and the form value is
`YYYY-MM` (`YYYY`).

```tsx
<DatePicker label="Billing month" granularity="month" name="month" />
```

## Data Grid editor

The Data Grid's built-in `date` and `datetime` editors keep the grid's own
compact picker, so a grid never loads the full `DatePicker`. To edit a
column with `DatePicker`, pass `datePickerEditor()` from
`@salmonui/calendars/date-picker/data-grid` as the column's `renderEditor`. The
column's `editorProps` (`min`, `max`, `firstDayOfWeek`, `calendar`,
`weekRule`, `showWeekNumbers`, `weekdayFormat`, `showTodayButton`,
`showOutsideDays`, `isDateDisabled`, `format`, `inputFormats`, `hourCycle`,
`timeStep`, `minTime`, `maxTime`, `locale`, `timeZone`, `popupMode`,
`strict`, `clearable`, `placeholder`, `className`, `style`) reach the
picker under the same names, and the options
apply to every cell of the column:

```tsx
import { datePickerEditor } from '@salmonui/calendars/date-picker/data-grid';
import '@salmonui/calendars/date-picker/styles.css';

const columns = [
  { field: 'due', type: 'date', renderEditor: datePickerEditor(), editorProps: { min: '2026-01-01', firstDayOfWeek: 1, calendar: 'japanese' } },
  { field: 'at', type: 'datetime', renderEditor: datePickerEditor({ granularity: 'minute', timeZone: 'UTC' }), editorProps: { timeStep: 15 } },
];
```

Date-only cells are read as the UTC midnight the grid stores and written
back as ISO dates; date-times are written as `Date` instants in
`timeZone` (else the browser zone). Enter saves and Escape cancels the edit
as in any grid editor; inside the open popup Escape only closes the popup.
The editor is its own subpath, so it adds nothing to the Data Grid bundle.

### Live demo: DatePicker as a Data Grid editor

Live demo: https://salmonui.dev/demos/calendars/date-picker-data-grid

Date and date-time columns that edit with the public DatePicker through datePickerEditor, with min, max, first day of week, week numbers, disabled weekends and a time window passed as column editorProps.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { DataGrid, type DataGridColumn } from '@salmonui/data-grid';
import { datePickerEditor } from '@salmonui/calendars/date-picker/data-grid';

type Milestone = { id: number; name: string; due: string; review: string };

const rows: Milestone[] = [
  { id: 1, name: 'Design freeze', due: '2026-12-04', review: '2026-12-03T10:00:00Z' },
  { id: 2, name: 'Beta', due: '2027-01-15', review: '2027-01-14T14:30:00Z' },
  { id: 3, name: 'Release candidate', due: '2027-02-19', review: '2027-02-18T09:00:00Z' },
  { id: 4, name: 'General availability', due: '2027-03-05', review: '2027-03-04T16:00:00Z' },
];

const weekend = (d: { year: number; month: number; day: number }): boolean => {
  const day = new Date(Date.UTC(d.year, d.month - 1, d.day)).getUTCDay();
  return day === 0 || day === 6;
};

const columns: DataGridColumn<Milestone>[] = [
  { field: 'id', header: 'ID', primaryKey: true, width: 70, editable: false },
  { field: 'name', header: 'Milestone', width: 200 },
  {
    field: 'due',
    header: 'Due (DatePicker editor)',
    type: 'date',
    format: 'yMMMd',
    width: 210,
    renderEditor: datePickerEditor({ locale: 'en-GB' }),
    // Column editorProps reach the DatePicker under the same names as DatePicker props.
    editorProps: { min: '2026-12-01', max: '2027-06-30', firstDayOfWeek: 1, showWeekNumbers: true, isDateDisabled: weekend },
  },
  {
    field: 'review',
    header: 'Review (date and time, UTC)',
    type: 'datetime',
    format: 'yMMMdHm',
    width: 240,
    renderEditor: datePickerEditor({ granularity: 'minute', timeZone: 'UTC', hourCycle: 'h23' }),
    editorProps: { timeStep: 15, minTime: '08:00', maxTime: '18:00' },
  },
];

/** A Data Grid whose date and date-time columns edit with the public DatePicker through datePickerEditor and column editorProps. */
export default function DatePickerDataGridDemo() {
  const [last, setLast] = useState('');
  return (
    <div>
      <DataGrid<Milestone>
        aria-label="Release milestones"
        data={rows}
        columns={columns}
        height={240}
        locale="en-GB"
        timeZone="UTC"
        editing={{ mode: 'cell' }}
        onSave={(e) => setLast(e.rows().changedRecords.map((row) => JSON.stringify(row)).join(', '))}
      />
      <p className="demo-status" aria-live="polite">
        Last save: <code>{last || 'none'}</code>
      </p>
    </div>
  );
}
```

## Design notes

- The picker holds one date, so there is no method to remove a date from a
  multi-date selection; use `Calendar` for multiple dates.
- The value is a `PlainDate`, not a `Date`, so a date never shifts across
  time zones.
- The Data Grid `date` / `datetime` editors are not the `DatePicker`
  component: the `DatePicker` editor is opt-in through `datePickerEditor`,
  because loading the full picker by default would grow every Data Grid
  bundle.

## API reference

The API reference for
[`@salmonui/calendars/date-picker`](https://salmonui.dev/docs/reference/calendars/date-picker) and
[`@salmonui/calendars/date-picker/data-grid`](https://salmonui.dev/docs/reference/calendars/date-picker-data-grid)
lists every prop, method and type. Open state, focus, forms and events
shared by the pickers are in [Picker behavior and
forms](https://salmonui.dev/docs/calendars/picker-behavior); server rendering, accessibility
coverage and budgets in [SSR, accessibility and
performance](https://salmonui.dev/docs/calendars/platform).

### Live demo: DatePicker

Live demo: https://salmonui.dev/demos/calendars/calendar-date-picker

A date field with a popup calendar, bounds, disabled weekends, a month picker and the ISO value it reports.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { DatePicker, type DatePickerValueChangeDetails, type PlainDate } from '@salmonui/calendars/date-picker';

const weekend = (d: PlainDate): boolean => {
  const day = new Date(Date.UTC(d.year, d.month - 1, d.day)).getUTCDay();
  return day === 0 || day === 6;
};

export default function CalendarDatePickerDemo() {
  const [last, setLast] = useState<string>('');
  const report = (value: PlainDate | null, details: DatePickerValueChangeDetails) => setLast(`${String(value)} (${details.reason}, iso ${details.iso || "''"})`);
  return (
    <div style={{ display: 'flex', flexWrap: 'wrap', gap: 24, alignItems: 'flex-start' }}>
      <DatePicker label="Arrival" defaultValue="2026-11-23" min="2026-11-02" max="2026-12-23" isDateDisabled={weekend} errorMessage="Choose a weekday" showTodayButton clearable onValueChange={report} />
      <DatePicker label="Billing month" granularity="month" defaultValue="2026-11-01" onValueChange={report} />
      <DatePicker label="Pick only" editable={false} placeholder="Choose a date" onValueChange={report} />
      <output style={{ flexBasis: '100%' }}>Last change: {last || 'none'}</output>
    </div>
  );
}
```
