2023-04-18 17:20:02 -04:00
|
|
|
import {Component} from './component';
|
2019-06-04 05:45:44 -04:00
|
|
|
|
2022-11-15 07:44:57 -05:00
|
|
|
export class CustomCheckbox extends Component {
|
2019-06-04 05:45:44 -04:00
|
|
|
|
2022-11-15 07:44:57 -05:00
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
|
|
|
this.checkbox = this.container.querySelector('input[type=checkbox]');
|
|
|
|
this.display = this.container.querySelector('[role="checkbox"]');
|
2019-06-04 05:45:44 -04:00
|
|
|
|
|
|
|
this.checkbox.addEventListener('change', this.stateChange.bind(this));
|
2022-11-15 07:44:57 -05:00
|
|
|
this.container.addEventListener('keydown', this.onKeyDown.bind(this));
|
2019-06-04 05:45:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
onKeyDown(event) {
|
2022-11-15 07:44:57 -05:00
|
|
|
const isEnterOrSpace = event.key === ' ' || event.key === 'Enter';
|
|
|
|
if (isEnterOrSpace) {
|
2019-06-04 05:45:44 -04:00
|
|
|
event.preventDefault();
|
|
|
|
this.toggle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toggle() {
|
|
|
|
this.checkbox.checked = !this.checkbox.checked;
|
|
|
|
this.checkbox.dispatchEvent(new Event('change'));
|
|
|
|
this.stateChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
stateChange() {
|
|
|
|
const checked = this.checkbox.checked ? 'true' : 'false';
|
|
|
|
this.display.setAttribute('aria-checked', checked);
|
|
|
|
}
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
}
|