Events
Events | Description |
---|---|
on_change | (optional) will be called on a date change event. Returns an object . See Returned Object below. |
on_type | (optional) will be called on every input and date picker interaction. Returns an object . See Returned Object below. |
on_submit | (optional) will be called once a user presses the submit button. |
on_cancel | (optional) will be called once a user presses the cancel button. |
on_reset | (optional) will be called once a user presses the reset button. |
on_show | (optional) will be called once date-picker is visible. |
on_hide | (optional) will be called once date-picker is hidden. |
on_days_render | (optional) will be called right before every new calendar view gets rendered. See the example above. |
onBlur | (optional) will be called once the input lose focus. |
Returned Object
The type of native event will depend on the interaction. All additional HTML attributes will be returned as well.
{date: null|'like return_format', /* Gets returned if range is false */start_date: null|'like return_format',end_date: null|'like return_format',days_between: number,attributes: { attributes },event: null|{ native event }}
Validation during input changes
In order to validate dates during typing, you can make use of is_valid
or is_valid_start_date
and is_valid_end_date
. Because the user can change a date in the input field, and the on_type
event will then return a falsy is_valid
.
Additional event return object properties:
{is_valid: boolean, /* Gets returned if range is false */is_valid_start_date: boolean,is_valid_end_date: boolean,...}
Min & Max date
If min_date
or max_date
is given, the return object also contains info about if the start_date
or end_date
is in between the given limits. The reason is that the user can still enter an invalid date in the input.
{is_valid_start_date: boolean,is_valid_end_date: boolean,...}
Manipulate the days in the calendar view
The callback event on_days_render
gives you the possibility to manipulate the "day" object, before it gets rendered. This callback will be called many times. Both on the first render, and on every user interaction, like hover and selection, etc. This means you have to ensure a performant date calculation.
Please use date-fns to make the calculations.
<DatePicker on_days_render={(days, calendarNumber = 0) => { return days.map((dayObject) => { if (isWeekend(dayObject.date)) { dayObject.isInactive = true dayObject.className = 'dnb-date-picker__day--weekend' // custom css } return dayObject }) }} />
The dayObject
object contains:
[{date: Date,// Vanilla JavaScript Date objectclassName: // define your custom css classesisInactive: boolean,// shows it as disabled onlyisDisabled: boolean,// shows it as disabled and with a strikethroughisPreview: boolean,// date is between startDate (exclusive) and hoverDate (inclusive)isSelectable: boolean,// if not last and next month and not disabled – handles z-indexisStartDate: boolean,// date selected is start dateisEndDate: boolean,// date selected is end dateisToday: boolean,isWithinSelection: boolean,// date is between selection rangeisNextMonth: boolean,// used for selection and inactive calculationisLastMonth: boolean,// used for selection and inactive calculation},...]