pstanoev / simple-svelte-autocomplete

Simple Autocomplete / typeahead component for Svelte
http://simple-svelte-autocomplete.surge.sh/
MIT License
464 stars 78 forks source link

onChange should only trigger by user action #198

Open zogs opened 1 year ago

zogs commented 1 year ago

In my opinion onChange shoud not be trigger by a external assignment, only when user manually change the field

This special case that is causing me trouble : I need to persist the object in DB when user change the value, then the value gets update from the DB, but it's triggering a onChange event causing a recursive loop ...

Blitzlord commented 1 year ago

Similar issue here. onChange shouldn't trigger when the bound value is set externally

rornic commented 1 year ago

I'm running into the same issue.

In this case I'm usingbind:selectedItem in combination with onChange that causes a page reload, which causes onChange to fire again and enter a loop.

Happy to put a PR together if there's appetite from the maintainer for this change, @pstanoev?

In the meantime I've found a workaround for my issue using beforeChange instead of onChange in this manner:

<AutoComplete
    beforeChange={(oldItem, newItem) => {
        filter.selectedCountry = newItem;
        dispatch('change');
    }}
    bind:selectedItem={filter.selectedCountry}
    items={filter.countryOptions}
    labelFieldName="name"
    valueFieldName="iso_code"
/>

The call to dispatch('change') is what eventually triggers a page reload.

DavidBruant commented 5 months ago

Part of the problem is also in this issue : https://github.com/pstanoev/simple-svelte-autocomplete/issues/36