-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathbase-value-accessor.ts
45 lines (38 loc) · 1.32 KB
/
base-value-accessor.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
import { ControlValueAccessor } from "@angular/forms";
export function generateValueAccessorSelector(...tagNames: string[]): string {
const tags: string[] = [];
tagNames.forEach(tagName => {
tags.push(tagName); // regular tag
tags.push(tagName.charAt(0).toLowerCase() + tagName.slice(1)); // lowercase first char
tags.push(tagName.split(/(?=[A-Z])/).join("-").toLowerCase()); // kebab case
});
const selectors = [];
for (const tag of tags) {
for (const directive of ["ngModel", "formControlName"]) {
selectors.push(`${tag}[${directive}]`);
}
}
return selectors.join(", ");
}
export class BaseValueAccessor<TView> implements ControlValueAccessor {
constructor(public view: TView) { }
onChange = (_) => { };
private pendingChangeNotification: number = 0;
registerOnChange(fn: (_: any) => void): void {
this.onChange = (arg) => {
if (this.pendingChangeNotification) {
clearTimeout(this.pendingChangeNotification);
}
this.pendingChangeNotification = setTimeout(() => {
this.pendingChangeNotification = 0;
fn(arg);
}, 20);
};
}
writeValue(_: any) {
//
}
registerOnTouched(_: () => void): void {
//
}
}