Skip to content

[Autocomplete][LiveComponent] Select box doesn't reset when used in a live component #1500

@pierredup

Description

@pierredup

When using the Autocomplete component in a form as part of a LiveComponent, the field is not reset when re-rendering the component (E.G when submitting the form). The previous selected values are still selected.

Here is an example using a multi-select dropdown, with a custom query builder that removes the selected items from the list:

Screen.Recording.2024-02-15.at.15.21.08.mov

When changing the form to an Autocomplete type, and selecting items, it doesn't get removed from the select

Screen.Recording.2024-02-15.at.15.24.12.mov

Currently in order to reset the autocomplete dropdown, I'm emitting a custom browser event with a custom controller on the select with the following which seems to work fine:

import { Controller } from '@hotwired/stimulus';
import TomSelect from 'tom-select';
import { TomInput, TomSettings } from 'tom-select/dist/types/types';

/* stimulusFetch: 'lazy' */
export default class extends Controller {
    connect(): void {
        document.addEventListener('select:clear', (): void => {
            if ((this.element as TomInput).tomselect) {
                const settings: TomSettings|undefined = (this.element as TomInput).tomselect?.settings;
                (this.element as TomInput).tomselect?.destroy();
                new TomSelect(<TomInput>this.element, (settings as TomSettings));
            }
        });
    }
}

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