DatePicker
The DatePicker component is used to select a date value. At mobile viewports, the browsers native input is used.
Import
- React
- Angular
- Vue.js
// with @dhl-official/react-library:
import { DhlDatePicker } from "@dhl-official/react-library"
// with @dhl-official/ui-libraries/react-library:
import { DhlDatePicker } from "@dhl-official/ui-libraries/react-library"
If the DUIL has been installed, you can use the web component directly:
<dhl-date-picker></dhl-date-picker>
// with @dhl-official/vue-library:
import { DhlDatePicker } from "@dhl-official/vue-library"
// with @dhl-official/ui-libraries/vue-library:
import { DhlDatePicker } from "@dhl-official/ui-libraries/vue-library"
Please ensure all date values are passed in the international ISO-8601 format: YYYY-MM-DD.
Accessibility
The DatePicker is built with consideration for accessibility standards and keyboard support.
Choose Date Button
Space, Enter: Toggles DatePicker visibility and moves focus to month and year text.
DatePicker Dialog
Esc: Close DatePicker dialog and move focus to "choose date" button.Tab: Move focus to next focusable element.Shift + Tab: Move focus to previous focusable element
The calendar used inside DatePicker uses role="grid" meaning only one 'day button' is in the tab sequence at any one time.
DatePicker Dialog: Calendar Grid
Space, Enter: Selects a date, closes the dialog, and moves focus back to the “Choose Date” button.- Additionally updates the value of the Duet Date Picker input with the selected date, and adds selected date to “Choose Date” button label.
Arrow up: Moves focus to the same day of the previous week.Arrow down: Moves focus to the same day of the next week.Arrow right: Moves focus to the next day.Arrow left: Moves focus to the previous day.Home: Moves focus to the first day (e.g Monday) of the current week.End: Moves focus to the last day (e.g. Sunday) of the current week.Page Up: Changes the grid of dates to the previous month and sets focus on the same day of the same week.Shift + Page Up: Changes the grid of dates to the previous year and sets focus on the same day of the same week.Page Down: Changes the grid of dates to the next month and sets focus on the same day of the same week.Shift + Page Down: Changes the grid of dates to the next year and sets focus on the same day of the same week.
Please ensure the prop localeLabels is supplied and matches the locale language. This ensures screen-reader users can
leverage voice-over technology in the context of DatePicker.
Using Events
The DatePicker can be used with DOM events and listeners. Additionally, custom events are available (see Events table below)
which include a custom detail object. This object contains fields designed to increase ease of use. The example below demonstrates
how the detail object can be used to retrieve the value as a date through the valueAsDate field.
// Select the DatePicker component
const date = document.querySelector("dhl-date-picker");
// Listen for when date is selected
date.addEventListener("dhlChange", function (e) {
console.log("selected date", e.detail.valueAsDate);
});
With an output:
Tue Feb 14 2023 00:00:00 GMT+0000 (Greenwich Mean Time)
Using Custom Formats
By default, the DatePicker uses the yyyy-mm-dd (DE locale) format.
To use a different format, the prop dateAdapter is available for use. The example below demonstrates how the prop dateAdapter can be used with a en-GB locale format DD-MM-YYYY.
const picker = document.querySelector("dhl-date-picker");
const DATE_FORMAT = /^(\d{1,2})\.(\d{1,2})\.(\d{4})$/;
picker.dateAdapter = {
parse(value = "", createDate) {
const matches = value.match(DATE_FORMAT);
if (matches) {
return createDate(matches[3], matches[2], matches[1]);
}
},
format(date) {
const day = date.getDate().toString().padStart(2, "0"); // Get the day of the month as a two-digit string
const month = (date.getMonth() + 1).toString().padStart(2, "0"); // Get the month as a two-digit string
const year = date.getFullYear().toString(); // Get the year as a string
return `${day}/${month}/${year}`;
},
};