Skip to content

df-select Component ​

df-select selects one or several values from static or loaded choices, and optionally accepts new values ("tags").

Basic Usage ​

Basic demo with a fixed options list:

Basic example
Selected value: cherry

Advanced Features ​

Choices loaded by fetchChoices with two-line text and icons, a debounced search, a "no data" text and an optional minimum query length:

Dynamic options loading
Selected value:

Integration with DynamicForms ​

df-select bound to @dynamicforms/vue-forms fields:

Person Form


Form Output
{
  "firstName": "John",
  "lastName": "Doe",
  "age": 30,
  "active": true,
  "country": "si"
}

Features ​

  • Single and multiple selection
  • Choices loaded through fetchChoices
  • Entry of new values (tagging)
  • Icons in choices
  • Chips for selected values. With multiple and allowNull, chips that wrap onto more rows put the clear button above the menu arrow; the layout follows the field's width.
  • A chip's close button is labelled Remove <text> and removes the chip on a click, Enter or Space; focus then moves to the close button of the next chip, or to the input when no closable chip remains
  • A null selection under allowNull, and an empty array for a multiple select
  • The clear button (under allowNull, while the select holds a value and accepts input) is a native button in the tab order while the menu is closed, named Clear <label>; a click, Enter or Space clears the value and focus moves to the input
  • The input is described (aria-describedby) by the chips' text, so the chosen value is read on focus
  • Search with filtering
  • A text in the menu when there are no choices to show, and a minimum length of the search text before choices are fetched for it
  • Choice text as plain text, markdown, HTML or a component

Keyboard ​

While the menu is open, the highlighted option is the option focused with ArrowDown and ArrowUp or, while text is typed into the input, the first option that matches it. The highlighted option is outlined in the theme's primary colour with the focus overlay; a chosen option shows the activated overlay without an outline. While the input has the focus, its aria-activedescendant names the highlighted option, as long as the menu renders it: the menu renders only the options in view. The menu opens with a fade and a grow; while it fades in, a click on it does nothing and does not reach the page under it. It closes without a transition.

KeySingle selectMultiple select
Tab on a highlighted optionPicks it, closes the menu; the focus moves to the next element, past the clear buttonAdds it when it is not chosen (never removes it), closes the menu; the focus moves to the next element, past the clear button
Tab without a highlighted optionCloses the menu; the value is unchanged and the focus moves to the next element, past the clear buttonSame as single
Enter on a highlighted optionPicks it, closes the menu; the focus stays in the inputToggles it: adds it, or removes it when it is chosen (without allowNull, the last choice stays); the menu stays open, and an option focused with the arrow keys keeps the focus
Shift+TabCloses the menu; the value is unchanged and the focus moves backSame as single
EscapeCloses the menu; the focus stays in the inputSame as single

Tab with the menu open moves the focus as the browser does from the input: the clear button is out of the tab order while the menu is open, and the chip close buttons precede the input. Tab from an option moves the focus from the input in the same way. Inside a dialog, Vuetify keeps the focus in the dialog: Tab on its last field moves the focus to its first element.

A key that confirms an option clears the typed text. A menu closed with one of these keys stays closed when choices loaded by fetchChoices arrive; typing, a click, ArrowDown, ArrowUp or Enter opens it. Keys of an IME composition (isComposing, or keyCode 229 right after the composition ends) are left to the input; a key with keyCode 229 outside a composition, such as one from a virtual keyboard, acts as it does without it.

