BookStack/resources/js/components/custom-checkbox.js

34 lines
983 B
JavaScript
Raw Normal View History

import {Component} from './component';
2022-11-15 20:44:57 +08:00
export class CustomCheckbox extends Component {
2022-11-15 20:44:57 +08:00
setup() {
this.container = this.$el;
this.checkbox = this.container.querySelector('input[type=checkbox]');
this.display = this.container.querySelector('[role="checkbox"]');
this.checkbox.addEventListener('change', this.stateChange.bind(this));
2022-11-15 20:44:57 +08:00
this.container.addEventListener('keydown', this.onKeyDown.bind(this));
}
onKeyDown(event) {
2022-11-15 20:44:57 +08:00
const isEnterOrSpace = event.key === ' ' || event.key === 'Enter';
if (isEnterOrSpace) {
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);
}
}