DatePicker is used when the user has to select a date or date range.
DatePicker is distributed in its own package and must be installed separately.

also known as Calendar

Figma:

Responsive:

Adaptive:

A11y:

Props

Component props
Name
Type
Default
id
Required
string
-

A unique identifier for the input.

onChange
Required
({
  event: SyntheticInputEvent<HTMLInputElement>,
  value: Date | null,
}) => void
-

Callback triggered when the user selects a date.

disabled
boolean
-

When disabled, DatePicker looks inactive and cannot be interacted with. See the disabled example to learn more.

errorMessage
string
-

Provide feedback when an error on selection occurs. See the error message example to learn more.

excludeDates
$ReadOnlyArray<Date>
-

Array of disabled dates. Datepicker can be interacted with except for the dates passed which look inactive and cannot be selected. See the disable selected dates example to learn more.

helperText
string
-

More information about how to complete the DatePicker field. See the helper text example to learn more.

idealDirection
"up" | "right" | "down" | "left"
"down"

Preferred direction for the calendar popover to open. See the ideal direction example to learn more.

includeDates
$ReadOnlyArray<Date>
-

Array of enabled dates. Datepicker can be interacted with only on the dates passed, all other dates look inactive and cannot be selected. See the disable selected dates example to learn more.

label
string
-

Provide a label to identify the DatePicker field.

localeData
{
  code?: string,
  formatDistance?: (...args: $ReadOnlyArray<any>) => any,
  formatRelative?: (...args: $ReadOnlyArray<any>) => any,
  localize?: {
    ordinalNumber: (...args: $ReadOnlyArray<any>) => any,
    era: (...args: $ReadOnlyArray<any>) => any,
    quarter: (...args: $ReadOnlyArray<any>) => any,
    month: (...args: $ReadOnlyArray<any>) => any,
    day: (...args: $ReadOnlyArray<any>) => any,
    dayPeriod: (...args: $ReadOnlyArray<any>) => any,
  },
  formatLong?: {
    date: (...args: $ReadOnlyArray<any>) => any,
    time: (...args: $ReadOnlyArray<any>) => any,
    dateTime: (...args: $ReadOnlyArray<any>) => any,
  },
  match?: {
    ordinalNumber: (...args: $ReadOnlyArray<string>) => any,
    era: (...args: $ReadOnlyArray<any>) => any,
    quarter: (...args: $ReadOnlyArray<any>) => any,
    month: (...args: $ReadOnlyArray<any>) => any,
    day: (...args: $ReadOnlyArray<any>) => any,
    dayPeriod: (...args: $ReadOnlyArray<any>) => any,
  },
  options?: {
    weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
    firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7,
  },
}
-

DatePicker accepts imported locales from the open source date utility library date-fns. See the locales example to learn more.

maxDate
Date
-

Disable dates outside a max date. See the disable future and past example to learn more.

minDate
Date
-

Disable dates outside a min date. See the disable future and past example to learn more.

name
string
-

A unique name for the input.

nextRef
{ current: null | HTMLInputElement }
-

Required for date range selection. Pass the complimentary range date picker ref object to DatePicker to autofocus on the unselected date range field. See the date range picker example to learn more.

placeholder
string
-

Placeholder text shown if the user has not yet input a value. The default placeholder value shows the date format for each locale, e.g. MM/DD/YYYY.

rangeEndDate
Date | null
-

Required for date range selection. End date on a date range selection. See the date range example to learn more.

rangeSelector
"start" | "end"
-

Required for date range selection. Defines the datepicker start/end role in a date range selection.See the date range picker example to learn more.

rangeStartDate
Date | null
-

Required for date range selection. Start date on a date range selection. See the date range picker example to learn more.

ref
React.Element<"input">
-

Required for date range selection. Pass a ref object to DatePicker to autofocus on the unselected date range field. See the date range picker example to learn more.

selectLists
$ReadOnlyArray<"month" | "year">
-

Show a select list for quick selection of year and/or month. See the selectLists variant to learn more.

value
Date | null
-

DatePicker can be a controlled component. value sets the current value of the input. See the controlled component date example to learn more.

Usage guidelines

When to use
  • Allowing users to choose a date or date range by clicking through the calendar popup or typing in the text field.
  • Limiting date options to a specific range of dates.
When not to use
  • When the native date picking experience is preferred (typically mobile and mWeb experiences). In this case, use TextField with type=”date”.

Accessibility

Localization

Be sure to localize all text strings. Note that localization can lengthen text by 20 to 30 percent.

