svelte-forms-lib icon indicating copy to clipboard operation
svelte-forms-lib copied to clipboard

Select values not auto-selected

Open michaelgaultjr opened this issue 3 years ago • 2 comments

Summary

Selects don't properly reflect the current form value, they pick the first <option>, rather than the <option> that matches the current form value.

Example Project

CodeSandbox Example Project

What is the current bug behavior?

The first <option> in the <select> is selected regardless of the current form value, and the initial value is used when submitting the form if the <select> isn't modified.

For example, we have the following

initialValues: {
  greeting: "goodbye"
}

...

<Select name="greeting">
  <option value="hello">Say Hello</option> <!-- This IS selected -->
  <option value="goodbye">Say Goodbye</option> <!-- This IS NOT selected -->
</Select>

The initial value is goodbye, however, the select shows that hello is selected. Without interacting with the select the submitted value is goodbye even though the value of the option that appears to be selected is hello

What is the expected correct behavior?

The <option> with the value of the current form value is selected.

This is the expected behavior compared the last example.

initialValues: {
  greeting: "goodbye"
}

...

<Select name="greeting">
  <option value="hello">Say Hello</option> <!-- This IS NOT selected -->
  <option value="goodbye">Say Goodbye</option> <!-- This IS selected -->
</Select>

michaelgaultjr avatar May 14 '21 22:05 michaelgaultjr

@thegamingninja thanks, this definitely looks like a bug. I don't see what's wrong with the current element, but it's likely to do with this line: https://github.com/tjinauyeung/svelte-forms-lib/blob/master/lib/components/Select.svelte#L12

What I'd recommend for now is creating your own Select wrapper, and using the 'native' HTML select in your component (you can use the key export from svelte-forms-lib to get the form context) until this is resolved - you should be able to replace the native select with the custom one here once this is resolved with little effort.

If you find anything interesting when switching to the native select it'd be much appreciated if your report your findings here!

larrybotha avatar May 17 '21 07:05 larrybotha

Alright, thanks for the workaround, I'll let you know if I find anything interesting!

michaelgaultjr avatar May 17 '21 15:05 michaelgaultjr