salix/front/core/components/toggle/index.js

40 lines
962 B
JavaScript
Raw Normal View History

import ngModule from '../../module';
2019-10-09 22:47:29 +00:00
import FormInput from '../form-input';
import './style.scss';
/**
* Base component with common logic and styles for checkbox and radio button.
*
* @property {Boolean} checked Whether the checkbox is checked
*/
2019-10-09 22:47:29 +00:00
export default class Toggle extends FormInput {
constructor($element, $) {
super($element, $);
2019-10-24 08:17:32 +00:00
this.initTabIndex();
this.element.addEventListener('click', e => this.onClick(e));
this.element.addEventListener('keydown', e => this.onKeydown(e));
}
onKeydown(event) {
if (!event.defaultPrevented && event.key == ' ')
2019-10-24 08:17:32 +00:00
this.element.click();
}
onClick(event) {
2019-10-23 15:38:35 +00:00
if (!this.editable || event.defaultPrevented)
return true;
}
change(value) {
this.$.$applyAsync();
super.change(value);
}
}
ngModule.vnComponent('vnToggle', {
controller: Toggle,
bindings: {
checked: '<?'
}
});