Actions and inputs
Combobox
A text field that suggests values from a list as the person types. <kui-combobox> turns an input and its <datalist> into the WAI-ARIA combobox with a list popup. Without JavaScript the same markup is the browser’s own suggestion list.
Examples
A city, with suggestions
Type to filter. Case and accents do not matter: “cacak” finds Čačak and “sabac” finds Šabac. The part that matches is underlined. A value not in the list is allowed.
Start typing, then choose from the list or keep what you typed.
<div class="kui-field">
<label class="kui-label" for="combobox-city">City</label>
<kui-combobox class="kui-combobox">
<input class="kui-input" id="combobox-city" name="city" list="combobox-cities" autocomplete="off" aria-describedby="combobox-city-hint">
<datalist id="combobox-cities">
<option value="Beograd"></option>
<option value="Novi Sad"></option>
<option value="Niš"></option>
<option value="Kragujevac"></option>
<option value="Čačak"></option>
<option value="Šabac"></option>
<option value="Užice"></option>
<option value="Zrenjanin"></option>
<option value="Požarevac"></option>
<option value="Kruševac"></option>
<option value="Smederevo"></option>
<option value="Leskovac"></option>
</datalist>
</kui-combobox>
<p class="kui-hint" id="combobox-city-hint">Start typing, then choose from the list or keep what you typed.</p>
</div>
Only values from the list
With strict, a value that is not one of the options is marked invalid when the field loses focus. data-keywords lets an option be found by other words: “Germany” and “Nemačka” both find Deutschland.
<div class="kui-field">
<label class="kui-label" for="combobox-country">Country of the company</label>
<kui-combobox class="kui-combobox" strict>
<input class="kui-input" id="combobox-country" name="country" list="combobox-countries" autocomplete="off" aria-describedby="combobox-country-e">
<datalist id="combobox-countries">
<option value="Srbija" data-keywords="Serbia Србија"></option>
<option value="Deutschland" data-keywords="Germany Nemačka"></option>
<option value="Österreich" data-keywords="Austria Austrija"></option>
<option value="Schweiz" data-keywords="Switzerland Švajcarska"></option>
<option value="Sverige" data-keywords="Sweden Švedska"></option>
<option value="Italia" data-keywords="Italy Italija"></option>
<option value="中国" data-keywords="China Kina"></option>
</datalist>
</kui-combobox>
<p class="kui-error-text" id="combobox-country-e"></p>
</div>
States
Every state
Closed, the field looks like a text input. Open, the list sits under it; the marked option has a ring and a tinted row, the chosen one is bold with a tick. When nothing matches, the list says “No results” from the kit’s words. A disabled field does not open.
Choose a country from the list.
<div class="kui-form kui-form--wide">
<div class="kui-form__row">
<div class="kui-field"><label class="kui-label" for="combobox-s1">Filled</label><kui-combobox class="kui-combobox"><input class="kui-input" id="combobox-s1" list="combobox-s-list" value="Novi Sad" autocomplete="off"><datalist id="combobox-s-list"><option value="Beograd"></option><option value="Novi Sad"></option><option value="Niš"></option></datalist></kui-combobox></div>
<div class="kui-field"><label class="kui-label" for="combobox-s2">Disabled</label><kui-combobox class="kui-combobox"><input class="kui-input" id="combobox-s2" list="combobox-s-list2" value="Beograd" disabled autocomplete="off"><datalist id="combobox-s-list2"><option value="Beograd"></option></datalist></kui-combobox></div>
<div class="kui-field"><label class="kui-label" for="combobox-s3">Invalid</label><kui-combobox class="kui-combobox" strict><input class="kui-input" id="combobox-s3" list="combobox-s-list3" value="Atlantis" aria-invalid="true" aria-describedby="combobox-s3-e" autocomplete="off"><datalist id="combobox-s-list3"><option value="Srbija"></option><option value="Sverige"></option></datalist></kui-combobox><p class="kui-error-text" id="combobox-s3-e"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#circle-alert"></use></svg><span>Choose a country from the list.</span></p></div>
</div>
</div>
Classes and attributes
kui-combobox.kui-combobox- Wraps the input. The list comes from the datalist named by the input’s list attribute, or from a datalist inside.
datalist > option value- One suggestion. label sets other text to show; data-keywords adds words that also find it.
strict- Only a value from the list is valid; anything else is marked aria-invalid when the field loses focus.
.kui-listbox- The list the element draws under the field, with role="listbox" and one role="option" per suggestion.
event kui-select- Fired with detail.value when an option is chosen; input and change fire on the field as well.
Keyboard
- Down
- Opens the list and marks the first option, or marks the next one.
- Alt + Down
- Opens the list without marking an option.
- Up
- Marks the previous option.
- Enter
- Takes the marked option and closes the list.
- Escape
- Closes the list; a second Escape clears the field.
- Tab
- Closes the list and moves on, keeping what was typed.
Accessibility
- The input has role="combobox", aria-expanded, aria-controls and aria-activedescendant, as the WAI-ARIA combobox pattern asks; focus stays in the field.
- The number of suggestions is announced after each key, from the kit’s words (“5 results”).
- The list is labelled by the field’s label, and every option is at least 44 pixels high on a phone.
- A press outside the field closes the list.
Do and do not
Do
- Use it when the list is too long for a select, or when a value not on the list is also fine.
- Keep suggestions short and distinct at the start, so a few letters are enough.
- Add data-keywords for other names a person might type.
Do not
- Do not use it for fewer than about 10 options; a select or radios are simpler.
- Do not fetch suggestions on each key without a pause.
- Do not choose a value for the person when they leave the field.