DateField is used when the user has to select a date. Compared to DatePicker, DateField has no supporting calendar to select a date, the user must input date values with a numeric keyboard.

DateField is distributed within the "gestalt-datepicker" package and must be installed separately.

DateField is an experimental component. Expect development and design iteration, breaking API changes or even component deprecation.
Figma:

Responsive:

Adaptive:

A11y:

Props

Component props
Name
Type
Default
id
Required
string
-

A unique identifier for the input. See the controlled component example to learn more.

onChange
Required
({ value: Date | null }) => void
-

DateField is a controlled component. onChange is the callback triggered when the value of the input changes. Should be used to modify the controlled value. See the controlled component example to learn more.

onClearInput
Required
() => void
-

DateField is a controlled component. onClearInput is the callback triggered when the user clicks on the "clear" icon button. Should be used to clear the entered dates in the controlled component. See the controlled component example to learn more.

value
Required
Date | null
-

DateField is a controlled component. value sets the current value of the input. See the controlled component example to learn more.

autoComplete
"bday" | "off"
-

Indicate if birthday autocomplete should be available on the input.

disabled
boolean
false

Indicate if the input is disabled. See the disabled example for more details.

disableRange
"disableFuture" | "disablePast"
-

Prevent the user from selecting future or past dates. "disableFuture" disables values after the current date and "disablePast" disables values before the current date. This will return an error on onErrorwith values "disableFuture" or "disablePast". See the controlled component example to learn more.

errorMessage
string
-

Customize your error message for the cases the user enters invalid dates. See the controlled component example to learn more.

helperText
string
-

More information about how to complete the date field. See the controlled component example to learn more.

label
string
-

The label for the input. Be sure to localize the text. See the controlled component example to learn more.

labelDisplay
"visible" | "hidden"
"visible"

Whether the label should be visible or not. If hidden, the label is still available for screen reader users, but does not appear visually.

localeData
{
  code?: string,
  formatDistance?: (...args: $ReadOnlyArray<{ ... }>) => { ... },
  formatRelative?: (...args: $ReadOnlyArray<{ ... }>) => { ... },
  localize?: {
    ordinalNumber: (...args: $ReadOnlyArray<{ ... }>) => { ... },
    era: (...args: $ReadOnlyArray<{ ... }>) => { ... },
    quarter: (...args: $ReadOnlyArray<{ ... }>) => { ... },
    month: (...args: $ReadOnlyArray<{ ... }>) => { ... },
    day: (...args: $ReadOnlyArray<{ ... }>) => { ... },
    dayPeriod: (...args: $ReadOnlyArray<{ ... }>) => { ... },
  },
  formatLong?: {
    date: (...args: $ReadOnlyArray<{ ... }>) => { ... },
    time: (...args: $ReadOnlyArray<{ ... }>) => { ... },
    dateTime: (...args: $ReadOnlyArray<{ ... }>) => { ... },
  },
  match?: {
    ordinalNumber: (...args: $ReadOnlyArray<string>) => { ... },
    era: (...args: $ReadOnlyArray<{ ... }>) => { ... },
    quarter: (...args: $ReadOnlyArray<{ ... }>) => { ... },
    month: (...args: $ReadOnlyArray<{ ... }>) => { ... },
    day: (...args: $ReadOnlyArray<{ ... }>) => { ... },
    dayPeriod: (...args: $ReadOnlyArray<{ ... }>) => { ... },
  },
  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
-

Maximal selectable date. Disables any date values after the provided date.

minDate
Date
-

Minimal selectable date. Disables any date values before the provided date.

mobileEnterKeyHint
"enter" | "done" | "go" | "next" | "previous" | "search" | "send"
-

Mobile only prop. Optionally specify the action label to present for the enter key on virtual keyboards.

name
string
-

A unique name for the input. See the controlled component example to learn more.

onBlur
({
  event: SyntheticFocusEvent<HTMLInputElement>,
  value: string,
}) => void
-

Callback triggered when the user blurs the input.

onError
({
  errorMessage: string,
  value: Date | null,
}) => void
-

Callback triggered when the value entered is invalid. See the controlled component example to learn more.

onFocus
({
  event: SyntheticFocusEvent<HTMLInputElement>,
  value: string,
}) => void
-

Callback triggered when the user focuses the input.

readOnly
boolean
false

Indicate if the input is readOnly. See the readOnly example for more details.

size
"md" | "lg"
"lg"

Defines the height of ComboBox: md: 40px, lg: 48px. Width is defined by parent component.

Localization

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

Date format locales

DateField 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 { DateField } from 'gestalt-datepicker';
import { it } from 'date-fns/locale';
<DateField localeData={it}/>

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

Variants

Controlled component

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

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

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

  return (
    <Flex
      alignItems="center"
      gap={4}
      height="100%"
      justifyContent="center"
      width="100%"
    >
      <Box width={400}>
        <DateField
          errorMessage={errorText || undefined}
          helperText="Enter your date of birth"
          id="mainExample"
          label="Date of birth"
          name="bday_datefield"
          onChange={({ value }) => {
            setDateValue(value);
          }}
          onClearInput={() => setDateValue(null)}
          onError={({ errorMessage }) => setErrorText(errorMessage)}
          value={dateValue}
        />
      </Box>
    </Flex>
  );
}

