|
1 | 1 | /* eslint-disable @typescript-eslint/no-this-alias */ |
| 2 | +import type { FormGroup } from './form-group'; |
2 | 3 | import type { AsyncValidatorFn, ValidationErrors, ValidatorFn } from './validators'; |
3 | 4 | import type { Subscription } from 'rxjs'; |
4 | 5 |
|
5 | 6 | import { isArray } from 'lodash'; |
6 | 7 | import { from, Subject } from 'rxjs'; |
7 | 8 | import { forkJoin } from 'rxjs'; |
8 | 9 |
|
9 | | -import { FormGroup } from './form-group'; |
10 | | - |
11 | 10 | export type FormControlStatus = 'VALID' | 'INVALID' | 'PENDING' | 'DISABLED'; |
12 | 11 |
|
13 | 12 | export const [VALID, INVALID, PENDING, DISABLED] = ['VALID', 'INVALID', 'PENDING', 'DISABLED'] as FormControlStatus[]; |
14 | 13 |
|
15 | | -type Mutable<T> = { |
16 | | - -readonly [P in keyof T]: T[P]; |
17 | | -}; |
18 | | - |
19 | | -type GetFormControlPropertyFromArray<T, A> = Mutable<A> extends [infer K, ...infer R] |
20 | | - ? K extends keyof T |
21 | | - ? GetFormControlPropertyFromArray<T[K], R> |
22 | | - : null |
23 | | - : T; |
24 | | - |
25 | | -type GetFormControlProperty<T, S> = S extends `${infer K}.${infer R}` |
26 | | - ? K extends keyof T |
27 | | - ? GetFormControlProperty<T[K], R> |
28 | | - : null |
29 | | - : S extends keyof T |
30 | | - ? T[S] |
31 | | - : null; |
32 | | - |
33 | | -function find(control: AbstractControl, path: string[] | string, delimiter: string) { |
34 | | - if (path == null) return null; |
35 | | - |
36 | | - if (!Array.isArray(path)) { |
37 | | - path = path.split(delimiter); |
38 | | - } |
39 | | - if (Array.isArray(path) && path.length === 0) return null; |
40 | | - |
41 | | - // Not using Array.reduce here due to a Chrome 80 bug |
42 | | - // https://bugs.chromium.org/p/chromium/issues/detail?id=1049982 |
43 | | - let controlToFind: AbstractControl | null = control; |
44 | | - path.forEach((name) => { |
45 | | - if (controlToFind instanceof FormGroup) { |
46 | | - controlToFind = name in controlToFind.controls ? controlToFind.controls[name] : null; |
47 | | - } else { |
48 | | - controlToFind = null; |
49 | | - } |
50 | | - }); |
51 | | - return controlToFind; |
52 | | -} |
53 | | - |
54 | 14 | function mergeErrors(arrayOfErrors: (ValidationErrors | null)[]): ValidationErrors | null { |
55 | 15 | const res: { [key: string]: any } = {}; |
56 | 16 |
|
@@ -323,21 +283,6 @@ export abstract class AbstractControl<V = any> { |
323 | 283 | } |
324 | 284 | } |
325 | 285 |
|
326 | | - get<S extends string>(path: S): AbstractControl<GetFormControlProperty<V, S>>; |
327 | | - get<S extends ArrayLike<string>>(path: S): AbstractControl<GetFormControlPropertyFromArray<V, S>>; |
328 | | - get(path: string[] | string): AbstractControl | null { |
329 | | - return find(this, path, '.'); |
330 | | - } |
331 | | - |
332 | | - getError(errorCode: string, path?: string[] | string): any { |
333 | | - const control = path ? this.get(path) : this; |
334 | | - return control && control.errors ? control.errors[errorCode] : null; |
335 | | - } |
336 | | - |
337 | | - hasError(errorCode: string, path?: string[] | string): boolean { |
338 | | - return !!this.getError(errorCode, path); |
339 | | - } |
340 | | - |
341 | 286 | protected abstract _value: V; |
342 | 287 |
|
343 | 288 | protected abstract _status: FormControlStatus; |
|
0 commit comments