# DatePicker

> A date field that opens a calendar, plus DateRangePicker with presets like “Last 7 days”.

Status: beta. Category: Forms.

```ts
import { DatePicker, DateRangePicker } from '@glyphui/ui'
```

Exports: `DatePicker`, `DateRangePicker`, `datePickerStyles`.

## Notes

- Values are Date objects (or { from, to } for ranges), never strings. Pass name to submit ISO dates with a native form.
- Format the trigger with any date-fns pattern through the format prop.

## DatePicker props

| Prop | Type | Default | Description |
|---|---|---|---|
| `aria-describedby` | `string` |  |  |
| `aria-invalid` | `boolean` |  |  |
| `aria-label` | `string` |  |  |
| `className` | `string` |  |  |
| `clearable` | `boolean` |  | Show a button that clears the value. |
| `contentClassName` | `string` |  |  |
| `defaultValue` | `Date` |  |  |
| `disabled` | `boolean` |  |  |
| `disabledDates` | `Matcher \| Matcher[]` |  | Extra disabled dates, in react-day-picker matcher form. |
| `format` | `string` |  | date-fns format string for the trigger. Default `"MMM d, yyyy"`. |
| `id` | `string` |  |  |
| `maxDate` | `Date` |  | Latest selectable date. |
| `minDate` | `Date` |  | Earliest selectable date. |
| `name` | `string` |  | Submits the ISO date (yyyy-MM-dd) with a native form. |
| `onValueChange` | `((date: Date) => void)` |  |  |
| `placeholder` | `ReactNode` |  |  |
| `required` | `boolean` |  |  |
| `size` | `"sm" \| "md" \| "lg"` |  |  |
| `value` | `Date` |  |  |

## Example: Date

In a Field, with a minimum date and a clear button.

```tsx
import { DatePicker, Field } from '@glyphui/ui'
import { useState } from 'react'

export default function Example() {
  const [date, setDate] = useState<Date | undefined>()
  return (
    <Field label="Launch date" description="Weekdays from today onward." className="w-72">
      <DatePicker
        value={date}
        onValueChange={setDate}
        minDate={new Date()}
        disabledDates={{ dayOfWeek: [0, 6] }}
        clearable
      />
    </Field>
  )
}
```

## Example: Date range with presets

```tsx
import { type DateRange, DateRangePicker, Field } from '@glyphui/ui'
import { useState } from 'react'

const daysAgo = (n: number) => new Date(Date.now() - n * 86_400_000)
const startOfMonth = () => new Date(new Date().getFullYear(), new Date().getMonth(), 1)

export default function Example() {
  const [range, setRange] = useState<DateRange | undefined>({ from: daysAgo(6), to: new Date() })
  return (
    <Field label="Report period" className="w-80">
      <DateRangePicker
        value={range}
        onValueChange={setRange}
        maxDate={new Date()}
        presets={[
          { label: 'Today', range: () => ({ from: new Date(), to: new Date() }) },
          { label: 'Last 7 days', range: () => ({ from: daysAgo(6), to: new Date() }) },
          { label: 'Last 30 days', range: () => ({ from: daysAgo(29), to: new Date() }) },
          { label: 'Month to date', range: () => ({ from: startOfMonth(), to: new Date() }) },
        ]}
      />
    </Field>
  )
}
```

## Example: In a form

Value and onValueChange map straight to FormField.

```tsx
import { DatePicker } from '@glyphui/ui'
import { Form, FormField, FormSubmit, useForm } from '@glyphui/ui/form'
import { z } from 'zod'

const schema = z.object({
  dueDate: z.date('Choose a due date'),
})

export default function Example() {
  const form = useForm({ schema })
  return (
    <Form form={form} onSubmit={() => {}} className="grid w-72 gap-4">
      <FormField control={form.control} name="dueDate" label="Due date" required>
        {(field) => <DatePicker value={field.value} onValueChange={field.onChange} />}
      </FormField>
      <FormSubmit className="justify-self-start">Save</FormSubmit>
    </Form>
  )
}
```

Live page: https://glyphui.xyz/components/date-picker
