-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
radiobutton.ts
executable file
·207 lines (157 loc) · 5.91 KB
/
radiobutton.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {NgModule,Component,Input,Output,ElementRef,EventEmitter,forwardRef,ViewChild,ChangeDetectorRef,ChangeDetectionStrategy, Injectable, Injector, OnInit, OnDestroy} from '@angular/core';
import {CommonModule} from '@angular/common';
import {NG_VALUE_ACCESSOR, ControlValueAccessor, NgControl} from '@angular/forms';
export const RADIO_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => RadioButton),
multi: true
};
@Injectable({
providedIn: 'root',
})
export class RadioControlRegistry {
private accessors: any[] = [];
add(control: NgControl, accessor: RadioButton) {
this.accessors.push([control, accessor]);
}
remove(accessor: RadioButton) {
this.accessors = this.accessors.filter((c) => {
return c[1] !== accessor;
})
}
select(accessor: RadioButton) {
this.accessors.forEach((c) => {
if (this.isSameGroup(c, accessor) && c[1] !== accessor) {
c[1].writeValue(accessor.value);
}
});
}
private isSameGroup(controlPair: [NgControl, RadioButton], accessor: RadioButton): boolean {
if (!controlPair[0].control) {
return false;
}
return controlPair[0].control.root === accessor.control.control.root && controlPair[1].name === accessor.name;
}
}
@Component({
selector: 'p-radioButton',
template: `
<div [ngStyle]="style" [ngClass]="{'p-radiobutton p-component':true,'p-radiobutton-checked': checked, 'p-radiobutton-disabled': disabled, 'p-radiobutton-focused': focused}" [class]="styleClass">
<div class="p-hidden-accessible">
<input #rb type="radio" [attr.id]="inputId" [attr.name]="name" [attr.value]="value" [attr.tabindex]="tabindex" [attr.aria-checked]="checked" [attr.aria-label]="ariaLabel"
[attr.aria-labelledby]="ariaLabelledBy" [checked]="checked" (change)="onChange($event)" (focus)="onInputFocus($event)" (blur)="onInputBlur($event)" [disabled]="disabled">
</div>
<div (click)="handleClick($event, rb, true)" [ngClass]="{'p-radiobutton-box':true, 'p-highlight': checked, 'p-disabled': disabled, 'p-focus': focused}">
<span class="p-radiobutton-icon"></span>
</div>
</div>
<label (click)="select($event)" [class]="labelStyleClass"
[ngClass]="{'p-radiobutton-label':true, 'p-radiobutton-label-active':rb.checked, 'p-disabled':disabled, 'p-radiobutton-label-focus':focused}"
*ngIf="label" [attr.for]="inputId">{{label}}</label>
`,
providers: [RADIO_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class RadioButton implements ControlValueAccessor, OnInit, OnDestroy {
@Input() value: any;
@Input() formControlName: string;
@Input() name: string;
@Input() disabled: boolean;
@Input() label: string;
@Input() tabindex: number;
@Input() inputId: string;
@Input() ariaLabelledBy: string;
@Input() ariaLabel: string;
@Input() style: any;
@Input() styleClass: string;
@Input() labelStyleClass: string;
@Output() onClick: EventEmitter<any> = new EventEmitter();
@Output() onFocus: EventEmitter<any> = new EventEmitter();
@Output() onBlur: EventEmitter<any> = new EventEmitter();
@ViewChild('rb') inputViewChild: ElementRef;
public onModelChange: Function = () => {};
public onModelTouched: Function = () => {};
public checked: boolean;
public focused: boolean;
control: NgControl;
constructor(public cd: ChangeDetectorRef, private injector: Injector, private registry: RadioControlRegistry) {}
ngOnInit() {
this.control = this.injector.get(NgControl);
this.checkName();
this.registry.add(this.control, this);
}
handleClick(event, radioButton, focus) {
event.preventDefault();
if (this.disabled) {
return;
}
this.select(event);
if (focus) {
radioButton.focus();
}
}
select(event) {
if (!this.disabled) {
this.inputViewChild.nativeElement.checked = true;
this.checked = true;
this.onModelChange(this.value);
this.registry.select(this);
this.onClick.emit(event);
}
}
writeValue(value: any) : void {
this.checked = (value == this.value);
if (this.inputViewChild && this.inputViewChild.nativeElement) {
this.inputViewChild.nativeElement.checked = this.checked;
}
this.cd.markForCheck();
}
registerOnChange(fn: Function): void {
this.onModelChange = fn;
}
registerOnTouched(fn: Function): void {
this.onModelTouched = fn;
}
setDisabledState(val: boolean): void {
this.disabled = val;
this.cd.markForCheck();
}
onInputFocus(event) {
this.focused = true;
this.onFocus.emit(event);
}
onInputBlur(event) {
this.focused = false;
this.onModelTouched();
this.onBlur.emit(event);
}
onChange(event) {
this.select(event);
}
focus() {
this.inputViewChild.nativeElement.focus();
}
ngOnDestroy() {
this.registry.remove(this);
}
private checkName() {
if (this.name && this.formControlName && this.name !== this.formControlName) {
this.throwNameError();
}
if (!this.name && this.formControlName) {
this.name = this.formControlName;
}
}
private throwNameError() {
throw new Error(`
If you define both a name and a formControlName attribute on your radio button, their values
must match. Ex: <p-radioButton formControlName="food" name="food"></p-radioButton>
`);
}
}
@NgModule({
imports: [CommonModule],
exports: [RadioButton],
declarations: [RadioButton]
})
export class RadioButtonModule { }