

<InteractiveDemo
  name="date-field"
  controls="[
  {
    type: &#x22;string&#x22;,
    name: &#x22;label&#x22;,
    defaultValue: &#x22;Date&#x22;,
  },
  {
    type: &#x22;string&#x22;,
    name: &#x22;description&#x22;,
    defaultValue: &#x22;&#x22;,
    placeholder: &#x22;Helper text&#x22;,
  },
  {
    type: &#x22;enum&#x22;,
    name: &#x22;granularity&#x22;,
    options: [&#x22;day&#x22;, &#x22;hour&#x22;, &#x22;minute&#x22;, &#x22;second&#x22;],
    defaultValue: &#x22;day&#x22;,
  },
  {
    type: &#x22;enum&#x22;,
    name: &#x22;size&#x22;,
    options: [&#x22;sm&#x22;, &#x22;md&#x22;, &#x22;lg&#x22;],
    defaultValue: &#x22;md&#x22;,
  },
  {
    type: &#x22;boolean&#x22;,
    name: &#x22;isDisabled&#x22;,
    defaultValue: false,
  },
  {
    type: &#x22;boolean&#x22;,
    name: &#x22;isReadOnly&#x22;,
    defaultValue: false,
  },
  {
    type: &#x22;boolean&#x22;,
    name: &#x22;isInvalid&#x22;,
    defaultValue: false,
  },
  {
    type: &#x22;boolean&#x22;,
    name: &#x22;isRequired&#x22;,
    defaultValue: false,
  },
]"
/>

## Installation [#installation]

<InstallPrerequisite>
  Before adding components, init your project with your design system from the [Studio](/studio).
</InstallPrerequisite>

<CodeBlockTabs defaultValue="npm" groupId="package-manager">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="npm">
      npm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="pnpm">
      pnpm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="yarn">
      yarn
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="bun">
      bun
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="npm">
    ```bash
    npx shadcn@latest add @dotui/date-field
    ```
  </CodeBlockTab>

  <CodeBlockTab value="pnpm">
    ```bash
    pnpm dlx shadcn@latest add @dotui/date-field
    ```
  </CodeBlockTab>

  <CodeBlockTab value="yarn">
    ```bash
    yarn dlx shadcn@latest add @dotui/date-field
    ```
  </CodeBlockTab>

  <CodeBlockTab value="bun">
    ```bash
    bunx shadcn@latest add @dotui/date-field
    ```
  </CodeBlockTab>
</CodeBlockTabs>

## Usage [#usage]

```tsx
import { DateField } from "@/components/ui/date-field"
import { DateInput } from "@/components/ui/input"
import { Label } from "@/components/ui/field"
```

```tsx
<DateField>
  <Label>Date</Label>
  <DateInput />
</DateField>
```

## Anatomy [#anatomy]

`DateField` wraps a `Label`, a `DateInput` that renders the individually editable segments, and optionally a `Description` and `FieldError`.

```tsx
import { DateField } from "@/components/ui/date-field"
import { DateInput } from "@/components/ui/input"
import { Description, FieldError, Label } from "@/components/ui/field"
```

```tsx
<DateField>
  <Label />
  <DateInput />
  <Description />
  <FieldError />
</DateField>
```

## Value [#value]

Values are [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/) objects. Use `defaultValue` for uncontrolled state, or `value` with `onChange` to control it.

```tsx
import { parseDate } from "@internationalized/date"
```

```tsx
<DateField defaultValue={parseDate("2020-02-03")}>
  <Label>Date</Label>
  <DateInput />
</DateField>
```

Set `granularity` (`day`, `hour`, `minute`, or `second`) to control which segments render, and constrain the range with `minValue` and `maxValue`.

## Validation [#validation]

Mark the field with `isRequired`, or drive it with `isInvalid` and a custom `validate`. Errors surface through `FieldError`.

```tsx
<DateField isInvalid>
  <Label>Event date</Label>
  <DateInput />
  <FieldError>Please select a date.</FieldError>
</DateField>
```

## Examples [#examples]

<Examples className="sm:grid-cols-2">
  <Example name="date-field/demos/basic" title="Basic Date Field" />

  <Example name="date-field/demos/prefix-and-suffix" title="With Icon" />

  <Example name="date-field/demos/event-form" title="Event Date Picker" />

  <Example name="date-field/demos/appointment" title="Appointment Booking" />

  <Example name="date-field/demos/trip-dates" title="Trip Date Range" />
</Examples>

## API Reference [#api-reference]

### DateField [#datefield]

<Reference name="date-field" />

### DateInput [#dateinput]

<Reference name="date-input" />

### DateSegment [#datesegment]

<Reference name="date-segment" />
