Fix: regression in option selected

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: https://github.com/sveltejs/svelte/issues/6873
pull/6874/head
raivaibhav 5 years ago
parent 883c47b45d
commit ace6bc0354

@ -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};`;

@ -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) {

Loading…
Cancel
Save