-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
form-control.ts
211 lines (185 loc) Β· 6.14 KB
/
form-control.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
208
209
210
211
import {
UntypedFormControl,
AbstractControl,
ValidationErrors,
} from '@angular/forms';
import { isObservable, Observable, Subject, Subscription } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
import {
controlValueChanges$,
controlStatus$,
controlDisabledWhile,
controlEnabledWhile,
disableControl,
enableControl,
mergeErrors,
removeError,
hasErrorAnd,
controlErrorChanges$,
} from './core';
import { BoxedValue } from './types';
export class FormControl<T> extends UntypedFormControl {
readonly value!: T;
readonly valueChanges!: Observable<T>;
private touchChanges = new Subject<boolean>();
private dirtyChanges = new Subject<boolean>();
private errorsSubject = new Subject<ValidationErrors | null>();
readonly touch$ = this.touchChanges
.asObservable()
.pipe(distinctUntilChanged());
readonly dirty$ = this.dirtyChanges
.asObservable()
.pipe(distinctUntilChanged());
readonly value$ = controlValueChanges$<T>(this);
readonly disabled$ = controlStatus$(this, 'disabled');
readonly enabled$ = controlStatus$(this, 'enabled');
readonly invalid$ = controlStatus$(this, 'invalid');
readonly valid$ = controlStatus$(this, 'valid');
readonly status$ = controlStatus$(this, 'status');
readonly errors$ = controlErrorChanges$(
this,
this.errorsSubject.asObservable()
);
constructor(
formState?: BoxedValue<T>,
validatorOrOpts?: ConstructorParameters<typeof UntypedFormControl>[1],
asyncValidator?: ConstructorParameters<typeof UntypedFormControl>[2]
) {
super(formState, validatorOrOpts, asyncValidator);
}
setValue(
valueOrObservable: Observable<T>,
options?: Parameters<AbstractControl['setValue']>[1]
): Subscription;
setValue(
valueOrObservable: T,
options?: Parameters<AbstractControl['setValue']>[1]
): void;
setValue(
valueOrObservable: any,
options?: Parameters<AbstractControl['setValue']>[1]
): any {
if (isObservable(valueOrObservable)) {
return valueOrObservable.subscribe((value) =>
super.setValue(value as T, options)
);
}
super.setValue(valueOrObservable, options);
}
patchValue(
valueOrObservable: Observable<T>,
options?: Parameters<AbstractControl['patchValue']>[1]
): Subscription;
patchValue(
valueOrObservable: T,
options?: Parameters<AbstractControl['patchValue']>[1]
): void;
patchValue(valueOrObservable: any, options?: any): any {
if (isObservable(valueOrObservable)) {
return valueOrObservable.subscribe((value) =>
super.patchValue(value as T, options)
);
}
super.patchValue(valueOrObservable, options);
}
getRawValue(): T {
return this.value;
}
markAsTouched(
...opts: Parameters<AbstractControl['markAsTouched']>
): ReturnType<AbstractControl['markAsTouched']> {
super.markAsTouched(...opts);
this.touchChanges.next(true);
}
markAsUntouched(
...opts: Parameters<AbstractControl['markAsUntouched']>
): ReturnType<AbstractControl['markAsUntouched']> {
super.markAsUntouched(...opts);
this.touchChanges.next(false);
}
markAsPristine(
...opts: Parameters<AbstractControl['markAsPristine']>
): ReturnType<AbstractControl['markAsPristine']> {
super.markAsPristine(...opts);
this.dirtyChanges.next(false);
}
markAsDirty(
...opts: Parameters<AbstractControl['markAsDirty']>
): ReturnType<AbstractControl['markAsDirty']> {
super.markAsDirty(...opts);
this.dirtyChanges.next(true);
}
setEnable(enable = true, opts?: Parameters<AbstractControl['enable']>[0]) {
enableControl(this, enable, opts);
}
setDisable(disable = true, opts?: Parameters<AbstractControl['disable']>[0]) {
disableControl(this, disable, opts);
}
disabledWhile(
observable: Observable<boolean>,
options?: Parameters<AbstractControl['disable']>[0]
) {
return controlDisabledWhile(this, observable, options);
}
enabledWhile(
observable: Observable<boolean>,
options?: Parameters<AbstractControl['enable']>[0]
) {
return controlEnabledWhile(this, observable, options);
}
reset(
formState?: T,
options?: Parameters<AbstractControl['reset']>[1]
): void {
super.reset(formState, options);
}
setValidators(
newValidators: Parameters<AbstractControl['setValidators']>[0],
options?: Parameters<AbstractControl['updateValueAndValidity']>[0]
) {
super.setValidators(newValidators);
super.updateValueAndValidity(options);
}
setAsyncValidators(
newValidator: Parameters<AbstractControl['setAsyncValidators']>[0],
options?: Parameters<AbstractControl['updateValueAndValidity']>[0]
) {
super.setAsyncValidators(newValidator);
super.updateValueAndValidity(options);
}
getError<E>(...params: Parameters<AbstractControl['getError']>): E | null {
return super.getError(...params);
}
setErrors(...opts: Parameters<AbstractControl['setErrors']>) {
/**
* @description
* Use an elvis operator to avoid a throw when the control is used with an async validator
* Which will be instantly resolved (like with `of(null)`)
* In such case, Angular will call this method instantly before even instancing the properties causing the throw
* Can be easily reproduced with a step-by-step debug once compiled when checking the stack trace of the constructor
*
* Issue: https://github.com/ngneat/reactive-forms/issues/91
* Reproduction: https://codesandbox.io/embed/github/C0ZEN/ngneat-reactive-forms-error-issue-cs/tree/main/?autoresize=1&expanddevtools=1&fontsize=14&hidenavigation=1&theme=dark
*/
this.errorsSubject?.next(opts[0]);
return super.setErrors(...opts);
}
mergeErrors(
errors: ValidationErrors | null,
opts?: Parameters<AbstractControl['setErrors']>[1]
) {
this.setErrors(mergeErrors(this.errors, errors), opts);
}
removeError(
key: string,
opts?: Parameters<AbstractControl['setErrors']>[1]
): void {
this.setErrors(removeError(this.errors, key), opts);
}
hasErrorAndTouched(error: string): boolean {
return hasErrorAnd('touched', this, error);
}
hasErrorAndDirty(error: string): boolean {
return hasErrorAnd('dirty', this, error);
}
}