DatePicker (code)
Allows users to enter a date. This can be done by either having the fill out the date via an input, but also provides the user with a calendar view for easier date selection.
Props
OnChange
's function is slightly different than that from . Instead of returning the input field's value, we return 2 arguments.
- The first argument is the JavaScript Date object of the currently selected date. However, this value can be in two cases: if a user manually inputs a date that is either not yet complete (e.g. ) or inputs an invalid date (e.g. ).
- The second argument is an object that contains more information about the
selected date. The current properties of this object are:
- : This will be true if the currently selected date is inside your range of (see below).
- : This is a string representation of the currently selected date, formatted by the prop.
DisabledDays
To prevent a user from selecting certain dates, you can supply the prop. This prop can be either:
- A object. This will disable the specific day.
- A range object with and keys to match a range of days.
- An object with a and/or key to match the days before and/or after the given date.
- An object with a array to match specific days of week.
- A function taking the day as first argument and .
- An array of the above.
Date object
This example disables a single day.
Range object
In this example, a range, described by and keys, is disabled.
Before
When providing an object with a key, all dates before that date (not including the date itself) are disabled.
After
When providing an object with an key, all dates after that date (not including the date itself) are disabled.
Before and after
The and keys can be combined to create a range of "allowedDates". The next example will only allow a single week.
Days of week
An array of numbers corresponding to days to be disabled (Note: starting on Sunday and zero-index, so Sunday is 0). The example below disabled every Friday and every Sunday.
Function
The function takes the day as the first argument. If the function returns , the day is disabled. The example below disables every day that is not even.
Array
An can be used to combine any number of the above options. The example below disables all days except a single week, and in that week, Wednesday, Saturday and Sunday are disabled.
Mask & format
To visually aid the user with the expected format, you can use an input mask. Input masks will display a template of the expected value. The mask is built out of several definitions:
- : numeric
- : alphabetical
- : alphanumeric
Linked to the input mask, is the format of the date. The available tokens for parsing date are:
- Two-digit year (e.g. 18)
- Four-digit year (e.g. 2018)
- Month, beginning at 1 (e.g. 1-12)
- Month, 2-digits (e.g. 01-12)
- The abbreviated month name (e.g. Jan-Dec)
- The full month name (e.g. January-December)
- Day of month (e.g. 1-31)
- Day of month, 2-digits (e.g. 01-31)
- Hours (e.g. 0-23)
- Hours, 2-digits (e.g. 00-23)
- Hours, 12-hour clock (e.g. 1-12)
- Hours, 12-hour clock, 2-digits (e.g. 01-12)
- Minutes (e.g. 0-59)
- Minutes, 2-digits (e.g. 00-59)
- Seconds (e.g. 0-59)
- Seconds, 2-digits (e.g. 00-59)
- Hundreds of milliseconds, 1-digit (e.g. 0-9)
- Tens of milliseconds, 2-digits (e.g. 00-99)
- Milliseconds, 3-digits (e.g. 000-999)
- Offset from UTC (e.g. -05:00)
- Compact offset from UTC, 2-digits (e.g. -0500)
- Post or ante meridiem, upper-case (e.g. AM PM)
- Post or ante meridiem, lower-case (e.g. am pm)
- Day of Month with ordinal (e.g. 1st... 31st)
Note: It is important that if you change one of these values, that the other value is still compliant. Setting a mask of won't make much sense if your is still on default .
Localization
DatePicker has localization built in. This built-in localization may be different between brands, but this is logic that is provided by Chameleon so that you as a developer don't have to worry about it.
In the example below, you can see the localization by properties such as the first day of the week, the short names of the days, ...
Error
To convey an error to the user, you can set the prop to true. This can come in handy to indicate an invalid date, or if the user would type in a date that would be outside of your date range.
In the example below, try removing a character or set the date to an impossible date (e.g. 30/02/2020).
Inline
By default, the calendar will be closed and only opens when clicking the calender icon. You can have the calendar open by default and act as an inline element by passing in the prop.
Disabled
Marking a DatePicker as disabled, will disable all interaction with the element.
OptionalLabel (Required fields)
By default the DatePicker is always required. To make a DatePicker optional you can add the prop.
With Chameleon we take a specific approach where we encourage marking optional fields, not required. The majority of fields in a form are always required. Indicating that fields are required make users more fearful, it increases the risk of errors and reduces the form completion rate.
PickerInputOnly
If you still want full functionality of the DatePicker, but don't want users to be able to manually enter a date, you can pass in the prop.
Label Props
As with other input elements, also has a wide range of extra properties to convey even more meaning.