Skip to content

Commit

Permalink
fix(switch): reflect selected state in input event
Browse files Browse the repository at this point in the history
  • Loading branch information
vdegenne committed Feb 28, 2024
1 parent 274ce3e commit 8d201e0
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions switch/internal/switch.ts
Expand Up @@ -114,6 +114,7 @@ export class Switch extends switchBaseClass {
?checked=${this.selected}
?disabled=${this.disabled}
?required=${this.required}
@input=${this.handleInput}
@change=${this.handleChange} />
<md-focus-ring part="focus-ring" for="switch"></md-focus-ring>
Expand Down Expand Up @@ -190,9 +191,16 @@ export class Switch extends switchBaseClass {
return this.icons || this.showOnlySelectedIcon;
}

private handleInput(event: Event) {
const target = event.target as HTMLInputElement;
this.selected = target.checked;
// <input> 'input' event bubbles and is composed, don't re-dispatch it.
}

private handleChange(event: Event) {
const target = event.target as HTMLInputElement;
this.selected = target.checked;
// <input> 'change' event is not composed, re-dispatch it.
redispatchEvent(this, event);
}

Expand Down

0 comments on commit 8d201e0

Please sign in to comment.