DatePicker allows a user to select a date or date range from a dropdown calendar.
The component can be imported via:
The PlumaDatePicker component uses the @internationalized/date
package for handling date values. This means it expects CalendarDate
instances in the value prop, and returns them in the onChange callback as well.
A CalendarDate can be created using its constructor:
Or by parsing an ISO 8601 formatted string with the parseDate function:
When withTime is enabled, the component will return ZonedDateTime instances that include time and timezone information. With isRange, it returns a start and end pair of ZonedDateTime instances.
See the CalendarDate documentation or the ZonedDateTime documentation for more details.
A native Date object can be converted to a CalendarDate like this:
A CalendarDate can be converted back to a native Date object with the toDate method, for example:
For more information, see the CalendarDate conversion docs.
To use the date picker, at a minimum you must provide the following props:
value
CalendarDate or ZonedDateTime instanceonChange
CalendarDate or ZonedDateTime instance for the date selected in the calendarlabel
PlumaDatePicker extends from the PlumaTextField component, and also accepts
ariaLabel as an alternative (see PlumaTextField docs for more details on accessibility)For example:
The date picker supports selecting date ranges, which can be enabled
by setting the isRange prop to true.
When enabled, the value prop should be an object with start and end properties,
each containing a CalendarDate instance.
Similarly, the value in the onChange callback will also be an object of that shape.
Set both isRange and withTime to select a range with a start time and an end time.
The value is an object with start and end properties, each a ZonedDateTime in the same time zone.
A new range starts as the whole selected day, from 12:00:00 AM to 11:59:59 PM.
The picker calls onChange when the user clicks "Save and Close".
minRange and maxRange also accept hours, minutes, and seconds in this mode, for example "2 hours" or { minutes: 30 }.
The picker shows an error and disables "Save and Close" when the range is outside these limits,
or when the end time is not after the start time.
Presets also work in this mode (see Presets). A relative preset ends now. With hours, minutes or seconds it is an exact span, so timeAgo: { value: 1, unit: 'hours' } selects the last hour. With days or longer units it starts at midnight, so PLUMA_DATE_PICKER_PRESET_LAST_7_DAYS (6 days back) covers 7 calendar days, today included. The selection is whole seconds.
A custom preset's callback can return ZonedDateTime values. If it returns CalendarDate values, the start becomes midnight of the start day and the end becomes the end of the end day, capped at the current time.
When you do not pass presets, a range with time uses PLUMA_DATE_PICKER_DEFAULT_TIME_PRESETS: Last hour, Last 24 hours, Last 7 days, and Last 30 days.
You can constrain the selectable dates in two ways:
The simplest approach is to use the minDate and maxDate props to define a valid date range.
Dates outside this range will be disabled and visually indicated. Users will not be able to select dates outside the specified range.
You can use either one or both constraints:
minDate to enforce a minimum selectable date with no upper limitmaxDate to enforce a maximum selectable date with no lower limitBoth minDate and maxDate accept a CalendarDate instance:
You can also use just one constraint if needed:
These constraints also work with date ranges:
For more complex date constraints, you can use the isDateDisabled function prop. This gives you complete control over which dates are selectable.
The isDateDisabled function takes a CalendarDate parameter and should return true if the date should be disabled, or false if it should be enabled. It will take precedence over the minDate and maxDate props.
Important notes about date constraints:
isRange={true}), both the start and end dates must fall within the allowed range.value that contains dates outside the allowed range, they will still be displayed, but the user will be unable to select new dates outside the range.minDate and maxDate are inclusive - users can select the exact date specified as the min/max boundary.To make it easier to select common date ranges, the date picker supports
a withPresets prop, which will render a list of presets next to the calendar.
This is only available when isRange is true.
The list of presets shown can be customized, by passing an array of preset objects
into the presets prop.
A preset object can be one of two types: relative or custom.
A relative preset requires:
type: 'relative'
label
timeAgo
timeAgo should contain two properties:
unit
DateTimeDuration type),
one of 'years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds', 'milliseconds'.value
unit to go back from todayA custom preset allows for more flexibility in creating the date range. It requires:
type: 'custom'labelcallback
start and end properties, each containing a CalendarDate instance.
This range will be set when the preset is clicked.localTimeZone - a string representing the current time zone (either the local time zone, or the one set via the DatePicker's timeZone prop)The default presets and preset types can also be imported from Pluma via:
Both preset types accept three optional keys:
icon - the name of an icon to show after the labeltooltip - text to show in a tooltip when the preset is hovered or focusedonClick - a function to call when the preset is clicked, instead of selecting its range.
It is called with an object containing close, a function that closes the date picker.
Use it for a preset that starts another action, for example a range the account cannot use yet,
where a click opens a modal to upgrade.
minDate, maxDate and isDateDisabled do not disable a preset with onClick, because it does not
select its range. A preset disabled with isPresetDisabled does not call onClick.By default, presets will be automatically disabled if their resulting date range
falls (even partially) outside the date constraints set by minDate and maxDate.
However, sometimes it's desirable to have the presets enabled anyway, allowing the selection
of partial ranges.
The isPresetDisabled boolean can control this behavior. It can receive either:
By default, the date picker allows selecting a range of any length - including a single day.
To enforce a minimum and/or maximum range length, the minRange and maxRange props can be used.
Both props accept multiple formats:
"{quantity} {unit}", where unit is one of: days, weeks, months, years (e.g. "6 months", "2 weeks"). With withTime, the unit can also be hours, minutes, or seconds (see Time ranges)@internationalized/date DateTimeDuration objectBy default, PlumaDatePicker will use the user's local time zone when working with dates:
most importantly, determining what "today" is for pre-selecting today's date, calculating relative ranges etc.
However, sometimes it's necessary to show a DatePicker that refers to dates in a different time zone.
To make sure the component handles disabled dates, presets, etc correctly, a localTimeZone string prop
can be provided.
The time zone string is passed into Intl API calls,
and should be an IANA time zone name.
If an invalid time zone is provided, it will fall back to @internationalized/date's getLocalTimeZone function.
If a string label is not sufficient, a component can be used to render the label.
In React, the label prop accepts any ReactNode.
If a string description is not sufficient, a component can be used to render the description.
In React, the description prop accepts any ReactNode.
If a string error is not sufficient, a component can be used to render the error.
In React, the error prop accepts any ReactNode.
PlumaDatePicker extends TextFieldDefault:'input'
What element or component should be rendered.
Whether the date picker should render in the open state.
Function to determine if a date is disabled and cannot be selected. This takes precedence over minDate and maxDate if provided.
By default, a preset will become disabled if its range cannot be fully selected
(based on dates being disabled via minDate and maxDate).
A preset with onClick is not disabled this way, because it does not select its range.
This allows overriding that behavior to enable/disable presets conditionally,
or keep them always enabled.
If a function is provided, it will be called with the preset and its calculated range,
and should return a boolean.
If the value is a boolean, it will apply to all presets.
Whether the date picker should select date ranges.
Combine with withTime to select a range with a start time and an end time.
By default, the DatePicker uses the user's local time zone for calculating dates like "today". If the date picker represents dates in a different time zone, a time zone name can be provided here.
The maximum selectable date.
The maximum range length that must be selected. By default, there is no maximum range length. It accepts multiple formats:
withTime, the unit can also be hours, minutes, or seconds (e.g. "2 hours", "30 minutes")@internationalized/date DateTimeDuration objectThe minimum selectable date.
The minimum length of a time range that must be selected. By default, there is no minimum range length (essentially 0 - same day selection allowed). It accepts multiple formats:
withTime, the unit can also be hours, minutes, or seconds (e.g. "2 hours", "30 minutes")@internationalized/date DateTimeDuration objectCallback fired when the date selection changes.
The duration (in ms) for the popover animation.
Middleware options for the PlumaPopover component.
An array of presets to render instead of the default ones.
Default:true
Whether the calendar should automatically focus when it first renders.
Default:true
Whether the open popover traps focus and hides the rest of the page from assistive technology, as a modal dialog does. Turn it off only where the popover is shown as an illustration beside other content, such as a documentation page.
Additional inline styles to apply on the popover component (the floating element, not the trigger). Should only be used as a last resort
The current date selection.
Whether the date picker should show preset selection.
Only available when isRange is true.
Default:false
Whether to include time selection in the picker.
With isRange, the picker selects a start time and an end time.
An icon to show after the label, aligned to the end of the preset button.
The text to show for the preset button.
Called when the preset is clicked, instead of selecting its range.
Use it for a preset that starts another action, like opening a modal.
minDate, maxDate and isDateDisabled do not disable a preset with onClick.
A preset disabled with isPresetDisabled does not call it.
The time period (from now into the past) to select.
unit is an @internationalized/date DateTimeDuration key, one of:
'years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds', 'milliseconds'.
value is how many of that unit to go back.
Text to show in a tooltip when the preset button is hovered or focused.
The type of preset.
A callback function that returns the start and end dates for the preset.
The dates must be @internationalized/date CalendarDates
or, for pickers with withTime, ZonedDateTimes.
With withTime, CalendarDate results are converted: the start becomes the start of its day, and the end
becomes the end of its day, capped at the current time.
An icon to show after the label, aligned to the end of the preset button.
The text to show for the preset button.
Called when the preset is clicked, instead of selecting its range.
Use it for a preset that starts another action, like opening a modal.
minDate, maxDate and isDateDisabled do not disable a preset with onClick.
A preset disabled with isPresetDisabled does not call it.
Text to show in a tooltip when the preset button is hovered or focused.
The type of preset.