Skip to content

Commit

Permalink
Fix: regression in option selected
Browse files Browse the repository at this point in the history
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: #6873
  • Loading branch information
RaiVaibhav committed Oct 22, 2021
1 parent 883c47b commit ace6bc0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/compiler/compile/render_dom/wrappers/Element/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ export default class AttributeWrapper extends BaseAttributeWrapper {
block.chunks.hydrate.push(
b`${element.var}.${property_name} = ${init};`
);
const is_selected = property_name === 'selected';
const is_boolean = typeof init.value === 'boolean';
const attr_val = is_boolean ? x`""` : init;
if (is_selected) {
block.chunks.hydrate.push(
b`${method}(${element.var}, "${name}", ${attr_val});`
);
}
updater = block.renderer.options.dev
? b`@prop_dev(${element.var}, "${property_name}", ${should_cache ? this.last : value});`
: b`${element.var}.${property_name} = ${should_cache ? this.last : value};`;
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,16 +534,17 @@ export function set_style(node, key, value, important) {
}

export function select_option(select, value) {
let have_select_attr = false;
for (let i = 0; i < select.options.length; i += 1) {
const option = select.options[i];

if (option.hasAttribute('selected')) have_select_attr = true;
if (option.__value === value) {
option.selected = true;
return;
}
}

select.selectedIndex = -1; // no option should be selected
if (!have_select_attr) select.selectedIndex = -1; // no option should be selected
}

export function select_options(select, value) {
Expand Down

0 comments on commit ace6bc0

Please sign in to comment.