Dojo NG is being built in the open, one section at a time — follow along on Heptapod.

GUIDE

Choosing a select

Dojo NG has six controls for picking from a list. They differ in how many values the user can pick, whether the user can type, and whether the user can enter a value that is not in the list. This page says which one to use, and how each handles long lists and options from a server.

Submitted values

Nothing yet. Choose some values and submit.
A native form with live components. The submit handler prints FormData, with one fruit entry per chosen fruit.

Which one

ControlUse it whenValue
dj-native-selectThe list is short and the platform's own picker is fine, as on a phone.A string
dj-selectThe user picks one option from a list you style.A string
dj-select multipleThe user picks several options from a fixed list.A string array
dj-typeaheadThe list is long enough that typing to filter is faster than scrolling, or the options come from a search.A string
dj-chip-typeaheadThe user picks several values by typing, or enters new tags with allow-new.A string array
dj-search-boxThe text is a search query, not a choice from a list.The query

Use dj-select multiple when every allowed value is in the list. Use dj-chip-typeahead when the user may type a value of their own. dj-select never creates an option from typed text.

Long lists

dj-select, dj-typeahead, and dj-chip-typeahead show their options in a dj-list. Above 200 options the list renders only the rows in view, so 10,000 options open as fast as 50. Change the threshold with virtual-threshold on dj-list.

  • The arrow keys, Home, End, PageUp, and PageDown move through every option, rendered or not. Typing the first letters of a label jumps to it.
  • A screen reader hears the position in the whole list, such as "Option 812, 812 of 5,000".
  • Every row has the same height. Set --dj-list-item-height, or the list measures the first option. Long labels are cut with an ellipsis.
  • The popup opens with the selected option in view. It scrolls inside 20rem; change that with --dj-select-list-max-height or --dj-typeahead-list-max-height.
  • reorderable lists are not virtual: reordering turns off above the threshold.

Several values: dj-select multiple

With multiple, choosing an option toggles it and the list stays open. The selected values show as chips in the trigger. Each chip has a remove button, and Backspace on the trigger removes the last one. When the chips do not fit on one line, the rest collapse to "+N". The trigger's accessible name lists every selected value.

select-all adds a row that selects every enabled option, or clears them all. required means at least one value.

<form>
  <dj-select name="fruit" label="Fruit" multiple select-all required
    value='["apple","cherry"]'></dj-select>
</form>

<script type="module">
  import "@dojo-ng/select";
  document.querySelector("dj-select").options = [
    { value: "apple", label: "Apple" },
    { value: "banana", label: "Banana" },
    { value: "cherry", label: "Cherry" },
  ];
</script>

In a native form the select submits one entry per value, so new FormData(form).getAll("fruit") gives the array. dj-form puts the array under the name. A form reset returns to the value attribute.

Options from a server: remote

Set remote on dj-select or dj-typeahead and answer its requests. The component emits dj-options-request with { query, offset, epoch }, and you call setOptions(options, { epoch, total, append }).

const city = document.querySelector("dj-typeahead[remote]");

city.addEventListener("dj-options-request", async (event) => {
  const { query, offset, epoch } = event.detail;
  const res = await fetch(`/api/cities?q=${encodeURIComponent(query)}&offset=${offset}&limit=50`);
  const { items, total } = await res.json();
  // An answer to an older query has an older epoch; setOptions drops it and returns false.
  city.setOptions(items, { epoch, total, append: offset > 0 });
});
  • The typeahead asks when the user stops typing for request-delay milliseconds (200 by default). The select asks when it first opens with no options.
  • Each new query starts a new epoch. An answer with an older epoch is dropped, so a slow answer to "lis" never replaces the answer to "lisbon". You do not need to cancel requests.
  • When the user scrolls near the end, the component asks for the next page with offset set to the number of options loaded. Pass append: true for a page. Paging stops at total, or when a page comes back empty.
  • While a request is out, the list shows a spinner and a screen reader hears "Loading options", then the number of options.

A selected value may not be in the options you have loaded. Give its label in selectedOptions (the selected-options attribute takes JSON), and the trigger shows it. The component adds to selectedOptions as the user chooses.

<dj-select name="cities" label="Cities" remote multiple
  value='["c900"]'
  selected-options='[{"value":"c900","label":"Lisbon"}]'></dj-select>
Next: Forms & validation What each control submits, required and custom rules, and when errors show.