-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Fix regression in option selected #6874
Fix regression in option selected #6874
Conversation
This add the selected attr to the dom element and later using the hasAttribute it check if any of the option have selected atrribute then don't set selectedIndex to -1, this way it avoid setting the selectedIndex on mount Fixes: sveltejs#6873
b620b46
to
ace6bc0
Compare
The other way around to solve the issue was to add a check before the This PR is open for review, tests will be updated and added after the initial review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for tackling this! Could you add tests that would fail without this fix?
src/runtime/internal/dom.ts
Outdated
@@ -534,16 +534,17 @@ export function set_style(node, key, value, important) { | |||
} | |||
|
|||
export function select_option(select, value) { | |||
let count_selected_attr = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable is used as a boolean, so I suggest to make it a boolean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I Will revert 👍 after digging a little I figured there will be a bug with this logic, can you tell me what will the value of val
in this case https://svelte.dev/repl/d8faa8db7ce84059bb633dd969b429eb?version=3.44.0
when value is null in that selectedIndex set same as selected but in that that it should be -1.
src/runtime/internal/dom.ts
Outdated
@@ -534,17 +534,15 @@ export function set_style(node, key, value, important) { | |||
} | |||
|
|||
export function select_option(select, value) { | |||
let count_selected_attr = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this part of the code reverted because it will create a bug when value is null
or something similar like ""
@@ -556,6 +554,14 @@ export function select_options(select, value) { | |||
|
|||
export function select_value(select) { | |||
const selected_option = select.querySelector(':checked') || select.options[0]; | |||
if (select.selectedIndex === -1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
select_value
get's called when value is void 0
but before this get's called select_option from dom will get's called which will set the select index to -1, and we need the -1 value to make this #6126 work.
block.chunks.mount.push(b` | ||
(${data}.multiple ? @select_options : @select_option)(${this.var}, ${data}.value); | ||
('value' in ${data} && !${data}.multiple && @select_option(${this.var}, ${data}.value)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reason - selection option gets called even though the rest is something like {...{}}
which will again create a bug
71bd316
to
97344f4
Compare
Hey @dummdidumm do let me know if this PR needs any update |
The bug was fixed by #8371 |
This adds the selected attr to the dom element and later using the hasAttribute it checks if any of the option have selected attribute then don't set selectedIndex to -1, this way it avoids setting the selectedIndex on mount
Fixes: #6873
Before submitting the PR, please make sure you do the following
[feat]
,[fix]
,[chore]
, or[docs]
.Tests
npm test
and lint the project withnpm run lint