Date picker API
Import
Section titled “Import”import { DatePicker } from '@cimpress-ui/react';Value format
Section titled “Value format”Date picker uses the @internationalized/date package to represent dates and times. You do not need to install this package separately. All exports from @internationalized/date are re-exported from @cimpress-ui/react/date.
Refer to @internationalized/date documentation to learn more about it. Remember that whenever the documentation instructs you to import something from @internationalized/date, you can import the same thing from @cimpress-ui/react/date instead.
To display a time within the date picker, pass a CalendarDateTime instance to the value or defaultValue prop. You can also control how many date/time segments are displayed using the granularity prop.
In this example, both fields are controlled with the same state value, but with different display granularity.
import { DatePicker, Stack, Text } from '@cimpress-ui/react';import { type CalendarDateTime, parseDateTime } from '@cimpress-ui/react/date';import { useState } from 'react';
export default function Demo() { const [selectedDate, setSelectedDate] = useState<CalendarDateTime | null>(() => parseDateTime('1990-11-01T10:54'));
return ( <Stack gap={32}> <Text as="p"> In this example, both fields are controlled with the same state value, but with different display granularity. </Text> <DatePicker label="Date and time of birth" value={selectedDate} onChange={setSelectedDate} granularity="minute" /> <DatePicker label="Date of birth" value={selectedDate} onChange={setSelectedDate} granularity="day" /> </Stack> );}Time zones
Section titled “Time zones”Provide a ZonedDateTime instance to the date picker to make it time zone aware.
In this example, both fields are controlled with the same state value, but with different time zones.
import { DatePicker, Stack, Text } from '@cimpress-ui/react';import { parseZonedDateTime, toTimeZone, type ZonedDateTime } from '@cimpress-ui/react/date';import { LocalizationProvider } from '@cimpress-ui/react/i18n';import { useState } from 'react';
export default function Demo() { const [selectedDate, setSelectedDate] = useState<ZonedDateTime | null>(() => parseZonedDateTime('1990-11-01T03:54[Europe/Warsaw]'), );
return ( <Stack gap={32}> <Text as="p"> In this example, both fields are controlled with the same state value, but with different time zones. </Text> <LocalizationProvider locale="en-US"> <DatePicker label="Date and time of birth (Warsaw)" value={selectedDate} onChange={setSelectedDate} granularity="minute" /> <DatePicker label="Date and time of birth (Los Angeles)" value={selectedDate ? toTimeZone(selectedDate, 'America/Los_Angeles') : null} onChange={(value) => setSelectedDate(value ? toTimeZone(value, 'Europe/Warsaw') : null)} granularity="minute" /> </LocalizationProvider> </Stack> );}Controlled/uncontrolled usage
Section titled “Controlled/uncontrolled usage”This component can be used in a controlled or uncontrolled way.
In the controlled way, this component doesn’t maintain its own internal state, and its value is provided by the parent component. Use the controlled way when you need to be able to change the state of this component in other parts of your application.
In the uncontrolled way, this component maintains its own internal state, and can optionally notify the parent component when its internal state changes. Use the uncontrolled way when you don’t need to change the state of this component in other parts of your application.
import { DatePicker, Stack } from '@cimpress-ui/react';import { type CalendarDate, getLocalTimeZone, today } from '@cimpress-ui/react/date';import { useState } from 'react';
export default function Demo() { const [selectedDate, setSelectedDate] = useState<CalendarDate | null>(() => today(getLocalTimeZone()));
return ( <Stack gap={32}> <DatePicker label="Date of birth (controlled)" value={selectedDate} onChange={setSelectedDate} /> <DatePicker label="Date of birth (uncontrolled)" defaultValue={today(getLocalTimeZone())} /> </Stack> );}Imperative API
Section titled “Imperative API”This component exposes an imperative API through the apiRef prop. This API allows triggering behaviors that can’t be expressed by props.
import { Button, DatePicker, type DatePickerApi, Stack } from '@cimpress-ui/react';import { getLocalTimeZone, today } from '@cimpress-ui/react/date';import { useRef } from 'react';
export default function Demo() { const apiRef = useRef<DatePickerApi>(null);
return ( <Stack gap={16}> <DatePicker label="Date of birth" defaultValue={today(getLocalTimeZone())} apiRef={apiRef} />
<Button onPress={() => apiRef.current?.focus()}>Focus</Button> </Stack> );}Accessibility notes
Section titled “Accessibility notes”Grids in the popup calendar follow the ARIA grid pattern. See the linked page for a list of available keyboard interactions.
Each date and time unit in the date picker field is an individually focusable and editable segment. Each segment can be edited using a keyboard, and incremented/decremented using up/down arrow keys. Use left/right arrow keys or the Tab key to move between segments.
DatePicker requires a textual label to remain accessible to assistive technologies. See our accessibility guide for more details.
Changing the calendar system
Section titled “Changing the calendar system”By default, DatePicker selects a calendar system based on the application’s locale. You can force the date picker to use a specific calendar system using a locale extension subtag.
import { DatePicker, Stack } from '@cimpress-ui/react';import { LocalizationProvider } from '@cimpress-ui/react/i18n';
export default function Demo() { return ( <Stack gap={32}> <LocalizationProvider locale="en-US-u-ca-gregory"> <DatePicker label="Date of birth" description="This date picker uses the Gregorian calendar." /> </LocalizationProvider>
<LocalizationProvider locale="en-US-u-ca-indian"> <DatePicker label="Date of birth" description="This date picker uses the Indian calendar." /> </LocalizationProvider> </Stack> );}Date picker can integrate with native HTML forms. See our forms guide to learn how to work with forms.
API reference
Section titled “API reference”DatePicker
Section titled “DatePicker”Allows users to enter or select a date and time value.
- Ref<HTMLDivElement>
-
The
reftype for this component.
DatePickerProps<T extends DateValue>
- string
-
The element's unique identifier. See MDN.
- boolean
data-cim-style-root
Section titled “ data-cim-style-root ” -
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- string
UNSAFE_className
Section titled “ UNSAFE_className ” -
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- CSSProperties
UNSAFE_style
Section titled “ UNSAFE_style ” -
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
- string
label
Section titled “ label ” -
The content to display as the label.
- string
aria-label
Section titled “ aria-label ” -
Defines a string value that labels the current element.
- string
aria-labelledby
Section titled “ aria-labelledby ” -
Identifies the element (or elements) that labels the current element.
- string
aria-describedby
Section titled “ aria-describedby ” -
Identifies the element (or elements) that describes the object.
- string
aria-details
Section titled “ aria-details ” -
Identifies the element (or elements) that provide a detailed, extended description for the object.
- string
-
The
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN. - string
-
The name of the input element, used when submitting an HTML form. See MDN.
- string
description
Section titled “ description ” -
A description for the field. Provides a hint such as specific requirements for what to choose.
- FieldError
error
Section titled “ error ” -
An error message for the field.
- (value: MappedDateValue) => string | true | string[] | undefined
validate
Section titled “ validate ” -
A function that returns an error message (or
true) if a given value is invalid. Validation errors are displayed to the user when the form is submitted. For real-time validation, use theerrorprop instead. - RefObject<DatePickerApi | null>
apiRef
Section titled “ apiRef ” -
A React ref that allows access to the imperative API of this component.
- DateValue | null
focusedValue
Section titled “ focusedValue ” -
Controls the currently focused date within the calendar.
- DateValue | null
defaultFocusedValue
Section titled “ defaultFocusedValue ” -
The date that is focused when the calendar first mounts (uncountrolled).
- DateValue | null
minValue
Section titled “ minValue ” -
The minimum allowed date that a user may select.
- DateValue | null
maxValue
Section titled “ maxValue ” -
The maximum allowed date that a user may select.
- (date: DateValue) => boolean
isDateUnavailable
Section titled “ isDateUnavailable ” -
Callback that is called for each date of the calendar. If it returns true, then the date is unavailable.
- T | null
placeholderValue
Section titled “ placeholderValue ” -
A placeholder date that influences the format of the placeholder shown when no value is selected. Defaults to today's date at midnight.
- Granularity
granularity
Section titled “ granularity ” -
Determines the smallest unit that is displayed in the date picker. By default, this is
"day"for dates, and"minute"for times. - 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat'
firstDayOfWeek
Section titled “ firstDayOfWeek ” -
The day that starts the week.
- string
autoComplete
Section titled “ autoComplete ” -
Describes the type of autocomplete functionality the input should provide if any. See MDN.
- boolean
isOpen
Section titled “ isOpen ” -
Whether the overlay is open by default (controlled).
- boolean
defaultOpen
Section titled “ defaultOpen ” -
Whether the overlay is open by default (uncontrolled).
- (isOpen: boolean) => void
onOpenChange
Section titled “ onOpenChange ” -
Handler that is called when the overlay's open state changes.
- (e: FocusEvent<Element>) => void
onFocus
Section titled “ onFocus ” -
Handler that is called when the element receives focus.
- (e: FocusEvent<Element>) => void
onBlur
Section titled “ onBlur ” -
Handler that is called when the element loses focus.
- boolean
autoFocus
Section titled “ autoFocus ” -
Whether the element should receive focus on render.
- boolean
isRequired
Section titled “ isRequired ” -
Whether user input is required on the input before form submission.
- boolean
isInvalid
Section titled “ isInvalid ” -
Whether the input value is invalid.
- boolean
isDisabled
Section titled “ isDisabled ” -
Whether the input is disabled.
- boolean
isReadOnly
Section titled “ isReadOnly ” -
Whether the input can be selected but not changed by the user.
- T | null
value
Section titled “ value ” -
The current value (controlled).
- T | null
defaultValue
Section titled “ defaultValue ” -
The default value (uncontrolled).
- (value: MappedDateValue<T> | null) => void
onChange
Section titled “ onChange ” -
Handler that is called when the value changes.
StyleProps
- Responsive<Spacing | "auto">
margin
Section titled “ margin ” -
The amount of margin applied to all edges of this component.
- Responsive<Spacing | "auto">
marginX
Section titled “ marginX ” -
The amount of margin applied to the left and right edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginY
Section titled “ marginY ” -
The amount of margin applied to the top and bottom edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginTop
Section titled “ marginTop ” -
The amount of margin applied to the top edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginRight
Section titled “ marginRight ” -
The amount of margin applied to the right edge of this component. Takes priority over
marginXandmargin. - Responsive<Spacing | "auto">
marginBottom
Section titled “ marginBottom ” -
The amount of margin applied to the bottom edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginLeft
Section titled “ marginLeft ” -
The amount of margin applied to the left edge of this component. Takes priority over
marginXandmargin.