Date Range Picker
A date range picker input element allows users to select 2 dates from a calendar-style interface.
import { DateRangePicker } from "@vitality-ds/components";
The DateRangePicker
input element is a form field that provides users with a calendar-style interface to select a range of dates. This input type is commonly used for tasks such as filtering search results by date.
The DateRangePicker
's value
returns the selected date range as an array of strings in ISO 8601 format: ["yyyy-mm-dd", "yyyy-mm-dd"]
.
When interacting with either of the date inputs, an event
object is returned. You can find more details about this event object in the DatePicker documentation.
Within the event object, each key/value you'll find an array that contains two elements. The first element represents the 'from' field, and the second element represents the 'to' field. These elements provide you with the selected date range for further processing or display.
Here's an example returned event object:
event = { isValid: [true, false], errorMessage: [null, "Invalid Date Provided"], textValue: ["10/05/2023", "10/15/2023"], value: ["2023-05-10", ""], };
Building a form? Checkout our FormField here
() => { const [dateRange, setDateRange] = React.useState([]); const changeHandler = (event) => { setDateRange(event.value); }; return ( <Stack> <DateRangePicker value={dateRange} name="filter_date_range" id="filter_date_range" onChange={changeHandler} /> </Stack> ); };
Just like the DatePicker
you can limit the range available for selection (with the DatePicker
navigation tools). The props minYear
and maxYear
. The DateRangePicker
's drop-down menus and navigation buttons have restricted options based on the defined props, preventing selection of dates outside the specified range. By default, the date picker allows for choosing dates within a range of 5 years before and after the current year.
It's important to keep in mind that these props only limit the available options in the navigation tools, and manual entry of dates outside the range is still possible. Additional validation is required to ensure that any manually entered date falls within the desired range.
() => { const [dateRange, setDateRange] = React.useState([]); const changeHandler = (event) => { setDateRange(event.value); }; return ( <Stack> <DateRangePicker value={dateRange} minYear={2023} maxYear={2030} name="filter_date_range" id="filter_date_range" onChange={changeHandler} /> </Stack> ); };
The DateRangePicker
should be used as a controlled component to work correctly. It requires passing the selected date range value through a value
prop in order for the calendar to update correctly when a date range is selected.
The value of the DateRangePicker
is always an array of two strings in the format ["yyyy-mm-dd", "yyyy-mm-dd"]
.
- If no first date is selected then the first item in the list will be an empty string:
["", "yyyy-mm-dd"]
. - If the second
DatePicker
has no value the value of theDateRangePicker
will be["yyyy-mm-dd", ""]
. - If both are empty:
["", ""]
.
() => { const [dateRange, setDateRange] = React.useState([]); const changeHandler = (event) => { setDateRange(event.value); }; return ( <Stack> <DateRangePicker value={dateRange} name="filter_date_range" id="filter_date_range" onChange={changeHandler} /> </Stack> ); };
With the props fromLabel
and toLabel
allow you to change the labels on both of the DatePickers
. fromLabel
changes the first label, defaulting to "From"
. tolabel
changes the second label, defaulting to "To"
.
() => { const [dateRange, setDateRange] = React.useState([]); const changeHandler = (event) => { setDateRange(event.value); }; return ( <Stack> <DateRangePicker value={dateRange} fromLabel="I've changed the first label" toLabel="I've changed the second label" name="filter_date_range" id="filter_date_range" onChange={changeHandler} /> </Stack> ); };
By passing the hasError
prop to the DateRangePicker
component error styling will apply.
() => { const [dateRange, setDateRange] = React.useState(""); const changeHandler = (event) => { setDateRange(event.value); }; return ( <Stack> <DateRangePicker hasError value={dateRange} name="filter_date_range" id="filter_date_range" onChange={changeHandler} /> </Stack> ); };
By passing the disabled
prop to the DateRangePicker
component all related sub components will be non-interactive and disabled styling will apply.
() => { const [dateRange, setDateRange] = React.useState(""); const changeHandler = (event) => { setDateRange(event.value); }; return ( <Stack> <DateRangePicker disabled value={dateRange} name="filter_date_range" id="filter_date_range" onChange={changeHandler} /> </Stack> ); };
Preset date ranges" in a date range picker can provide users with quick access to commonly used date ranges, such as "This week" or "Last month" allowing them to save time and effort in selecting dates. This feature can also ensure consistency in date range selection across different parts of the application, enhancing the user experience and reducing user confusion.
- Today
- Tomorrow
- This week
- Last week
- This month
- Last month
- This quarter
- Last quarter
- Last 12 months
- Last 24 months
- This financial year
- Last financial year
If you find that the preset dropdown button is unnecessary or taking up valuable space, or if the filters are not serving any useful purpose, you may want to consider removing it using the showPresets
prop. This prop controls whether the preset dropdown button is displayed, and its default value is true
. However, setting it to false
will remove the button.
() => { const [dateRange, setDateRange] = React.useState(""); const changeHandler = (event) => { setDateRange(event.value); }; return ( <Stack> <DateRangePicker showPresets={false} value={dateRange} name="filter_date_range" id="filter_date_range" onChange={changeHandler} /> </Stack> ); };
If you want to provide the default value of the date range picker to be one of the presets rather then empty you can pass a defaultValueFromPreset
prop. The valid strings for this prop are snake_case
versions of their labels.
- Today ->
today
- Tomorrow ->
tomorrow
- This week ->
this_week
- Last week ->
last_week
- This month ->
this_month
- Last month ->
last_month
- This quarter ->
this_quarter
- Last quarter ->
last_quarter
- Last 12 months ->
last_12_months
- Last 24 months ->
last_24_months
- This financial year ->
this_financial_year
- Last financial year ->
last_financial_year
() => { const [dateRange, setDateRange] = React.useState(""); const changeHandler = (event) => { setDateRange(event.value); }; return ( <Stack> <DateRangePicker value={dateRange} name="filter_date_range" id="filter_date_range" onChange={changeHandler} defaultValueFromPreset="this_week" /> </Stack> ); };
Description
Sets the initial state of the range picker to be one of the presets
Type
"today" | "yesterday" | "this_week" | "last_week" | "this_month" | "last_month" | "this_quarter" | "last_quarter" | "past_12_months" | "past_24_months" | "this_financial_year" | "last_financial_year"
Description
Determines whether the user has the ability to interact with the input.
Type
boolean
Description
The label of the first DatePicker
Type
string
Default Value
From
Description
Adds additional styling to indicate failed validation.
Type
boolean
Description
The id of the input. Used to connect label to input. Unique ID to represent the calendar. ID is included and concatinated with sub-components such as the Selects, TextInputs and Buttons
Type
string
Description
The maximum year that the calendar widget can navigate to through the selects or the buttons
Type
number
Default Value
`currentYear + 5`
Description
The minimum year that the calendar widget can navigate to through the selects or the buttons
Type
number
Default Value
`currentYear - 5`
Description
The name of the text input. Submitted with its owning form as part of a name/value pair. Unique name to represent the calendar. name is included and concatinated with sub-components such as the Selects, TextInputs and Buttons
Type
string
Description
The OnChange function for the DateRangePicker. Returns an object with the following properties: - isValid: An array of booleans, where the first item is the validity of the first DatePicker and the second item is the validity of the second DatePicker. - errorMessage: An array of strings, where the first item is the error message of the first DatePicker and the second item is the error message of the second DatePicker. - textValue: An array of strings, where the first item is the text value of the first DatePicker and the second item is the text value of the second DatePicker. - value: An array of strings, where the first item is the value of the first DatePicker and the second item is the value of the second DatePicker.
Type
(event: OnChangeDateRangeReturnType) => void
Default Value
() => null
Description
Toggles the presets menu dropdown.
Type
boolean
Default Value
Description
The label of the second DatePicker
Type
string
Default Value
To
Description
The value of the Date range picker. Is processed as an array of strings. Where the first item of the array is the value of the first DatePicker and the second value the value of the second DatePicker. The Dates are in the string format of dd/MM/yyyy.
Type
[string, string]