Error messaging

DateField can communicate input errors to the user. Use onError and errorMessage to implement it correctly.

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

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

  return (
    <Flex
      alignItems="center"
      gap={4}
      height="100%"
      justifyContent="center"
      width="100%"
    >
      <Box width={400}>
        <DateField
          disableRange="disableFuture"
          errorMessage={errorText || undefined}
          helperText="Enter your date of birth"
          id="errorExample"
          label="Date of birth"
          name="bday_datefield"
          onChange={({ value }) => {
            setDateValue(value);
          }}
          onClearInput={() => {
            setErrorText(null);
            setDateValue(null);
          }}
          onError={({ errorMessage, value }) => {
            const date = value ? new Date(value) : null;

            if (errorMessage === 'invalidDate') return;
            if (
              errorMessage === 'disableFuture' ||
              (date && date.getFullYear() === 1)
            )
              setErrorText('Please, select a valid birth date');
            if (date && date.getFullYear() > 1) setErrorText(null);
          }}
          value={dateValue}
        />
      </Box>
    </Flex>
  );
}

States

DateField supports disabled and readOnly states.

import { Box, Flex } from 'gestalt';
import { DateField } from 'gestalt-datepicker';

export default function Example() {
  return (
    <Flex
      alignItems="center"
      direction="column"
      gap={4}
      height="100%"
      justifyContent="center"
      width="100%"
    >
      <Box width={400}>
        <DateField
          disabled
          id="disabled_example"
          label="Date of birth"
          name="bday_datefield"
          onChange={() => {}}
          onClearInput={() => {}}
          onError={() => {}}
          value={new Date('1995-12-17T03:24:00')}
        />
      </Box>
      <Box width={400}>
        <DateField
          id="readonly_example"
          label="Date of birth"
          name="bday_datefield"
          onChange={() => {}}
          onClearInput={() => {}}
          onError={() => {}}
          readOnly
          value={new Date('1995-12-17T03:24:00')}
        />
      </Box>
    </Flex>
  );
}

Disable past & future dates

DateField supports disabling future & past dates from being selected.

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

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

  const [errorTextDisableFuture, setErrorTextDisableFuture] = useState(null);
  const [errorTextDisablePast, setErrorTextDisablePast] = useState(null);

  return (
    <Flex
      alignItems="center"
      direction="column"
      gap={4}
      height="100%"
      justifyContent="center"
      width="100%"
    >
      <Box width={400}>
        <DateField
          disableRange="disableFuture"
          errorMessage={
            (errorTextDisableFuture &&
              errorTextDisableFuture === 'disableFuture' &&
              'Please, enter a valid past date') ||
            undefined
          }
          helperText="Enter your date of birth"
          id="disableFuture"
          label="Date of birth"
          name="bday_datefield"
          onChange={({ value }) => {
            setDateValueDisableFuture(value);
          }}
          onClearInput={() => setDateValueDisableFuture(null)}
          onError={({ errorMessage }) =>
            setErrorTextDisableFuture(errorMessage)
          }
          value={dateValueDisableFuture}
        />
      </Box>
      <Box width={400}>
        <DateField
          disableRange="disablePast"
          errorMessage={
            (errorTextDisablePast &&
              errorTextDisablePast === 'disablePast' &&
              'Please, enter a valid future date') ||
            undefined
          }
          helperText="Enter an activation date for your campaign"
          id="disablePast"
          label="Campaign activation date"
          name="bday_datefield"
          onChange={({ value }) => {
            setDatealueDisablePast(value);
          }}
          onClearInput={() => setDatealueDisablePast(null)}
          onError={({ errorMessage }) => setErrorTextDisablePast(errorMessage)}
          value={dateValueDisablePast}
        />
      </Box>
    </Flex>
  );
}

External handlers

Experimental

DateField consumes external handlers from GlobalEventsHandlerProvider.

Handlers:

  • onRender: executed when DateField mounts for the first time

See GlobalEventsHandlerProvider for more information.

Internal documentation

DatePicker
Use DatePicker if the user is allowed to pick a date from a calendar popup.