Overview
A custom select is a field that opens a list of options under it. The user picks one option, and the field shows its label. The field has three parts: a read-only text box for the label, a hidden input for the value, and a list of option buttons. An option can also hold an icon and a second line of text.
Pick another component when:
- the user types to search a long list: Autocomplete
- the user picks more than one option: Multiselect
- the design asks for the phone’s own picker: the native select in Text Fields
Markup
Variants
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Plain (default) | — | — | Options with a label only |
| Structure | Icon and description (id: rich) | canon #custom-select-rich |
— | Options with an icon and a second line, for choices that need a short explanation. See Option Content |
| Size | LG (default) | — | — | 40px high. It needs no class |
| Size | MD | .nds-md |
.nds-form-container |
32px high, with smaller text, for a table filter or a side panel |
| Style | Outline (default) | — | — | A border on the page background |
| Style | Lighter | .nds-lighter |
.nds-form-container |
A light fill and no border, for a field on a white card |
| Style | Darker | .nds-darker |
.nds-form-container |
A darker fill and no border, for a field on a gray surface |
| Saved value | Saved value (not: rich) (hint: The field opens with Makkah picked) | [value="makkah"] |
.nds-select-value |
The field opens with Makkah picked. See Saved Value |
| State (any) | Disabled | [data-state~="disabled"] |
.nds-select:not([data-state~="readonly"]) |
The user cannot open the list, and the value does not post. Not with Read-only |
| State (any) | Read-only | [data-state~="readonly"] |
.nds-select:not([data-state~="disabled"]) |
The user sees the value but cannot change it. The value posts. Not with Disabled |
| Validation | Required (hint: Press Validate with no option picked) | [data-required] |
.nds-select |
The form needs a pick. It does not submit while the field is empty |
| Field states | Label, info, feedback, required | — | — | Shared by every form field. See Text Fields |
Behavior
Option Content
An option holds its label in .nds-label inside .nds-option-text. A .nds-description beside the label adds a second line, and an icon before .nds-option-text sits on the label’s line. The field shows the label only, never the description or the icon.
Saved Value
To show a saved value, write it in the value of .nds-select-value, and leave the text box empty. When the page loads, the script finds the option with the same data-value and writes its label in the text box. When the list opens, that option shows as picked.
Disabled and Read-only
data-state~="disabled" on .nds-select disables both inputs, so the user cannot open the list and the value does not post. With data-state~="readonly", the list does not open by mouse or by keyboard. The value still posts.
Built-in Features
-
Auto-initialization
Every
.nds-select-inputworks with no setup. The script builds a field's list the first time the user focuses it. -
Form Value
The hidden
.nds-select-valueholds the picked option'sdata-valueand posts with the form. The text box shows the label and does not post. -
Keyboard Support
Enter, Space and the arrow keys open the list on the picked option. The arrow keys move through the options, and Escape closes the list and returns focus to the field.
-
Required Check
With
data-required, the form checks the hidden value at submit. An empty field shows "Please select an option" in the page language and blocks the submit. -
List Position
The list opens under the field, as wide as the field. In a modal or a scrolling box, it moves to
<body>, so nothing clips it.
Best Practices
- Use a custom select for a single choice in a form, whatever the number of options. A native select beside styled fields does not match them.
- Put the
nameon.nds-select-valueonly. - Write
readonlyon.nds-select-input. It stops the user from typing in the text box. - Write
hiddenon.nds-select-dropdown, so the list stays closed until the script builds it. - Give each option a unique
data-value.setValue()and a saved value pick the first option that matches. - Put
data-requiredon.nds-select, not on the text box. The form reads the hidden value. - Keep a description to one short line.
- Write
aria-hidden="true"on an option’s icon. The label names the option. - To change the value from a script, use
setValue()orclear(). They update the label, the value and theselectedstate together. - For the label, info text, feedback and the required mark, see Text Fields. They work the same on every field.
API
Other Classes
| Class | Element | Effect |
|---|---|---|
.nds-select-menu |
.nds-select-dropdown |
The script adds it when it builds the list. Style the list with it: it stays on the list when the list moves to <body> |
.nds-dropmenu, .nds-dropmenu-trigger, .nds-dropmenu-menu, .nds-dropmenu-scroll, .nds-dropmenu-item |
.nds-form-control, .nds-select-input, .nds-select-dropdown, .nds-select-options, each .nds-select-option, in the same order |
The script adds them when it builds the list, so Dropmenu opens, places and closes it. Do not write them |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-required |
.nds-select |
The form needs a pick. It does not submit while .nds-select-value is empty |
data-error-message |
.nds-select |
Replaces the message the required check shows |
data-state~="disabled", data-state~="readonly" |
.nds-select |
Set it yourself. See Disabled and Read-only |
data-state~="open" |
.nds-select |
The script sets it when the list opens, and removes it when the list closes. It turns the arrow |
role="combobox" |
.nds-select-input |
The script sets it when it builds the list. Dropmenu then sets aria-expanded on it |
hidden |
.nds-select-dropdown |
Write it. The script removes it when it builds the list |
data-value |
.nds-select-option |
The value the option writes in .nds-select-value |
data-state~="selected" |
.nds-select-option |
The script sets it on the picked option, and removes it from the others, at each pick, setValue() and clear(). Do not write it: write the value in .nds-select-value |
CSS Custom Properties
| Property | Default | Controls |
|---|---|---|
--dropmenu-min-width |
100% |
The least width of the list. 100% is the width of the field. Set it on .nds-select. The other Dropmenu width properties work here too |
Keyboard
| Key | Where | Effect |
|---|---|---|
| Enter, Space | text box | Opens or closes the list, and focuses the picked option or the first one |
| Arrow Down, Arrow Up | text box | Opens the list, and focuses the picked option, or the first or last one |
| Arrow Down, Arrow Up, Home, End | list | Moves through the options |
| Enter, Space | an option | Picks it, closes the list and returns focus to the text box |
| Escape | list | Closes the list and returns focus to the text box |
JavaScript
el can be the .nds-select-input, an element that holds it, such as its .nds-form-control or .nds-form-container, its id or a selector.
| Method | Effect |
|---|---|
NDS.CustomSelect.init() |
Writes the label of every saved value. The first call also starts to watch the page for focus and picks. The loader calls it on load. Call it again after you add a field with a saved value |
NDS.CustomSelect.create(el) |
Builds the list of one field now, instead of at its first focus. A built field is left as it is. Call it, or focus the field, before a script opens it with click(): a click with no focus does not open an unbuilt field |
NDS.CustomSelect.getValue(el) |
Returns the data-value of the picked option, or '' when none is picked or the field is not found |
NDS.CustomSelect.setValue(el, 'makkah') |
Picks the option with that data-value, the same as a click, and returns true. It returns false, and changes nothing, when no option has the value. It works before the list is built |
NDS.CustomSelect.clear(el) |
Empties the text box and the value, removes the selected state, and returns true. It returns false when the field is not found |
formControl.ndsCustomSelect.getValue(), .setValue(value), .clear() |
The same, on a field that has built its list |
NDS.CustomSelect.destroy(el) |
Undoes create() for one field: pass the input or its field. The list is hidden again and the field builds again at its next focus. Call it before you remove the field from the page |
NDS.CustomSelect.validate(el, options) |
Checks the field and returns { valid, message, value }. Pass { showMessage: false } to check without a message |
| Event | Fired on | Detail |
|---|---|---|
nds:custom-select:change |
.nds-form-control. It does not bubble |
{ value, text }: the picked option’s data-value and label. Empty strings after clear(). It fires at each pick, setValue() and clear() |
input, change |
.nds-select-input and .nds-select-value, and they bubble |
Native events, with no detail. They fire with nds:custom-select:change |
nds:dropmenu:opened, nds:dropmenu:closed |
.nds-form-control, and they bubble |
The list opened or closed. See Dropmenu |
The full API is in the banner of _js/nds-custom-select.js.