UNPKG

ngx-bootstrap

Version:
126 lines 4.61 kB
import { ChangeDetectorRef, Directive, ElementRef, forwardRef, HostBinding, HostListener, Input, Optional, Renderer2 } from '@angular/core'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { ButtonRadioGroupDirective } from './button-radio-group.directive'; export var RADIO_CONTROL_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(function () { return ButtonRadioDirective; }), multi: true }; /** * Create radio buttons or groups of buttons. * A value of a selected button is bound to a variable specified via ngModel. */ var ButtonRadioDirective = /** @class */ (function () { function ButtonRadioDirective(el, cdr, group, renderer) { this.el = el; this.cdr = cdr; this.group = group; this.renderer = renderer; this.onChange = Function.prototype; this.onTouched = Function.prototype; } Object.defineProperty(ButtonRadioDirective.prototype, "value", { get: /** Current value of radio component or group */ function () { return this.group ? this.group.value : this._value; }, set: function (value) { if (this.group) { this.group.value = value; return; } this._value = value; }, enumerable: true, configurable: true }); Object.defineProperty(ButtonRadioDirective.prototype, "disabled", { get: /** If `true` — radio button is disabled */ function () { return this._disabled; }, set: function (disabled) { this._disabled = disabled; this.setDisabledState(disabled); }, enumerable: true, configurable: true }); Object.defineProperty(ButtonRadioDirective.prototype, "isActive", { get: function () { return this.btnRadio === this.value; }, enumerable: true, configurable: true }); ButtonRadioDirective.prototype.onClick = function () { if (this.el.nativeElement.attributes.disabled || !this.uncheckable && this.btnRadio === this.value) { return; } this.value = this.uncheckable && this.btnRadio === this.value ? undefined : this.btnRadio; this._onChange(this.value); }; ButtonRadioDirective.prototype.ngOnInit = function () { this.uncheckable = typeof this.uncheckable !== 'undefined'; }; ButtonRadioDirective.prototype.onBlur = function () { this.onTouched(); }; ButtonRadioDirective.prototype._onChange = function (value) { if (this.group) { this.group.onTouched(); this.group.onChange(value); return; } this.onTouched(); this.onChange(value); }; // ControlValueAccessor // model -> view // ControlValueAccessor // model -> view ButtonRadioDirective.prototype.writeValue = // ControlValueAccessor // model -> view function (value) { this.value = value; this.cdr.markForCheck(); }; ButtonRadioDirective.prototype.registerOnChange = function (fn) { this.onChange = fn; }; ButtonRadioDirective.prototype.registerOnTouched = function (fn) { this.onTouched = fn; }; ButtonRadioDirective.prototype.setDisabledState = function (disabled) { if (disabled) { this.renderer.setAttribute(this.el.nativeElement, 'disabled', 'disabled'); return; } this.renderer.removeAttribute(this.el.nativeElement, 'disabled'); }; ButtonRadioDirective.decorators = [ { type: Directive, args: [{ selector: '[btnRadio]', providers: [RADIO_CONTROL_VALUE_ACCESSOR] },] }, ]; /** @nocollapse */ ButtonRadioDirective.ctorParameters = function () { return [ { type: ElementRef, }, { type: ChangeDetectorRef, }, { type: ButtonRadioGroupDirective, decorators: [{ type: Optional },] }, { type: Renderer2, }, ]; }; ButtonRadioDirective.propDecorators = { "btnRadio": [{ type: Input },], "uncheckable": [{ type: Input },], "value": [{ type: Input },], "disabled": [{ type: Input },], "isActive": [{ type: HostBinding, args: ['class.active',] }, { type: HostBinding, args: ['attr.aria-pressed',] },], "onClick": [{ type: HostListener, args: ['click',] },], }; return ButtonRadioDirective; }()); export { ButtonRadioDirective }; //# sourceMappingURL=button-radio.directive.js.map