Date format locales

DatePicker supports multiple locales. Adjust the date format to each date-fns locale.

The following locale examples show the different locale format variants.

Note that locale data from date-fns is external to gestalt-datepicker, it's not an internal dependency. Add date-fns to your app's dependencies.

import { DatePicker } from 'gestalt-datepicker';
import { it } from 'date-fns/locale';
<DatePicker localeData={it}/>

Use the SelectList to try out different locales by passing in the localeData prop.

Variants

Controlled component

DatePicker is a controlled component. Use value, onChange, onClearInput and onError to implement it correctly.

DatePicker is controlled when value is not "undefined". When value is "undefined", it stays uncontrolled.

  1. Empty input. If DatePicker doesn't present pre-selected date values, initialize value with "null" so the component is controlled.
  2. Pre-selected date values. If DatePicker presents pre-selected date values, initialize value with the pre-selected date so the component is controlled.
Empty input
import { useState } from 'react';
import { Box, Flex } from 'gestalt';
import { DatePicker } from 'gestalt-datepicker';

export default function Example() {
  const [dateValue, setDateValue] = useState(null);

  return (
    <Flex alignItems="start" height="100%" justifyContent="center" width="100%">
      <Box padding={2}>
        <DatePicker
          id="example-basic"
          label="Select a date"
          onChange={({ value }) => setDateValue(value)}
          value={dateValue}
        />
      </Box>
    </Flex>
  );
}

Pre-selected date values
import { useState } from 'react';
import { Box, Flex } from 'gestalt';
import { DatePicker } from 'gestalt-datepicker';

export default function Example() {
  const [dateValue, setDateValue] = useState(new Date(1985, 6, 4));

  return (
    <Flex alignItems="start" height="100%" justifyContent="center" width="100%">
      <Box padding={2}>
        <DatePicker
          id="example-basic"
          label="Select a date"
          onChange={({ value }) => {
            setDateValue(value);
          }}
          value={dateValue}
        />
      </Box>
    </Flex>
  );
}

States

  1. Disabled
  2. Error. Display an error message. Error message overrides the helper text.
Disabled
import { useState } from 'react';
import { Box, Flex } from 'gestalt';
import { DatePicker } from 'gestalt-datepicker';

export default function Example() {
  const [dateValue, setDateValue] = useState(new Date(1985, 6, 4));

  return (
    <Flex alignItems="start" height="100%" justifyContent="center" width="100%">
      <Box padding={2}>
        <DatePicker
          disabled
          id="example-disabled"
          label="User Activation Date"
          onChange={({ value }) => setDateValue(value)}
          value={dateValue}
        />
      </Box>
    </Flex>
  );
}

Error
import { useState } from 'react';
import { Box, Flex } from 'gestalt';
import { DatePicker } from 'gestalt-datepicker';

export default function Example() {
  const [dateValue, setDateValue] = useState(null);

  return (
    <Flex alignItems="start" height="100%" justifyContent="center" width="100%">
      <Box padding={2}>
        <DatePicker
          errorMessage={dateValue ? undefined : "This field can't be blank!"}
          helperText="Select a preferred day for your training."
          id="example-errorMessage"
          label="Schedule your training"
          onChange={({ value }) => setDateValue(value)}
        />
      </Box>
    </Flex>
  );
}

Helper text

import { useState } from 'react';
import { Box, Flex } from 'gestalt';
import { DatePicker } from 'gestalt-datepicker';

export default function Example() {
  const [dateValue, setDateValue] = useState(null);

  return (
    <Flex alignItems="start" height="100%" justifyContent="center" width="100%">
      <Box padding={2}>
        <DatePicker
          helperText="Select from the next available dateValueDisablePast"
          id="heleprText"
          label="Customer service appointment"
          minDate={new Date()}
          onChange={({ value }) => setDateValue(value)}
          value={dateValue}
        />
      </Box>
    </Flex>
  );
}

Date range

import { useRef, useState } from 'react';
import { Box, Flex } from 'gestalt';
import { DatePicker } from 'gestalt-datepicker';

