Skip to content

Combo Box

A text input with a floating results dropdown, for a single searchable value. It is on the bag as shared.createSearchableComboBox and has no top level export. createComboBox is a plain field with suggestions, for a value that may be any text, in place of a <datalist>.

Decision

When Use
Searching a server for one value, such as a library Combo Box
Several values Chip Select
One value that must come from the list A Chip Select with single: true
A short list A plain select
Filtering a list on the page Search Field

Usage

var combo = shared.createSearchableComboBox({
    placeholder: 'Search a library',
    searchFn: shared.createDebouncedSearch('SourceLibraries', 300),
    onSelect: function (value, item) { selectedLibrary = value; }
});
container.appendChild(combo.element);

Classes: stolon-combo, stolon-combo-dropdown, stolon-combo-option. The handle is { element, getValue, setValue, destroy }.

States and Modifiers

  • searchFn(query, cb) is required and calls cb(results), where each result is a string or { value, label }.
  • shared.createDebouncedSearch(endpoint, delay) returns a searchFn. It needs at least 2 characters, waits delay ms (default 300), calls apiRequest(endpoint + '?query=...'), and passes the result array, or [] on error. Only the answer to the latest query arrives, and a shorter query drops one still due.
  • getValue() returns the chosen value, or the free text when nothing was chosen. Typing clears the earlier choice.
  • Arrow Down and Up highlight, Enter chooses, and Escape closes. Rows react to mousedown, so the input keeps focus.
  • The input is a combobox, the list is a listbox of option rows, and aria-activedescendant names the highlighted row.
  • A search icon sits at the input's trailing end, in a stolon-adorned stolon-adorned-search wrapper from Field Adornments.
  • The dropdown floats like the Overflow Menu list, fixed to the window and as wide as the input. It opens below the input, or above it with no room, stays on a 375px screen, and follows a scroll.
  • A search that finds nothing shows "No matches." (noResultsText), and a searchFn that throws shows "Could not search." (errorText). The sentence sits under the input, in the stolon-empty-inline style.
  • onInput() runs on each keystroke, after the earlier choice is cleared.
  • size is small, medium (default), or large, and sizes its fields on the size scale.
  • createComboBox({ value, suggestions, searchFn, placeholder, maxResults, size, errorText, onChange }) is a text field whose value is whatever is typed. Focus or typing lists the suggestions that match by label or value, as strings or { value, label }, and a pick puts the value in the field. It has no search icon and shows no sentence when nothing matches. onChange(value) runs after a pick or a keystroke, and the handle is { element, input, getValue, setValue, setSuggestions, destroy }.

Gotchas

  • The list opens only after typing, unlike the chip select, which also opens on focus.
  • A parent with a transform or a filter holds a fixed dropdown inside it, as it does the overflow menu.
  • setValue shows the value, not a label. Call destroy() when the page hides.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createComboBox

function createComboBox(options?: ComboBoxFieldOptions): ComboBoxFieldHandle

A text field with suggestions, for one value that may be anything typed. Focus or typing lists the suggestions that match, and a pick puts its value in the field. It shows no search icon and no sentence when nothing matches, since any text is a value, in place of a plain datalist.

Option Type What it does
value string | null the starting text [optional]
suggestions ComboBoxItem[] the values offered, as strings or { value, label } pairs, where a pick puts the value in the field [optional]
Handle member Type
input HTMLInputElement
getValue (): string
setValue (value: string | null | undefined): void
setSuggestions (list: ComboBoxItem[] | null | undefined): void
destroy (): void
Type What it is
ComboBoxMessages The sentences a searchable combo box shows under its input, besides ComboBoxOptions.
Class What it looks like
stolon-combo Text input wrapper with a floating results dropdown
stolon-combo-dropdown Floating list of options on the stolon-float surface, fixed to the viewport and as wide as the input, placed by the typeahead under the input, or above it with no room
stolon-combo-option One option row that tints in the accent on hover or when active

Example

Example Code

<!-- title: Combo Box -->
<!-- min-height: 260 -->
<div id="mount"></div>
<span id="comboStatus" class="stolon-status-text"></span>
<script type="module">
const base = await window.__baseReady;
const shared = base.createShared(document.getElementById('view'), 'preview-plugin', 'Preview');
const animals = ['Aardvark', 'Albatross', 'Beaver', 'Bison', 'Cheetah', 'Cobra', 'Dolphin', 'Eagle', 'Falcon'];
const combo = shared.createSearchableComboBox({
    placeholder: 'Search animals',
    searchFn: (query, cb) => cb(animals
        .filter((a) => !query || a.toLowerCase().indexOf(query.toLowerCase()) !== -1)
        .map((a) => ({ value: a.toLowerCase(), label: a }))),
    onSelect: (value) => shared.setStatus('comboStatus', 'Picked ' + value + '.', false, { timeout: 0 })
});
document.getElementById('mount').appendChild(combo.element);
</script>