UNPKG

ngx-bootstrap

Version:
76 lines 2.42 kB
import { Directive, forwardRef, HostBinding, HostListener, Input } from '@angular/core'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; // TODO: config: activeClass - Class to apply to the checked buttons export const CHECKBOX_CONTROL_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => ButtonCheckboxDirective), multi: true }; /** * Add checkbox functionality to any element */ export class ButtonCheckboxDirective { constructor() { /** Truthy value, will be set to ngModel */ this.btnCheckboxTrue = true; /** Falsy value, will be set to ngModel */ this.btnCheckboxFalse = false; this.state = false; this.isDisabled = false; this.onChange = Function.prototype; this.onTouched = Function.prototype; } // view -> model onClick() { if (this.isDisabled) { return; } this.toggle(!this.state); this.onChange(this.value); } ngOnInit() { this.toggle(this.trueValue === this.value); } get trueValue() { return typeof this.btnCheckboxTrue !== 'undefined' ? this.btnCheckboxTrue : true; } get falseValue() { return typeof this.btnCheckboxFalse !== 'undefined' ? this.btnCheckboxFalse : false; } toggle(state) { this.state = state; this.value = this.state ? this.trueValue : this.falseValue; } // ControlValueAccessor // model -> view writeValue(value) { this.state = this.trueValue === value; this.value = value ? this.trueValue : this.falseValue; } setDisabledState(isDisabled) { this.isDisabled = isDisabled; } registerOnChange(fn) { this.onChange = fn; } registerOnTouched(fn) { this.onTouched = fn; } } ButtonCheckboxDirective.decorators = [ { type: Directive, args: [{ selector: '[btnCheckbox]', providers: [CHECKBOX_CONTROL_VALUE_ACCESSOR] },] } ]; ButtonCheckboxDirective.propDecorators = { btnCheckboxTrue: [{ type: Input }], btnCheckboxFalse: [{ type: Input }], state: [{ type: HostBinding, args: ['class.active',] }, { type: HostBinding, args: ['attr.aria-pressed',] }], onClick: [{ type: HostListener, args: ['click',] }] }; //# sourceMappingURL=button-checkbox.directive.js.map