refactor(forms): avoid duplicated code in `AbstractControlStatus` directive (#40651)

This commit updates `AbstractControlStatus` directive code to remove duplicated logic in getters and replaces
that logic with a new function that accepts an argument.

PR Close #40651
This commit is contained in:
Andrew Kushnir 2021-01-31 22:22:09 -08:00 committed by Misko Hevery
parent 1579df243d
commit 72c0188e05
1 changed files with 11 additions and 27 deletions

View File

@ -12,6 +12,8 @@ import {AbstractControlDirective} from './abstract_control_directive';
import {ControlContainer} from './control_container'; import {ControlContainer} from './control_container';
import {NgControl} from './ng_control'; import {NgControl} from './ng_control';
type AnyControlStatus = 'untouched'|'touched'|'pristine'|'dirty'|'valid'|'invalid'|'pending';
export class AbstractControlStatus { export class AbstractControlStatus {
private _cd: AbstractControlDirective|null; private _cd: AbstractControlDirective|null;
@ -19,37 +21,19 @@ export class AbstractControlStatus {
this._cd = cd; this._cd = cd;
} }
get ngClassUntouched(): boolean { is(status: AnyControlStatus): boolean {
return this._cd?.control?.untouched ?? false; return !!this._cd?.control?.[status];
}
get ngClassTouched(): boolean {
return this._cd?.control?.touched ?? false;
}
get ngClassPristine(): boolean {
return this._cd?.control?.pristine ?? false;
}
get ngClassDirty(): boolean {
return this._cd?.control?.dirty ?? false;
}
get ngClassValid(): boolean {
return this._cd?.control?.valid ?? false;
}
get ngClassInvalid(): boolean {
return this._cd?.control?.invalid ?? false;
}
get ngClassPending(): boolean {
return this._cd?.control?.pending ?? false;
} }
} }
export const ngControlStatusHost = { export const ngControlStatusHost = {
'[class.ng-untouched]': 'ngClassUntouched', '[class.ng-untouched]': 'is("untouched")',
'[class.ng-touched]': 'ngClassTouched', '[class.ng-touched]': 'is("touched")',
'[class.ng-pristine]': 'ngClassPristine', '[class.ng-pristine]': 'is("pristine")',
'[class.ng-dirty]': 'ngClassDirty', '[class.ng-dirty]': 'is("dirty")',
'[class.ng-valid]': 'ngClassValid', '[class.ng-valid]': 'is("valid")',
'[class.ng-invalid]': 'ngClassInvalid', '[class.ng-invalid]': 'is("invalid")',
'[class.ng-pending]': 'ngClassPending', '[class.ng-pending]': 'is("pending")',
}; };
/** /**