Number Selector
A Number Selector allows the user to add or remove quantities incrementally.
- Standard Number Selector
- Examples
- Usage
- Examples
- Developer Notes
- Right-to-Left Support
- Accessibility Notes
Standard Number Selector
When to Use
- Use a Number Selector when users are most likely to increase or decrease a value by a small range of increments. For example, when adding passengers to a travel booking.
When Not to Use
- If the user is selecting from a larger range of values consider a Select or Text Input Field instead.
Examples
- Standard (shown above)
- Number Selector with Hidden Label
- Number Selector Error States
Usage
- In general, the Number Selector input width should be wide enough to comfortably fit the label, but not so wide as to render the Number Selector’s increase/decrease controls too far from the value.
- The Number Selector should have a default value.
Examples
Number Selector with Hidden Label
This example of the Number Selector may be used when the Number Selector is part of a repetitive grouping, such as a column in a table.
Number Selector Error States
Developer Notes
A Number Selector is a simple combination of two buttons and a number <input>
element. The button to decrement the number should have a class of spark-number-selector__down
. Similarly, the button to increment the number should have a class of spark-number-selector__up
. The Number Selector allows you to provide additional parameters using the following attributes on the input
element: min
, max
, value
, and step
. If providing these, it is important to verify that the values initially provided for min
, max
, and value
are multiples of the step
value.
Use of the Number Selector requires a JavaScript helper. It can be instantiated using new Spark.NumberSelector(el)
. It implements the Messaging and Validation mixins, and provides mechanisms for getting and setting the value.
The following options may be passed when using new Spark.NumberSelector(el, {});
:
For all available methods and full API, refer to the component code on Bitbucket.
Standard Number Selector
Number Selector with Hidden Label
Number Selector Error States
Right-to-Left Support
Accessibility Notes
The Number Selector component requires additional steps when included within a product to make it fully accessible to assistive technologies. This is to be done at the product level as the context and language requirements will vary between different products, and as such this functionality is not included within the Spark library.
The label
element requires a for
attribute that identifies which element it is bound to, in this case the input
element. The id
of the input
should match the for
attribute of the label
.
Each of the buttons in the selector has an aria-label
attribute to provide alternative text as the visible portion of the buttons simply show a ‘+’ or ‘-‘. The buttons also have aria-controls
attributes to indicate which element they control. The value of these attributes is the id
of the input
element.
The input
also has additional ARIA attributes, some of which can change based on the current status of the component. The aria-live
attribute which has been set to ‘assertive’ to provide immediate feedback as the value of the input is changed using the button controls or the value is manually typed into the field. Along with aria-invalid
, the aria-describedby
attribute indicates which element will provide additional context for the field when there is an error. In this example, this attribute is added or removed using Javascript depending on whether there is an error or not. The value of the aria-describedby
attribute is the same value as the id
of the element that holds the error message that gets displayed. The role
attribute is applied to the error message element and is used to announce the message as it’s displayed. Refer to the JS tab below to see an example of how this functionality could be included.