DatePicker
This component can display past, present, or future dates relevant to a task. Whilst this component is useful for selecting a range of dates, we recommend using DateInput for collecting data like date of birth.
Basic usage
import { DatePicker } from '@krakentech/coral';
<DatePicker label="Date Picker" />Properties
disablePast / disableFuture
Use the disablePast flag if you need to disable past dates and the disableFuture flag if you need to disable future dates.
<DatePicker label="Disable Past" disablePast />
<DatePicker label="Disable Future" disableFuture />disabled
<DatePicker disabled />defaultMonth
Customise the date that the calender opens to initially.
<DatePicker label="Date Picker" defaultMonth={new Date(2023, 11)} />endIcon
<DatePicker label="With End Icon" endIcon={<IconCross />} />startIcon
<DatePicker label="With Start Icon" startIcon={<IconCross />} />error / errorMessage
Analogous to the TextField component, the error flag and errorMessage prop are used for controlling error behaviour.
<DatePicker
label="Date Picker"
error
errorMessage="Some generic error message."
/>Some generic error message.
excludeDateIntervals
The excludeDateIntervals props provide a very convenient interface for excluding multiple date intervals.
const daysFromNow = (days: number) => {
const today = new Date();
return new Date(today.getTime() + days * 24 * 60 * 60 * 1000);
};
<DatePicker
label="Date Picker"
excludeDateIntervals={[
{ start: daysFromNow(-2), end: daysFromNow(2) },
{ start: daysFromNow(5), end: daysFromNow(8) },
]}
/>excludeDates
The excludeDates props provide a very convenient interface for excluding specific dates.
<DatePicker
label="Date Picker"
excludeDates={[daysFromNow(-8), daysFromNow(-10)]}
/>fullWidth
<DatePicker label="Date Picker" fullWidth />includeDateIntervals
You can use includeDateIntervals if you want specific date spans to be allowed.
<DatePicker
label="Date Picker"
includeDateIntervals={[
{ start: daysFromNow(-2), end: daysFromNow(2) },
{ start: daysFromNow(5), end: daysFromNow(8) },
]}
/>includeDates
You can use includeDates and includeDateIntervals if you want specific dates or date spans to be allowed.
<DatePicker
label="Date Picker"
includeDates={[daysFromNow(-1), daysFromNow(1), daysFromNow(3)]}
/>isClearable
<DatePicker label="Date Picker" isClearable />locale
<DatePicker label="Germany 🇩🇪" locale="de" />
<DatePicker label="Spain 🇪🇸" locale="es" />
<DatePicker label="France 🇫🇷" locale="fr" />
<DatePicker label="Italy 🇮🇹" locale="it" />
<DatePicker label="Japan 🇯🇵" locale="ja" />minDate / maxDate
The minDate and maxDate props are used to configure a valid time window.
<DatePicker
label="Date Picker"
minDate={daysFromNow(-12)}
maxDate={daysFromNow(12)}
/>open
const WithOpen = (args: DatePickerProps) => {
const [open, setOpen] = useState(false);
return (
<Stack>
<DatePicker
label="Date Picker"
open={open}
onSelect={() => setOpen(false)}
/>
<Button size="small" onClick={() => setOpen(!open)}>
{open ? 'Close Datepicker' : 'Open Datepicker'}
</Button>
</Stack>
);
};theme
<Container padding="md" theme="base100">
<DatePicker label="Theme: secondary500" theme="secondary500" />
</Container>placeholderText
<DatePicker label="Date Picker" placeholderText="Enter a date" />popperPlacement
<DatePicker label="Date Picker" popperPlacement="auto" />optional
<DatePicker label="Date Picker" optional />selectsRange
<DatePicker label="Date Picker" selectsRange />shouldCloseOnSelect
<DatePicker label="Date Picker" shouldCloseOnSelect />showYearPicker
<DatePicker label="Year range picker" showYearPicker selectsRange />
<DatePicker label="Year picker" showYearPicker />
<DatePicker label="Year picker with disabled past" showYearPicker disablePast={true} />Accessibility
aria-labels
<DatePicker
label="Date Picker"
nextMonthButtonLabel="Next Month"
nextYearButtonLabel="Next Year"
previousMonthButtonLabel="Previous Month"
previousYearButtonLabel="Previous Year"
/>User Events
<DatePicker
onBlur={() => {}}
onCalendarClose={() => {}}
onCalendarOpen={() => {}}
onChange={(date) => {}}
onClickOutside={(event) => {}}
onFocus={(event) => {}}
onInputClick={() => {}}
onKeyDown={(event) => {}}
onMonthChange={(date) => {}}
onSelect={(date, event) => {}}
...
/>Responsiveness
<Container padding="md" theme={{ base: "base900", lg: "base100" }}>
<DatePicker
label="Responsive"
fullWidth={{ base: false, lg: true }}
theme={{ base: "base100", lg: "secondary500" }}
/>
</Container>Full API
| Name | Type | Default |
|---|---|---|
disabled | booleanIf | false |
disableFuture | booleanIf | false |
disablePast | booleanIf | false |
defaultMonth | DateThe default month to display when the DatePicker is opened. | |
endIcon | ReactNodeThe icon displayed at the end of the DatePicker input. | |
error | booleanIf | false |
errorMessage | stringIf set, the | 'Invalid input' |
excludeDateIntervals | { start: Date; end: Date; }[]A list of date intervals to exclude from selection. Each interval is defined by a start and end date. | [] |
excludeDates | Date[]A list of dates to exclude from selection. | [] |
fullWidth | ResponsiveVariant<boolean>If | |
id | stringThe id of the input element. If unspecified, an id will be generated internally. | |
includeDateIntervals | { start: Date; end: Date; }[]A list of date intervals to include from selection. Each interval is defined by a start and end date. | [] |
includeDates | Date[]A list of dates to include from selection. | [] |
isClearable | booleanIf | false |
label | stringThe label for the DatePicker input field. | |
locale | SupportedLocaleThe locale to use for formatting dates. | en-GB |
maxDate | Date | nullThe maximum date allowed for selection. | |
minDate | Date | nullThe minimum date allowed for selection. | |
name | stringThe field name used on the POST request when the form is submitted. | |
nextMonthButtonLabel | stringThe aria-label for the DatePicker’s next month button. | 'Next month' |
nextYearButtonLabel | stringThe aria-label for the DatePicker’s next year button. | 'Next year' |
numberOfMonths | numberThe number of months to display in the DatePicker. | |
onBlur | FocusEventHandler<HTMLInputElement>Callback fired when the input loses user focus.
| |
onCalendarClose | () => voidCallback fired when the calendar popup is closed. | |
onCalendarOpen | () => voidCallback fired when the calendar popup is opened. | |
onChange | (date: Date | DateRange | null) => voidCallback fired when the selected date changes.
| |
onClickOutside | (event: Event | undefined) => voidCallback fired when the user clicks outside the DatePicker.
| |
onFocus | (event: Event | undefined) => voidCallback fired when the DatePicker input gains user focus.
| |
onInputClick | () => voidCallback fired when the DatePicker input is clicked. | |
onKeyDown | (event: KeyboardEvent<HTMLDivElement>) => voidCallback fired when a key is pressed down on the DatePicker input. | |
onMonthChange | (date: Date) => voidCallback fired when the month is changed.
| |
onSelect | (date: Date, event: Event | undefined) => voidCallback fired when a date is selected.
| |
open | booleanIf | |
theme | ResponsiveVariant<SharedThemeTypes>The colourway applied to this component. | 'base100' |
placeholderText | stringThe placeholder text for the input field. | |
popperPlacement | PlacementPosition of the popper relative to the input. | |
previousMonthButtonLabel | stringThe aria-label for the DatePicker’s previous month button. | 'Previous month' |
previousYearButtonLabel | stringThe aria-label for the DatePicker’s previous year button. | 'Previous year' |
optional | booleanAdd an optional flag to the label for this field (controlled by https://coral-oe.vercel.app/?path=/docs/guides-configuration—docs#overrides-optional-indicator optionalIndicator). This should be set to true if you have set this field to be optional in your validation schema. | false |
selectsRange | booleanIf | false |
shouldCloseOnSelect | booleanIf | true |
showYearPicker | booleanIf | false |
showYearDropdown | booleanIf | false |
startIcon | ReactNodeThe icon displayed at the start of the DatePicker input field. | |
tabIndex | numberThe tab index for the DatePicker input. | |
title | stringThe label title for the DatePicker input. | |
valueInput | string | Date | DateRange | nullProvides external control over the | |
valueDate | Date | DateRange | nullProvides external control over the | |
yearDropdownItemNumber | numberThe number of items which are displayed in the year dropdown in each direction. | 100 |
upLevelButtonLabel | stringThe aria-label for the button that moves back to calendar view from Month or Year view. | 'Back to Calendar' |
monthSelectionLabel | stringThe aria-label for the manual month selection. | 'Select Month' |
yearSelectionLabel | stringThe aria-label for the manual year selection. | 'Select Year' |
datesTableLabel | ReactNode | |
monthsTableLabel | ReactNode | |
yearsTableLabel | ReactNode |
Design
Good to know
Choose a date picker if you need or want the user to select a date in the past, present or future this could include. This can also include a date range. The date picker is partnered with a text field, this means the user can manually input the date or select it from the calendar to avoid manual error.
How does it work?
When text field is clicked the calendar appears below. This allows you to interact with it or manually input the desired date. Once a date or date range is selected the text field automatically fills and the calendar disappears.
How do I navigate date, month and year?
The day you simply select from the calendar. The ‘down’ chevron allows you to see the various years (in 12 year groups). The ‘left’ and ‘right’ chevrons allow you to navigate the months on the existing year.
Component breakdown
- Corner radius: 12px
- Fill colour: Blueberry and Ink
- Text colour: Ice and Purple Haze
- Title: H6
- Days: Caption text
- Dates/Years: Body Text 2