Events
| Property | Type | Description |
|---|---|---|
onChange | function | (optional) Will be called on a date change event. Returns an object. See Returned Object below. |
onType | function | (optional) Will be called on every input and date picker interaction. Returns an object. See Returned Object below. |
onSubmit | function | (optional) Will be called once a user presses the submit button. |
onCancel | function | (optional) Will be called once a user presses the cancel button. |
onReset | function | (optional) Will be called once a user presses the reset button. |
onShow | function | (optional) Will be called once date-picker is visible. |
onHide | function | (optional) Will be called once date-picker is hidden. |
onDaysRender | function | (optional) Will be called right before every new calendar view gets rendered. See the example above. |
onFocus | function | (optional) Will be called once the input gets focus. |
onBlur | function | (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 | 'date as `returnFormat` | `yyyy-MM-dd` ', /* Available if `range` is `false` */startDate: null | 'date as `returnFormat` | `yyyy-MM-dd`', /* Available if `range` is `true` */endDate: null | 'date as `returnFormat` | `yyyy-MM-dd`', /* Available if `range` is `true` */invalidDate: null | 'date as `returnFormat` | `yyyy-MM-dd`', /* Available if `range` is `false` */invalidStartDate: null | 'date as `returnFormat` | ´yyyy-MM-dd`', /* Available if `range` is `true` */invalidEndDate: null | 'date as `returnFormat` | `yyyy-MM-dd`', /* Available if `range` is `true` */partialDate: null | 'date as `returnFormat` | `yyyy-MM-dd`' /* Available if `range` is `false` */partialStartDate: null | 'date as `returnFormat` | `yyyy-MM-dd`' /* Available if `range` is `true` */partialEndDate: null | 'date as `returnFormat` | `yyyy-MM-dd`' /* Available if `range` is `true` */daysBetween: number,attributes: { attributes },event: null | { native event }}
Validation during input changes
In order to validate dates during typing, you can make use of isValid or isValidStartDate and isValidEndDate. Because the user can change a date in the input field, and the onType event will then return a falsy isValid.
Additional event return object properties:
{isValid: boolean, /* Available if `range` is `false` */isValidStartDate: boolean, /* Available if `range` is `true` */isValidEndDate: boolean, /* Available if `range` is `true` */}
Min & Max date
If minDate or maxDate is given, the return object also contains info about if the startDate or endDate is in between the given limits. The reason is that the user can still enter an invalid date in the input.
{isValidStartDate: boolean,isValidEndDate: boolean,...}
Manipulate the days in the calendar view
The callback event onDaysRender 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 onDaysRender={(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},...]