Skip to content

Commit

Permalink
feat(buttons): add setDisabledState
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaSurmay committed Jan 12, 2018
1 parent a7c3173 commit f3f2bd3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions demo/src/ng-api-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ export const ngdoc: any = {
"type": "any",
"description": "<p>Radio button value, will be set to <code>ngModel</code> </p>\n"
},
{
"name": "disabled",
"type": "boolean",
"description": "<p>If <code>true</code> — radio button is disabled </p>\n"
},
{
"name": "uncheckable",
"type": "boolean",
Expand Down
24 changes: 22 additions & 2 deletions src/buttons/button-radio.directive.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// tslint:disable:no-use-before-declare
import {
ChangeDetectorRef, Directive, ElementRef, forwardRef, HostBinding, HostListener, Input, OnInit,
Optional
Optional, Renderer2
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { ButtonRadioGroupDirective } from './button-radio-group.directive';
Expand All @@ -24,6 +24,7 @@ export class ButtonRadioDirective implements ControlValueAccessor, OnInit {
onChange: any = Function.prototype;
onTouched: any = Function.prototype;
private _value: any;
private _disabled: boolean;

/** Radio button value, will be set to `ngModel` */
@Input() btnRadio: any;
Expand All @@ -42,6 +43,15 @@ export class ButtonRadioDirective implements ControlValueAccessor, OnInit {
}
this._value = value;
}
/** If `true` — radio button is disabled */
@Input() get disabled(): boolean {
return this._disabled;
}

set disabled(disabled: boolean) {
this._disabled = disabled;
this.setDisabledState(disabled);
}

@HostBinding('class.active')
get isActive(): boolean {
Expand All @@ -51,7 +61,8 @@ export class ButtonRadioDirective implements ControlValueAccessor, OnInit {
constructor(
private el: ElementRef,
private cdr: ChangeDetectorRef,
@Optional() private group: ButtonRadioGroupDirective
@Optional() private group: ButtonRadioGroupDirective,
private renderer: Renderer2
) {}

@HostListener('click')
Expand Down Expand Up @@ -94,4 +105,13 @@ export class ButtonRadioDirective implements ControlValueAccessor, OnInit {
registerOnTouched(fn: any): void {
this.onTouched = fn;
}

setDisabledState(disabled: boolean): void {
if (disabled) {
this.renderer.setAttribute(this.el.nativeElement, 'disabled', 'disabled');

return;
}
this.renderer.removeAttribute(this.el.nativeElement, 'disabled');
}
}

0 comments on commit f3f2bd3

Please sign in to comment.