Skip to content

Commit

Permalink
fix(#184): improve switch event
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-prinz-mw committed Jul 11, 2023
1 parent a4eb405 commit 3678a85
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions mwui-stencil/src/components/mw-switch/mw-switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ export class MwSwitch {
*/
@Event({
bubbles: true,
cancelable: false,
composed: false,
composed: true,
})
emitter: EventEmitter;
emitter: EventEmitter<boolean>;

private toggleSwitch(event: Event & { path: unknown[] }): void {
(event.target as HTMLInputElement).blur();
this.checked = this.checkbox.checked;
this.emitter.emit(event);
}
private toggleSwitch = (event: Event): void => {
if (!this.disabled) {
(event.target as HTMLInputElement).blur();
this.checked = !this.checked;
this.emitter.emit(this.checked);
}
};

private hasLabel: boolean;
private hasOnOffLabel: boolean;
Expand All @@ -59,8 +60,8 @@ export class MwSwitch {
render() {
return (
<Host>
<label test-id={this.testId} onClick={this.toggleSwitch.bind(this)} class="switch">
<input disabled={this.disabled} ref={(el: HTMLInputElement) => (this.checkbox = el)} type="checkbox" checked={this.checked} />
<label test-id={this.testId} class="switch">
<input disabled={this.disabled} type="checkbox" onInput={this.toggleSwitch} checked={this.checked} />
<span class="slider round"></span>
</label>
{this.hasLabel && <span class="label">{this.label}</span>}
Expand Down

0 comments on commit 3678a85

Please sign in to comment.