Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#184): improve switch event #190

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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