Skip to content

Enhanced select does not get reset when removing selected entry #777

@ch-ncolley

Description

@ch-ncolley

Code used:

const $select = document.querySelector("select");
window.accessibleAutocomplete.enhanceSelectElement({
    selectElement: $select
});

Steps to reproduce:

  1. Enter a value in the autocomplete input
  2. Select an option
  3. Notice that the underlying select element is updated
  4. Select the value in the autocomplete input and delete it
  5. Click outside the input
  6. Notice that the underlying select element is still as was selected on step 3

Expected behaviour:
If the input is empty all options are unselected.

This is what I ended up doing to work around this:

const $select = document.querySelector("select");
window.accessibleAutocomplete.enhanceSelectElement({
    selectElement: $select,
    onConfirm: (query) => {
        const $wrapper = document.getElementById($select.id).parentNode;
        const $input = $wrapper.querySelector("input");
        const inputEmpty = $input.value === "";

        const selectOptions = Array.from($select.options);
        const requestedOption = selectOptions.filter(option => {
            return (option.textContent || option.innerText) === query;
        })[0];

        if (requestedOption) {
            console.log("Selected", requestedOption.value);
            requestedOption.selected = true;
        } else if (inputEmpty) {
            console.log("Reset the select");
            selectOptions.forEach(option => (option.selected = false));
        }
    }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions