-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Select becomes blank when value is provided #139
Comments
Oh, it turns out the values are the problem. Making them strings works. I used numbers because the values are coming from a database. |
I looked at this more closely. The problem is that this library uses the DOM to get the given option-value. Unfortunately getting the value from the DOM means the value looses it's original type and is coerced to a string. svelte-forms-lib/lib/create-form.js Line 97 in 57e8250
Svelte stores the original value of the option in a property called https://svelte.dev/repl/626573ae7c6d47398aa3ee1173ae746b?version=3.43.0 function handleChange(e) {
const target = e.target;
let value;
if (target.multiple) {
value = Array.from(target.querySelectorAll('option:checked')).map(o => o.__value);
} else {
value = target.querySelector('option:checked')?.__value;
}
console.log(value);
} Would you be ok with this change? If so, I can create a PR. |
@iluuu1994 thanks for your feedback! Ye, I've experienced this, too. I was surprised at first, but this appears to be the way the DOM works, so I worked around the issue with strings, too 🤷 IIRC, values in DOM inputs are always strings, even
Feel free to give this a bash if you'd like! I'm inclined to augment the DOM as little as I can and treat Svelte as a light abstraction on top of what the DOM already does, but there's potentially an elegant solution here! |
Do you have a specific idea? For the |
@iluuu1994 on thinking more about this, a decorator on a per-element basis seems to be the good way to handle this. It may be a little more work for users (although a decorator would only need to be written once), doesn't add any complexity to // untested
<script>
const {handleChange, ...} = createForm(...)
function handleSelectChange(event) {
const {value: inputValue} = event.currentTarget
const maybeInt = Number.parseInt(inputValue, 10)
const value = !Number.isNaN(maybeInt)
? maybeInt
: inputValue
// mutate the event object directly
Object.assign(event, {target: {value}})
handleChange(event)
}
</script>
<form ...>
<select on:change={handleSelectChange}>
<option value="123">my option</option>
</select>
</form> I'm gonna close this out, as strings are the expected type for |
Thank you for this library!
Summary
Consider this example:
When selecting a different elementin the dropdown the select field becomes blank. Interestingly the element is still selected in the store because when the form is submitted the right value is returned.
Example Project
Of course, it works fine on codesandbox for some reason 😒 I don't understand why.
https://codesandbox.io/s/svelte-forms-lib-select-empty-8ctpk?file=/App.svelte
So I've created a small sample repository.
https://github.com/iluuu1994/svelte-forms-lib-select-error
What is the current bug behavior?
The selected element becomes blank.
What is the expected correct behavior?
The selected element should remain the one I've selected.
The text was updated successfully, but these errors were encountered: