Date Selection
Date Selection allows users to specify a single date or a range of dates.
- Text Input with Calendar - Single Date Example
- Examples
- Examples
- Text Input Optimized for Localization
- Text Input With Typeahead
- Text Input with Calendar - Date Range
- Text Input with Calendar with Advance by Month/Year
- Text Input with Inline Calendar
- Select (Dropdown) - Single Date Example
- Select (Dropdown) - Date Range Selection
- Text Input w/ Error
- Text Input w/ Error - Date Range
- Developer Notes
- Configurable Settings
- Text Input with Calendar - Single Date
- Text Input with Inline Calendar
- Text Input Optimized for Localization
- Text Input with Typeahead
- Text Input with Calendar - Date Range
- Text Input with Calendar - Single Selection with Advance by Month/Year
- Select (Dropdown) - Single Date Selection
- Select (Dropdown) - Date Range Selection
- Internationalization and Configuration
- Text Input w/ Error
- Text Input w/ Error - Date Range
- Accessibility Notes
Text Input with Calendar - Single Date Example
This date selection allows users to either type a date or access a calendar for selecting a date.
When to Use
- When it is helpful for users to see a calendar. For example, when the corresponding day of the week and/or visual display of a calendar is relevant to the selection the user is making.
Examples
- Text Input with Calendar - Single Date Example (shown above)
- Text Input with Single Date Calendar - Optimized for Localization Example
- Text Input with Typeahead Example
- Text Input with Calendar - Date Range Example
- Text Input with Calendar with Advance by Month/Year Example
Examples
Text Input Optimized for Localization
This example automatically changes to display the name of the month after the user types a numeric value or selects a date from the calendar.
When to Use
- This version is recommended when localization is a concern and/or when there may be confusion between months and days within a numeric-only date display.
When Not to Use
- When a sense of the span of time (number of days, weeks) is helpful to users.
Text Input With Typeahead
This date selection allows users to type a numerically-formatted date. It may be used with or without a calendar.
When to Use
- Use when the visual display of a calendar is not relevant to the date being entered.
Text Input with Calendar - Date Range
This date selection allows users to type a range of dates into a sequence of text input fields or access a calendar within each field to specify a date range. The text input fields may be separate fields (using the example above) or contiguous fields (shown below).
When to Use
- When it is helpful for users to see a calendar. For example, when the corresponding days of the week and/or visual display of a calendar is relevant to the selection the user is making.
Text Input with Calendar with Advance by Month/Year
This date selection allows users to either type a date or access a calendar for selecting a date. In addition, it allows users to advance by month or year using a select dropdown within the context of the calendar display.
When to Use
- Use this when it is likely that the user will want to advance by year and the display of a calendar is relevant to the decision the user is making. If a calendar is not helpful to the decision (for example, date of birth), a simple type-ahead text input or select input is recommended.
Text Input with Inline Calendar
This variation will show the calendar (by default) underneath the input field. It is embedded within the page rather than in a Popover.
When to Use
- When a Text Input with Calendar is inside a Popover or positioned at the bottom of a Modal causing the calendar to be cut off from the user’s view. View the Simple Form Modal to see this component in action.
- If within a small field set or feature where the Inline Calendar requires additional visual weight or the visual prominence is preferred.
Select (Dropdown) - Single Date Example
When to Use
- To select a date that is in the distant past or future. For example, date of birth.
- To select a date when it is not necessary for the user to see a calendar view.
When Not to Use
- When a user is likely to want to see the corresponding day of the week.
- When a sense of the span of time (number of days, weeks) is helpful to users.
Select (Dropdown) - Date Range Selection
When to Use
To specify a start and end date range when viewing a calendar is not deemed necessary. For example, specifying start and end dates when filtering a large data set. In this scenario, a simple select with pre-determined ranges may also be considered (View Current Month, Current Year, Last Year, etc.).
Text Input w/ Error
Text Input w/ Error - Date Range
Developer Notes
Configurable Settings
When working with the Spark Date Selection component, all instances are backed by an HTML input
with type="date"
. Depending on which variant you are using, additional inputs will be created to capture the user’s input. Changes to these inputs will update the value in the original type="date"
input.
The following options may be passed when using new Spark.DateInput(el, {});
:
For all available methods and full API, refer to the component code on Bitbucket.
Text Input with Calendar - Single Date
Date Input components can be augmented by including a Calendar Popover component. In addition to creating a date typeahead using the techniques described above, calendar functionality can be added to an input by creating an instance of the Calendar Popover Javascript component by calling new Spark.CalendarPopover(el)
.
This component will listen for clicks on an element with a class of spark-date__calendar-toggle
to show and hide a calendar popover.
The following options may be passed when using new CalendarPopover(el, {});
:
Text Input with Inline Calendar
Date Input components can be augmented by including an Inline Calendar component. In addition to creating a date typeahead using the techniques described above, calendar functionality can be added to an input by creating an instance of the Inline Calendar Javascript component by calling new Spark.CalendarInline(el)
.
The following options may be passed when using new CalendarInline(el, {});
:
Text Input Optimized for Localization
Use data-show-date-as-text
in spark-input__field
to make this available. The format of the date text is MM DD YYYY
.
Text Input with Typeahead
The Date Input component can also transform an input with type="date"
into a series of text typeaheads. First, add a class of spark-date
and to a regular Spark Input. Next, create an instance of the Date Input JavaScript component by calling new Spark.DateInput(el)
.
Text Input with Calendar - Date Range
When creating a range of inputs linked through a calendar, simply include multiple elements when instantiating the Javascript helper: new Spark.CalendarPopover([el1, el2])
.
Text Input with Calendar - Single Selection with Advance by Month/Year
When the quickJump
option is passed to the Calendar Popover constructor, the month and year are replaced with select inputs. How far a user can jump around in time is determined by the min
and max
value of the corresponding input. If no min
or max
is set, the maximum and minimum dates will be 50 years ahead and behind the current date selected.
Select (Dropdown) - Single Date Selection
When working with the Spark Date Selection component, all inputs are backed by an HTML input with type="date"
. Depending on which variation of the date selection you choose, additional inputs will be created to capture the user’s input. Changes to these inputs will update the value in the original type="date"
input. You can see this behavior in action below.
Select (Dropdown) - Date Range Selection
The Date Input component can transform an ordinary type="date"
input into a series of select inputs. First, add a class of spark-date
and spark-date--select
to a regular Spark Input. Next, create an instance of the Date Input JavaScript component by calling new Spark.DateInput(el)
. If min
and/or max
dates are provided, only the year dropdown will be limited to the supplied dates.
Internationalization and Configuration
It is possible to configure both the names of the months and the names of the days of the week. setMonthNames
, setMonthNamesShort
, setDayNames
and setDayNamesShort
are methods available on the dateHelper
object. For more details on the dateHelper
object’s API, refer to the code on Bitbucket.
The day to use as the first of the week can also be configured. The default value is 0
for Sunday, but calling dateHelper.setWeekStartsOn()
can change this behavior. Passing a value of 1
would set Monday to be the first day of the week. 2
would be Tuesday and so on.
Text Input w/ Error
Text Input w/ Error - Date Range
Accessibility Notes
Text Input Date Selection
The text input variation of the Date Input component creates three fields used to display the month, day and year of the date entered. As these fields are generated on the fly using Javascript, aria-label
attributes are also created to ensure that these fields have accessible labels that can be read by screen-readers and assistive devices. The value of this label is a combination of the label set on the Date Input’s spark-label
element and the field’s date part e.g. Departure Month, where “Departure” is the label set on the component and “Month” is one of the three fields created, the others being “Day” and “Year.”
Select (Dropdown) Date Selection
The Select (Dropdown) variation of the Date Input functions in a similar manner as the text input variation above in that additional fields and labels are created to display the month, day, and year select dropdowns. In order to dynamically create the appropriate labels for each of the individual select dropdowns, it is important to set an id
attribute on the spark-label element of the Date Input. This will then create a label similar to that of the text input variation above for each of the individual fields e.g. “Departure Month” or “Departure Year.”
Calendar Popover
The Calendar Popover inherits some functionality from the Popover component, but also includes additional enhancements to ensure the contents of the Calendar are accessible to assistive technologies.
When using the Date Input component that has a Calendar, ensure that the spark-date__calendar-toggle
element that toggles the calendar has a tabindex="0"
attribute. This ensures that the toggle can be accessed via keyboard and the calendar displayed by hitting the Enter key when focused on it. It is also necessary to add an aria-label
property to this element that informs non-sighted users that only available dates will be accessible on the calendar. This is because inactive or unavailable dates are skipped over when navigating via keyboard.
The controls within the calendar such as the arrow navigation buttons and dates have additional ARIA attributes added to make the content accessible. These attributes include aria-label
and aria-hidden
, and will provide additional information for a date e.g. announcing the full date or identify a date as an inactive date and prevent its selection. This functionality is handled by Javascript and will not need to be set as part of instantiating the component.
Inline Calendar
The Inline Calendar includes enhancements to ensure the contents of the Calendar are accessible to assistive technologies.
The controls within the calendar such as the arrow navigation buttons and dates have additional ARIA attributes added to make the content accessible. These attributes include aria-label
and aria-hidden
, and will provide additional information for a date e.g. announcing the full date or identify a date as an inactive date and prevent its selection. This functionality is handled by Javascript and will not need to be set as part of instantiating the component.
The navigation of the Inline Calendar uses a combination of tab controls to navigate the months and keyboard arrow controls to navigate the dates within that month.