NUI-Widgets documentation has moved to Storybook hosted on Github Pages.

DatePicker

There is a breaking change to this component in the next version of NUI Widgets.
  • The readOnly prop will behave differently in a future version of NUI Widgets. The Button will no longer appear in readOnly DatePickers.
  • The disabledDates prop will behave differently in a future version of NUI Widgets. It will not be treated as a date range. It will be an array of single disabled dates.
  • The validationMessage.state prop will be limited to warning and error to align with other components in a future version of NUI Widgets.
import DatePicker from '@concur/nui-widgets/lib/DatePicker/DatePicker';

The DatePicker component allows the entry of dates, validation of date strings, and the formatted display of dates. It uses an <input> element for date entry and display, and includes a calendar popover for selecting a date using the keyboard or mouse. It’s based on the Fundamental React DatePicker component. We recommend preparing to migrate to this DatePicker from the current DatePicker, as we plan to adopt more Fundamental React components under the hood in the future. This DatePicker addresses various accessibility issues, and will be the component to move forward with as we adopt SAP’s Fiori guidelines.

The HTML autocomplete attribute for the input element is enabled by default meaning browsers will try and provide suggestions for input fields. SAP Concur components that provide dropdowns or popovers to assist with populating an input field attempt to disable the browser's suggestions. Please open an issue if you are seeing browser suggestion behavior for these components. See the support page for more information.

Examples

Default

The following demonstrates a default DatePicker with no customizations. For a full set of examples, see the main Fundamental React site.

<DatePicker buttonLabel='Open Calendar' />

Date Format

The dateFormat property is useful for formatting and validating dates. It is also shown as the placeholder for the <input> element. Formats are those allowed by the Moment.js project.

If the dateFormat property is set to one of the following: MM/DD/YYYY, DD/MM/YYYY, YYYY/MM/DD (including using . or - as separators), any valid keyed input dates will be automatically formatted to match the supplied dateFormat property. For example, with the dateFormat property set to MM/DD/YYYY, keyed entries of 4/1/20, 04.1.2020, 4-01-20, etc will be reformatted to 04/01/2020 when onBlur is triggered by leaving the input field.

<DatePicker buttonLabel='Open Calendar' />

<DatePicker buttonLabel='Open Calendar' dateFormat='DD-MM-YYYY' />

<DatePicker buttonLabel='Open Calendar' dateFormat='YYYY.MM.DD' />

Date Range

The [enableRangeSelection] property is useful for allowing the selection of a range of dates.

<DatePicker enableRangeSelection />

Disabled State

Set the disabled property to change the DatePicker’s state to disabled.

<DatePicker buttonLabel='Open Calendar' disabled />

ReadOnly State

Set the readOnly property to change the DatePicker’s state to readOnly. A read-only field cannot be modified. However, a user can tab to it, highlight it, and copy the text from it.

<DatePicker buttonLabel='Open Calendar' readOnly />

Today Button

<DatePicker
    buttonLabel='Open Calendar'
    localizedText={{
        calendarInstructions: 'Use arrow keys to move between dates.',
        nextMonth: 'Next month',
        previousMonth: 'Previous month',
        show12NextYears: 'Show 12 next years',
        show12PreviousYears: 'Show 12 previous years'
    }}
    todayAction={{
        type: 'select',
        label: 'Today'
    }} />

Validation States

<DatePicker validationState={{ state: 'success', text: 'Test validation message' }} />

<DatePicker validationState={{ state: 'error', text: 'Test validation message' }} />

<DatePicker validationState={{ state: 'warning', text: 'Test validation message' }} />

<DatePicker validationState={{ state: 'information', text: 'Test validation message' }} />

Keyboard Shortcuts

When the DatePicker popover is open and contains focus, it is possible to quickly navigate through days, weeks, months, and years using the following shortcuts:

Key Function
Tab Cycle keyboard focus forward through Datepicker popover elements - header (previous view button, month view toggle button, year view toggle button, next view button), date/month/year grid, today button if visible; in that order
Shift + Tab Cycle keyboard focus backward through Datepicker popover elements - header (previous view button, month view toggle button, year view toggle button, next view button), date/month/year grid, today button if visible; in reverse order
Space, Enter Select currently focused date (and close popup), month, or year.
Up Arrow Moves focus to the same day of the previous week.
Down Arrow Moves focus to the same day of the next week.
Right Arrow Moves focus to the next day.
Left Arrow Moves focus to the previous day.
Home Moves focus to the first day of the current week.
End Moves focus to the last day of the current week.
Page Up Changes the grid of dates to the previous month and sets focus on the same day of the previous month. If that day does not exist, then moves focus to the last day of the previous month.
Shift + Page Up Changes the grid of dates to the previous year and sets focus on the same day of the previous year. If that day does not exist, then moves focus to the last day of the same month of the previous year.
Page Down Changes the grid of dates to the next month and sets focus on the same day of the next month. If that day does not exist, then moves focus to the last day of the next month.
Shift + Page Down Changes the grid of dates to the next year and sets focus on the same day of the next year. If that day does not exist, then moves focus to the last day of the same month of the next year.

