---
title: DatePicker
---

import LibraryInfo from '@theme/LibraryInfo'
import { Feedback, TokensTable, PropsTable } from '../../src/components'
import { DatePicker } from '@telus-uds/components-web'
import { DatePicker as WebDocs } from 'docs/@telus-uds/components-web'
import AvailableOnWebOnly from '../../partials/_platform-considerations-web-only.mdx'

# DatePicker

<LibraryInfo
  packageType="Web Component"
  figmaLink="https://www.figma.com/embed?embed_host=share&url=https%3A%2F%2Fwww.figma.com%2Ffile%2F4zJn2Qn6uXOqPP1GVDipy6%2F%25F0%259F%258D%2580-Allium-Components%3Fnode-id%3D7765%253A149445"
/>

```jsx live
<DatePicker id="DatePicker" label="DatePicker" hint="Enter a Date" tooltip="This is a tooltip" />
```

## Introduction

Use DatePicker to select a date on a calendar.

[Follow the appropriate instructions](importing-components) to add this component in to your app.

## Guidance

- Use overlay (default) variant to display in a modal container
- Use overlay variant whenever possible as it is the most accessible solution; the input form field is type-accessible and optimized for mobile customers
- Includes an input form field to allow manual key-in of dates
- Opens the modal when the input form field receives focus
- Width expands 100% until max-width of 340px
- Enable selection of old dates by passing a `function` that returns a `boolean` in `isDayDisabled`

### Uncontrolled component

:::warning
While `DatePicker` can be used as an uncontrolled component, using a ref to get the input's value behaves inconsistently between the default and inline version of the component. The inline version forwards the ref to a hidden input, but due to the limitations of the `react-dates` library, the default `DatePicker` attaches the ref to the component's wrapper.

It's recommended to use the `onDateChange` prop to get the current value of the input even when using it as an uncontrolled component. If you must rely on a ref to get the value on the input, you will need to find the input element nested inside the wrapper.
:::

```jsx live
function UncontrolledExample() {
  const [date, setDate] = useState()

  return (
    <>
      <DatePicker id="DatePicker" label="DatePicker" onDateChange={setDate} />
      Current Value: {date ? date.format('YYYY-MM-DD') : 'None'}
    </>
  )
}
```

### Controlled component

If it is required that the state of the `DatePicker` be controlled by the application or other external methods,
`date` and `onDateChange` props must be passed to the `DatePicker`.

```jsx live
function ControlledExample() {
  const [date, setDate] = useState()

  return <DatePicker id="DatePicker" label="DatePicker" date={date} onDateChange={setDate} />
}
```

### Initial Visible Month

The `initialVisibleMonth` prop can be used to set the month that the calendar will open to. This prop accepts a string in the format `YYYY-MM`.

```jsx live
function InitialVisibleMonthExample() {
  const [date, setDate] = useState()

  return (
    <>
      <DatePicker
        id="DatePicker"
        label="DatePicker"
        initialVisibleMonth="2024-12"
        date={date}
        onDateChange={setDate}
      />
      Current Value: {date ? date.format('YYYY-MM-DD') : 'None'}
    </>
  )
}
```

### Accessibility

DatePicker has accessible controls and also uses the `label` prop to label the corresponding input.

### Platform considerations

<AvailableOnWebOnly />

### Date enablement

The `isDayDisabled` prop allows you to disable specific dates in the calendar. This function is called for each date and should return `true` to disable that date, or `false` to enable it.

```jsx live
function DateEnablementExample() {
  const [date, setDate] = useState()

  // Example: Disable past dates
  const isDayDisabled = (day) => {
    const today = new Date()
    today.setHours(0, 0, 0, 0) // Reset time to start of day

    const dayDate = new Date(day.toDate())
    dayDate.setHours(0, 0, 0, 0) // Reset time to start of day

    return dayDate <= today
  }

  return (
    <>
      <DatePicker
        id="DatePickerDisabled"
        label="Select a future date"
        hint="Past dates are disabled"
        onDateChange={setDate}
        isDayDisabled={isDayDisabled}
      />
      Current Value: {date ? date.format('YYYY-MM-DD') : 'None'}
    </>
  )
}
```

### Custom date format

A custom date format can be defined through the `dateFormat` prop. It will also be the default `placeholder` value if one is not provided.
Valid date formats can be found in the [Moment.js Documentation](https://momentjs.com/docs/#/parsing/string-format/).

```jsx live
<DatePicker id="DatePicker" label="DatePicker" dateFormat="YYYY-MM-DD" />
```

## Props

<PropsTable web={WebDocs} />

## Tokens

<TokensTable web={WebDocs} component="DatePicker" />

## Variants

### Inline

- Use Inline to display the DatePicker in a larger vieweable area; allows the customer to quickly and easily date availability
- Does not include an input form field
- Optimized for keyboard interaction and tablet touch
- Recommended for viewports greater than or equal to 576px

```jsx live
<DatePicker id="DatePicker" inline />
```

## Feedback

<Feedback component="DatePicker" />