export default function Example() {
  const [startDate, setStartDate] = useState(null);
  const [endDate, setEndDate] = useState(null);
  const endDateInput = useRef(null);
  const startDateInput = useRef(null);

  return (
    <Flex alignItems="start" height="100%" justifyContent="center" width="100%">
      <Box padding={2}>
        <Flex gap={{ column: 0, row: 2 }}>
          <DatePicker
            ref={startDateInput}
            id="example-start-date"
            label="Check In"
            nextRef={endDateInput}
            onChange={({ value }) => {
              setStartDate(value);
            }}
            rangeEndDate={endDate}
            rangeSelector="start"
            rangeStartDate={startDate}
            value={startDate}
          />
          <DatePicker
            ref={endDateInput}
            id="example-end-date"
            label="Check Out"
            nextRef={startDateInput}
            onChange={({ value }) => setEndDate(value)}
            rangeEndDate={endDate}
            rangeSelector="end"
            rangeStartDate={startDate}
            value={endDate}
          />
        </Flex>
      </Box>
    </Flex>
  );
}

Disabled dates

DatePicker supports disabling future & past dates as well as an array of selected dates.

Disable past & future dates
import { useState } from 'react';
import { Box, Flex } from 'gestalt';
import { DatePicker } from 'gestalt-datepicker';

export default function Example() {
  const [dateValueDisableFuture, setDateValueDisableFuture] = useState(null);
  const [dateValueDisablePast, setDatealueDisablePast] = useState(null);

  return (
    <Flex
      alignItems="start"
      gap={4}
      height="100%"
      justifyContent="center"
      width="100%"
    >
      <Box padding={4}>
        <DatePicker
          helperText="Enter your date of birth"
          id="disableFuture"
          label="Date of birth"
          maxDate={new Date()}
          onChange={({ value }) => setDateValueDisableFuture(value)}
          value={dateValueDisableFuture}
        />
      </Box>
      <Box padding={4}>
        <DatePicker
          helperText="Enter an activation date for your campaign"
          id="disablePast"
          label="Campaign activation date"
          minDate={new Date()}
          name="bday_datefield"
          onChange={({ value }) => setDatealueDisablePast(value)}
          value={dateValueDisablePast}
        />
      </Box>
    </Flex>
  );
}

Disable selected dates
import { useState } from 'react';
import { Box, Flex } from 'gestalt';
import { DatePicker } from 'gestalt-datepicker';

export default function Example() {
  const [dateValue, setDateValue] = useState(new Date(2020, 2, 9));

  return (
    <Flex alignItems="start" height="100%" justifyContent="center" width="100%">
      <Box padding={4}>
        <DatePicker
          excludeDates={[new Date(2020, 2, 11), new Date(2020, 2, 12)]}
          helperText="Enter an activation date for your campaign"
          id="disableSelecxted"
          label="Select Your Appointment"
          minDate={new Date()}
          name="bday_datefield"
          onChange={({ value }) => setDateValue(value)}
          value={dateValue}
        />
      </Box>
    </Flex>
  );
}

Select list

Provide select lists for quickly selecting year and month

import { useState } from 'react';
import { Box, Flex, SegmentedControl } from 'gestalt';
import { DatePicker } from 'gestalt-datepicker';

export default function Example() {
  const mapOptions = { 0: ['month'], 1: ['year'], 2: ['year', 'month'] };
  const items = ['Month', 'Year', 'Month & Year'];
  const [itemIndex, setItemIndex] = useState(0);
  const [dateValue, setDateValue] = useState(new Date(1985, 6, 4));

  return (
    <Flex alignItems="start" height="100%" justifyContent="center" width="100%">
      <Box padding={2}>
        <Flex direction="column" gap={4} width="100%">
          <SegmentedControl
            items={items}
            onChange={({ activeIndex }) => setItemIndex(activeIndex)}
            selectedItemIndex={itemIndex}
          />
          <DatePicker
            id="selectLists"
            label="Alberto's birth date"
            onChange={({ value }) => setDateValue(value)}
            selectLists={mapOptions[itemIndex.toString()]}
            value={dateValue}
          />
        </Flex>
      </Box>
    </Flex>
  );
}

Ideal Direction

Define the preferred direction for the DatePicker popover to open. If that placement doesn't fit, the opposite direction will be used.

idealDirection="down"
idealDirection="left"
idealDirection="right"
idealDirection="up"

External handlers

Experimental

DatePicker consumes external handlers from GlobalEventsHandlerProvider.

Handlers:

  • onRender: executed when DateField mounts for the first time

See GlobalEventsHandlerProvider for more information.

Component quality checklist

Component quality checklist
Quality item
Status
Status description
Figma Library
Ready
Component is available in Figma for web and mobile web.
Responsive Web
Ready
Component responds to changing viewport sizes in web and mobile web.
Issues
This component has known issues.

Internal documentation