With the menu closed, Tab and Shift+Tab move the focus to the next or previous element (the clear button, a chip's close button, the next field); focusing the input does not open the menu. ArrowDown and ArrowUp open it. The same keys apply with allowTags and with fetchChoices. With allowTags, the menu is closed while no option matches the typed text; Enter, Tab or leaving the field takes the text as a new value (a multiple select adds it).

Props ​

In addition to common props from InputBase, this component supports:

PropTypeDefaultDescription
choicesSelectChoice[]undefinedStatic list of choice options
fetchChoices(query?: any, idValue?: any) => Promise<SelectChoice[]>undefinedFunction to load choices dynamically
multiplebooleanfalseWhether multiple selection is allowed
allowTagsbooleanfalseWhether custom values can be entered
allowNullbooleantrueWhether the user can empty the selection. With false, the select offers no clear button and no close on the last chip of a multiple select, the menu option of that chip does not deselect it, and Backspace and Delete in the input do not remove a single select's choice or the last chip; it selects no default, and an empty value stays empty until the user selects a choice
noDataTextRenderContenttranslatable NoChoices ("No choices")Shown in the menu when there are no choices to show. While choices are being fetched, or a debounced fetch is waiting, the menu shows the translatable Loading instead. A select with allowTags opens no menu without choices
minQueryLengthnumber0With fetchChoices: the length the search text must reach before fetchChoices is called for it. See Dynamic Option Loading
minQueryTextRenderContenttranslatable MinQuery ("Type at least {count} characters", count = minQueryLength)Shown in the menu in place of noDataText while the search text is shorter than minQueryLength
fetchDebouncenumber0With fetchChoices: milliseconds the search text must stay unchanged before fetchChoices is called for it. Only the last search text is fetched. 0 calls it at once

SelectChoice Interface ​

Each choice implements this interface:

typescript
interface SelectChoice {
  id: any;        // Unique identifier/value for the option
  text: any;      // RenderContent, or a function answering it; any other value is shown as String(value)
  icon?: string;  // Optional vue-cached-icon name (ion-…, mdi-…, fa-…), URL or SVG
}

text is RenderContent: a string, an MdString, an HtmlString, a SimpleComponentDef, or a function answering one of them, so that it can follow the locale. A function is read on every render. null and undefined show as an empty text, and any other value, such as a number, as String(value).

The menu's option and the chip render the content. Where only text is taken, the select uses its text (renderContentText, of an MdString once rendered as markdown, so **Java** reads as "Java"): the title the select's search filters by, the read-only text field the select renders on the server and until it renders in the browser, and the label of a chip's close button. An option whose text is not a string is named by the text of its rendered content, a line break and a block read as a space: Java<br>Statically typed is named "Java Statically typed". update:modelValueDisplay carries the choices as they were given.

Value Format ​

The value depends on multiple:

  • When multiple: false (default):

    • Returns a single value (the id of the selected option)
    • Returns null when nothing is selected
  • When multiple: true:

    • Returns an array of values (the ids of selected options)
    • Closing the last chip leaves an empty array; the clear button (under allowNull) sets null

The select writes into the value only when the user edits the selection. Setting it up, mounting it, enabling it, loading choices and a value set from outside leave the value as it is: null stays null, an empty array stays an empty array, and a value the select would show differently - a single id for a multiple select, an array for a single one - is shown as the select needs it and written in that form only with the user's next edit.

An id of the value that no choice covers is shown with a choice whose text is the id, and stays in the value. An id that is empty or blank, such as '', gets no such choice: the select shows no selection for it, and in a multiple select the user's edits keep it in the value until the clear button empties the value. Once choices that cover an id are loaded or given, the select shows that choice instead. With allowTags, such an id is shown as it is.

Dynamic Option Loading ​

When using the fetchChoices prop instead of static choices, the component:

  1. Calls the function while the component is being set up, before it is mounted, in the browser only (see Server-side rendering)
  2. Calls the function when the user types in the search field
  3. Uses the search text as the query parameter
  4. Uses the ids of the value that no loaded choice covers as the idValue parameter - an array when multiple is set, a single id otherwise. This happens while the component is being set up and whenever the value changes from outside the component, also when the field is read-only. With an empty value, the setup call passes the value itself (null) as idValue and expects the unfiltered choices back.

Until that call resolves, no chip shows the ids. Once it has resolved, an id the call returned no choice for is shown with a choice whose text is the id, as with static choices. The value is unchanged either way. A call that rejects is logged with console.error and leaves the loaded choices as they are.

With minQueryLength, a search text shorter than it is not passed to the function: the loaded choices are reduced to those of the value and the selection, results of an earlier longer search text are dropped, and the menu shows minQueryText in place of noDataText when it has no choices to show. The setup call is not made for an empty value then; a taggable select's setup call that passes its value as idValue is made, and only the returned choices of the value are kept. Clearing the search text after a selection follows the same rule. Loading the choices for the value's ids is not affected.

With fetchDebounce, a search text is passed to the function once it has not changed for that many milliseconds, and only the last one is. An empty search text, the calls that load the choices for the value's ids and the setup call are made at once. A waiting call is dropped when the component unmounts or becomes read-only. The results of a search text are dropped once a later search text is fetched or cut short by minQueryLength.

typescript
const fetchChoices = async (query?: string, idValue?: any): Promise<SelectChoice[]> => {
  const response = await apiCall('/api/options', { search: query, ids: idValue });
  return response.data.map(item => ({
    id: item.id,
    text: item.name,
    icon: item.icon
  }));
};

Events ​

In addition to the common events of InputBase:

  • update:modelValueDisplay - Carries a SelectChoice[] holding the choices shown for the current value, including those whose text is the id. It is emitted while the component is being set up and whenever those choices change; not while the choices for the value's ids are being fetched, nor on the server for a select with fetchChoices

Slots ​

Two slots are forwarded to the underlying v-autocomplete / v-combobox, with the slot props Vuetify passes:

  • append-inner
  • prepend-inner

clear replaces the clear button. It is rendered where the button is: under allowNull, while the select accepts input, and shown while it holds a value. It takes Vuetify's clear slot props, with props holding the attributes of a clear button in the tab order: aria-label (Clear <label>, translatable key ClearField), onClick, onKeydown (Enter and Space clear, and the select does not see the key), onFocus / onBlur, and tabindex: -1 while the menu is open, so that Tab with the menu open passes the control. Clearing moves focus to the input.

vue
<df-select v-model="stop" :choices="stops" label="Stop">
  <template #clear="{ props }">
    <button type="button" class="my-clear" v-bind="props"><v-icon icon="mdi-close" /></button>
  </template>
</df-select>

Attributes ​

Attributes the application passes to df-select (aria-* among them) are set on the input, except class, style and data-*, which are set on the root element; id is the input's id. An aria-describedby is added to the input's own: the chips of the chosen choices and the message row.

Examples ​

Basic Single Selection ​

vue
<template>
  <df-select
    v-model="selectedFruit"
    :choices="fruits"
    label="Choose a fruit"
  />
</template>

<script setup>
import { ref } from 'vue';
import { DfSelect } from '@dynamicforms/vuetify-inputs';

const selectedFruit = ref('apple');
const fruits = [
  { id: 'apple', text: 'Apple' },
  { id: 'banana', text: 'Banana' },
  { id: 'cherry', text: 'Cherry' }
];
</script>

Multiple Selection with DynamicForms ​

vue
<template>
  <df-select
    :control="form.fields.categories"
    :choices="categories"
    :multiple="true"
    label="Select categories"
  />
</template>

<script setup>
import { Group, Field, ValidationError, Validators } from '@dynamicforms/vue-forms';
import { DfSelect } from '@dynamicforms/vuetify-inputs';

const categories = [
  { id: 1, text: 'Electronics', icon: 'ion-flash-outline' },
  { id: 2, text: 'Books', icon: 'ion-book-outline' },
  { id: 3, text: 'Clothing', icon: 'ion-shirt-outline' }
];

const form = new Group({
  categories: new Field({
    value: [1],
    validators: [
      new Validators.Validator((value) => {
        if (!value || value.length === 0) return [new ValidationError('app.no_category', {}, 'Select at least one category')];
        return null;
      })
    ]
  })
});
</script>

Released under the MIT License.