mskocik / svelecte

Flexible autocomplete/select component written in Svelte,
https://svelecte.vercel.app
MIT License
469 stars 42 forks source link

Svelecte component clears initial values after it is initially rendered in the DOM #183

Closed jereyi closed 8 months ago

jereyi commented 1 year ago

Hi,

I am encountering an issue with rendering the Svelecte component with an initial value. By adding some print statements, I discovered that the initial values passed to the component seems to be immediately overwritten with null values whenever the component is first rendered. Afterwards, it functions as expected, but it does not seem like this should be the default behavior. Could this be due to a bug with the component?

Thanks!

mskocik commented 1 year ago

Youy provided no example code, but is your issue the same as #168?

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 3 days.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 3 days.

leifwalsh commented 11 months ago

We have a page where the selected value comes from query parameters but the list of options comes from a backend API call to list what the valid ones are. I think what's going on is that on first render, we take this branch:

https://github.com/mskocik/svelecte/blob/master/src/lib/Svelecte.svelte#L399-L406

and then after the list of options gets updated to include the value from the query parameter, this "is the selected option valid" logic doesn't get re-evaluated. I think we can hack around this by binding the selection to a value that doesn't get "set" until the set of options is available, but maybe there's a way Svelecte can handle this sort of thing more gracefully?

mskocik commented 11 months ago

Can you provide working example? Or IF the list is provided with backend call, can't you just pass it as fetch parameter?

When fetch is set and there is no [query] string in search params, url will be requested and if you have initial value, Svelecte "will wait" for options to be populated to prevent the !success branch

mskocik commented 8 months ago

@leifwalsh and @jereyi I would like to fix this in v4, but it's impossible without reproduction example. Can you provide example in REPL or in sveltelab.dev

mskocik commented 8 months ago

Check in v4 if it's resolved, re-open otherwise. Thanks.