logo dile-components dile-components

dile-input-suggestions

Free text field with a list of suggestions that appears while the user types. It follows the WAI-ARIA APG editable combobox ("list autocomplete") pattern: the value of the field is always the typed text, and choosing a suggestion copies its text into the field and emits dile-input-suggestions-selected with the whole object, so you decide what to do with it.

Use it when the user can type anything but you want to help with known values (product names, cities, previous concepts...). If the user must choose one of the values, use dile-select-search or dile-select-ajax-overlay instead.

The suggestions list is a dile-select-overlay anchored to the field, so it shares its keyboard behavior, styles and CSS parts.

There is an Ajax variant, dile-ajax-input-suggestions, in @dile/crud.

Installation

npm i @dile/ui

Usage

Import the component.

import '@dile/ui/components/input-suggestions/input-suggestions.js';

Use the component, giving it the suggestions as an array of objects. They are filtered by displayProperty, ignoring case and accents.

<dile-input-suggestions
  name="concept"
  label="Concept"
  .suggestions=${[{ id: 1, name: 'Web hosting' }, { id: 2, name: 'Domain name' }]}
></dile-input-suggestions>

suggestions can also be a function (keyword) => array | Promise<array> that decides what to return for the typed text.

Chosen suggestion: -

<script type="module">
  import '@dile/ui/components/input-suggestions/input-suggestions.js';
  const field = document.getElementById('suggestionsDemo');
  field.suggestions = [
    { id: 1, name: 'Web hosting' },
    { id: 2, name: 'Domain name' },
    { id: 3, name: 'Domain renewal' },
    { id: 4, name: 'Maintenance' },
  ];
  field.addEventListener('dile-input-suggestions-selected', (e) => {
    document.getElementById('suggestionsDemoOutput').textContent = JSON.stringify(e.detail.item);
  });
</script>
<dile-input-suggestions id="suggestionsDemo" name="concept" label="Concept" minLength="1" delay="100"></dile-input-suggestions>
<p>Chosen suggestion: <span id="suggestionsDemoOutput">-</span></p>

Keyboard

Only suggestions highlighted with the keyboard are chosen with Enter, never the one under the mouse pointer.

Properties

Methods

Events

Slots

Give an accessible name to interactive content (like an icon button), and hide decorative icons from screen readers with aria-hidden="true".

<dile-input-suggestions name="concept" label="Concept">
  <span slot="prefix" aria-hidden="true"><dile-lucide-icon-tag></dile-lucide-icon-tag></span>
  <button type="button" slot="suffix" aria-label="Search in the catalog" @click=${this.openCatalog}>
    <dile-lucide-icon-search></dile-lucide-icon-search>
  </button>
</dile-input-suggestions>

Extending

DileInputSuggestionsBase (exported from @dile/ui/components/input-suggestions/index.js) holds the behavior. Subclasses implement loadData(), which gets the suggestions for this.keyword and calls this.setResults(data, requestId). It also offers extension points, used by dile-input-tags: renderBeforeField(), renderFieldEnd(), filterResults(data), onEnterWithoutSuggestion(e) and selectItem(item).

Styling

The field uses the usual --dile-input-* custom properties and the list the --dile-select-overlay-* ones. Besides:

Custom property Description Default
--dile-input-suggestions-min-width Minimum width of the suggestions list when the field is narrower 16rem
--dile-input-suggestions-margin-bottom Bottom margin of the component 10px

The option, option-highlighted, option-selected and option-content parts of the list are exposed by the component.