Skip to content

Commit 0414afa

Browse files
committed
feat(buttons): clean control value accessor impl
1 parent fe7e39b commit 0414afa

File tree

3 files changed

+34
-35
lines changed

3 files changed

+34
-35
lines changed
Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,75 @@
1-
import {
2-
Directive, HostBinding, HostListener, Input, OnInit, Self, ElementRef
3-
} from '@angular/core';
4-
import { ControlValueAccessor, NgModel } from '@angular/forms';
1+
import { Directive, HostBinding, HostListener, Input, OnInit, forwardRef } from '@angular/core';
2+
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
53

6-
// TODO: config: activeClass - Class to apply to the checked buttons.
4+
// TODO: config: activeClass - Class to apply to the checked buttons
75

8-
@Directive({selector: '[btnCheckbox][ngModel]'})
6+
export const CHECKBOX_CONTROL_VALUE_ACCESSOR: any = {
7+
provide: NG_VALUE_ACCESSOR,
8+
useExisting: forwardRef(() => ButtonCheckboxDirective),
9+
multi: true
10+
};
11+
12+
@Directive({selector: '[btnCheckbox]', providers: [CHECKBOX_CONTROL_VALUE_ACCESSOR]})
913
export class ButtonCheckboxDirective implements ControlValueAccessor, OnInit {
10-
public cd:NgModel;
11-
public el: ElementRef;
12-
@Input() public btnCheckboxTrue:any;
14+
@Input() public btnCheckboxTrue: any;
15+
@Input() public btnCheckboxFalse: any;
1316

14-
@Input() public btnCheckboxFalse:any;
15-
@HostBinding('class.active')
16-
public state:boolean = false;
17+
@HostBinding('class.active') public state: boolean = false;
1718

18-
protected onChange:any = Function.prototype;
19-
protected onTouched:any = Function.prototype;
19+
protected value: any;
20+
protected isDisabled: boolean;
2021

21-
protected value:any;
22+
protected onChange: any = Function.prototype;
23+
protected onTouched: any = Function.prototype;
2224

2325
// view -> model
2426
@HostListener('click')
25-
public onClick():void {
26-
if (this.el.nativeElement.attributes.disabled) {
27+
public onClick(): void {
28+
if (this.isDisabled) {
2729
return;
2830
}
29-
this.toggle(!this.state);
30-
this.cd.viewToModelUpdate(this.value);
31-
}
3231

33-
public constructor(@Self() cd:NgModel, el: ElementRef) {
34-
this.el = el;
35-
this.cd = cd;
36-
// hack !
37-
cd.valueAccessor = this;
32+
this.toggle(!this.state);
33+
this.onChange(this.value);
3834
}
3935

40-
public ngOnInit():any {
36+
public ngOnInit(): any {
4137
this.toggle(this.trueValue === this.value);
4238
}
4339

44-
protected get trueValue():boolean {
40+
protected get trueValue(): boolean {
4541
return typeof this.btnCheckboxTrue !== 'undefined'
4642
? this.btnCheckboxTrue
4743
: true;
4844
}
4945

50-
protected get falseValue():boolean {
46+
protected get falseValue(): boolean {
5147
return typeof this.btnCheckboxFalse !== 'undefined'
5248
? this.btnCheckboxFalse
5349
: false;
5450
}
5551

56-
public toggle(state:boolean):void {
52+
public toggle(state: boolean): void {
5753
this.state = state;
5854
this.value = this.state ? this.trueValue : this.falseValue;
5955
}
6056

6157
// ControlValueAccessor
6258
// model -> view
63-
public writeValue(value:any):void {
59+
public writeValue(value: any): void {
6460
this.state = this.trueValue === value;
65-
this.value = value;
61+
this.value = value ? this.trueValue : this.falseValue;
62+
}
63+
64+
public setDisabledState(isDisabled: boolean): void {
65+
this.isDisabled = isDisabled;
6666
}
6767

68-
public registerOnChange(fn:(_:any) => {}):void {
68+
public registerOnChange(fn: (_: any) => {}): void {
6969
this.onChange = fn;
7070
}
7171

72-
public registerOnTouched(fn:() => {}):void {
72+
public registerOnTouched(fn: () => {}): void {
7373
this.onTouched = fn;
7474
}
7575
}

src/buttons/button-radio.directive.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import {
33
} from '@angular/core';
44
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
55

6-
/* tslint:disable */
76
export const RADIO_CONTROL_VALUE_ACCESSOR: any = {
87
provide: NG_VALUE_ACCESSOR,
98
useExisting: forwardRef(() => ButtonRadioDirective),
109
multi: true
1110
};
12-
/* tslint:enable */
1311

1412
@Directive({ selector: '[btnRadio]', providers: [RADIO_CONTROL_VALUE_ACCESSOR] })
1513
export class ButtonRadioDirective implements ControlValueAccessor, OnInit {

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "tslint-config-valorsoft",
33
"rulesDirectory": "./node_modules/codelyzer",
44
"rules": {
5+
"no-forward-ref": false,
56
"only-arrow-functions": false,
67
"no-access-missing-member": false,
78
"directive-selector": false,

0 commit comments

Comments
 (0)