Usage

Properties

Property Type Default Description
buttonLabel String Required Localized assistive text for the calendar icon/button.
buttonProps Object   Properties to pass through to the calendar button.
calendarProps String   Properties to pass through to the calendar popover.
className String   Custom classes applied to the wrapping <div> tag.
compact Boolean   Show in compact mode.
controlId String   Sets the id attribute of the <input>.
dateFormat String 'MM/DD/YYYY' Format to use for displaying the inputted or selected date e.g. "YYYY.M.D", "DD-MM-YYYY", "MM/DD/YYYY" etc. This overrides the date format derived from any set locale.
disabled Boolean false Controls whether the component is enabled or disabled.
disableAfterDate Date   Disables dates of a calendar that come after the specified date.
disableBeforeDate Date   Disables dates of a calendar that come before the specified date.
disabledDates Array   Array of Date objects that cannot be selected.
enableRangeSelection Boolean false Set to true to enable the selection of a date range (begin and end).
flipContainer Element or Element[]   Used to create a boundary for the underlying Popper to stay within.
initialValue String   Value to show in the <input> element when rendered.
highlightDates Moment[]   An array of dates to highlight.
inputProps Object   Properties to pass through to the <input> element.
locale String 'en' Provides a locale for date formatting and validation. Locales are those supported by the Moment.js project.
localizedText Object   Contains all the localized text strings for the component. See Localized Text for details.
placement String or String[]   Placement of the popover relative to the target element. Options are 'auto', 'auto-start', 'auto-end', 'top', 'top-start', 'top-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end', 'right', 'right-start', 'right-end'. If you use an array of strings, the first item in the array is the default placement and the following ones are chosen in order as fallback placements if the preceding ones don’t work.
popoverProps Object   Any extra props to pass through to the underlying popover.
readOnly Boolean false Adds the readonly attribute to the <input> element.
required Boolean false Adds the required attribute to the <input> element. NOTE: This does not perform any validation, but rather just marks the field as required.
size String 'lg' Size of input group. Options are 'sm', 'md', and 'lg'.
todayAction Object   A configuration object for ‘Today’ action.
todayAction.type should be one of the String values ‘none’ or ‘select’ and todayAction.label should be the localized label for the action button
e.g.,{ type: 'select', label: 'Today'}.
validationMessage Object   An object identifying a validation message. The object will include properties for state and text; e.g., { state: 'warning', text: 'This is your last warning' }.

Callbacks

Property Parameters Description
onBlur Object {date, formattedDate, isoFormattedDate} Callback function for onBlur events. In the object returned, date is the date object, formattedDate is the formatted date, and isoFormattedDate is the date formatted in ISO-8601 format (YYYY-MM-DD). NOTE: Likely the best callback to use to perform any custom validation. If the date is invalid, date will be null, formattedDate will be 'BAD DATE', and isoFormattedDate will be 'Invalid date'.
onChange Object {date, formattedDate, isoFormattedDate} Callback function for onChange events. In the object returned, date is the date object, formattedDate is the formatted date, and isoFormattedDate is the date formatted in ISO-8601 format (YYYY-MM-DD). NOTE: Called upon every keystroke.
onDatePickerClose Object {date, formattedDate, isoFormattedDate} Callback function for closing the popover. In the object returned, date is the date object, formattedDate is the formatted date, and isoFormattedDate is the date formatted in ISO-8601 format (YYYY-MM-DD).
onFocus Object {date, formattedDate, isoFormattedDate} Callback function for onFocus events. In the object returned, date is the date object, formattedDate is the formatted date, and isoFormattedDate is the date formatted in ISO-8601 format (YYYY-MM-DD).

Localized Text

Property Type Default Description
dayInstructions String 'Use arrow keys to move between days.' Localized string informing screen reader users the calendar can be navigated by arrow keys while in day view.
monthInstructions String 'Use arrow keys to move between months.' Localized string informing screen reader users the calendar can be navigated by arrow keys while in month view.
yearInstructions String 'Use arrow keys to move between years.' Localized string informing screen reader users the calendar can be navigated by arrow keys while in year view.
rangeInstructions String 'First date selected. Please select a second date.' Localized string informing screen reader users to select a second date when in range selection.
nextMonth String 'Next month' Localized string for the aria-label of the next button while in day view.
previousMonth String 'Previous month' Localized string for the aria-label of the previous button while in day view.
show12NextYears String 'Show 12 next years' Localized string for the aria-label of the next button while in year view.
show12PreviousYears String 'Show 12 previous years' Localized string for the aria-label of the previous button while in year view.
todayLabel String 'Today' Localized text for the today button.