fix(select): do not dispatch "change" event on initial load (#1356)

Follow-up to #1353

If `selected` is `undefined` or not provided, the "change" event will be dispatched since the first `SelectItem` value will be set as the default.

The "change" event should only be dispatched when the value is actually changed.
This commit is contained in:
metonym 2022-06-18 12:27:27 -07:00 committed by GitHub
commit 511d7c24e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -108,9 +108,16 @@
selectedValue.set(value); selectedValue.set(value);
}; };
let prevSelected = undefined;
afterUpdate(() => { afterUpdate(() => {
selected = $selectedValue; selected = $selectedValue;
dispatch("change", $selectedValue);
if (prevSelected !== undefined && selected !== prevSelected) {
dispatch("change", $selectedValue);
}
prevSelected = selected;
}); });
$: errorId = `error-${id}`; $: errorId = `error-${id}`;