Skip to content

Commit 4383f51

Browse files
authored
chore: change ErrorStateMatcher to use FormControl again (#7555)
1 parent 630dfad commit 4383f51

File tree

6 files changed

+20
-12
lines changed

6 files changed

+20
-12
lines changed

src/demo-app/input/input-demo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, ChangeDetectionStrategy} from '@angular/core';
2-
import {FormControl, NgControl, Validators} from '@angular/forms';
2+
import {FormControl, Validators} from '@angular/forms';
33
import {ErrorStateMatcher} from '@angular/material';
44

55

@@ -54,7 +54,7 @@ export class InputDemo {
5454
}
5555

5656
customErrorStateMatcher: ErrorStateMatcher = {
57-
isErrorState: (control: NgControl | null) => {
57+
isErrorState: (control: FormControl | null) => {
5858
if (control) {
5959
const hasInteraction = control.dirty || control.touched;
6060
const isInvalid = control.invalid;

src/lib/core/error/error-options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
*/
88

99
import {Injectable} from '@angular/core';
10-
import {FormGroupDirective, NgForm, NgControl} from '@angular/forms';
10+
import {FormGroupDirective, NgForm, FormControl} from '@angular/forms';
1111

1212
/** Error state matcher that matches when a control is invalid and dirty. */
1313
@Injectable()
1414
export class ShowOnDirtyErrorStateMatcher implements ErrorStateMatcher {
15-
isErrorState(control: NgControl | null, form: FormGroupDirective | NgForm | null): boolean {
15+
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
1616
return !!(control && control.invalid && (control.dirty || (form && form.submitted)));
1717
}
1818
}
1919

2020
/** Provider that defines how form controls behave with regards to displaying error messages. */
2121
@Injectable()
2222
export class ErrorStateMatcher {
23-
isErrorState(control: NgControl | null, form: FormGroupDirective | NgForm | null): boolean {
23+
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
2424
return !!(control && control.invalid && (control.touched || (form && form.submitted)));
2525
}
2626
}

src/lib/input/input.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ returns a boolean. A result of `true` will display the error messages.
127127

128128
```ts
129129
class MyErrorStateMatcher implements ErrorStateMatcher {
130-
isErrorState(control: NgControl | null, form: FormGroupDirective | NgForm | null): boolean {
130+
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
131131
// Error when invalid control is dirty, touched, or submitted
132132
const isSubmitted = form && form.submitted;
133133
return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted)));

src/lib/input/input.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
Self,
1919
} from '@angular/core';
2020
import {coerceBooleanProperty} from '@angular/cdk/coercion';
21-
import {FormGroupDirective, NgControl, NgForm} from '@angular/forms';
21+
import {FormGroupDirective, NgControl, NgForm, FormControl} from '@angular/forms';
2222
import {Platform, getSupportedInputTypes} from '@angular/cdk/platform';
2323
import {getMatInputUnsupportedTypeError} from './input-errors';
2424
import {ErrorStateMatcher} from '@angular/material/core';
@@ -225,7 +225,8 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
225225
const oldState = this.errorState;
226226
const parent = this._parentFormGroup || this._parentForm;
227227
const matcher = this.errorStateMatcher || this._defaultErrorStateMatcher;
228-
const newState = matcher.isErrorState(this.ngControl, parent);
228+
const control = this.ngControl ? this.ngControl.control as FormControl : null;
229+
const newState = matcher.isErrorState(control, parent);
229230

230231
if (newState !== oldState) {
231232
this.errorState = newState;

src/lib/select/select.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ import {
4545
ViewChild,
4646
ViewEncapsulation,
4747
} from '@angular/core';
48-
import {ControlValueAccessor, FormGroupDirective, NgControl, NgForm} from '@angular/forms';
48+
import {
49+
ControlValueAccessor,
50+
FormGroupDirective,
51+
NgControl,
52+
NgForm,
53+
FormControl
54+
} from '@angular/forms';
4955
import {
5056
CanDisable,
5157
HasTabIndex,
@@ -687,8 +693,9 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
687693
get errorState(): boolean {
688694
const parent = this._parentFormGroup || this._parentForm;
689695
const matcher = this.errorStateMatcher || this._defaultErrorStateMatcher;
696+
const control = this.ngControl ? this.ngControl.control as FormControl : null;
690697

691-
return matcher.isErrorState(this.ngControl, parent);
698+
return matcher.isErrorState(control, parent);
692699
}
693700

694701
private _initializeSelection(): void {

src/lib/stepper/stepper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
ViewEncapsulation,
2323
ChangeDetectionStrategy,
2424
} from '@angular/core';
25-
import {NgControl, FormGroupDirective, NgForm} from '@angular/forms';
25+
import {FormControl, FormGroupDirective, NgForm} from '@angular/forms';
2626
import {ErrorStateMatcher} from '@angular/material/core';
2727
import {MatStepHeader} from './step-header';
2828
import {MatStepLabel} from './step-label';
@@ -51,7 +51,7 @@ export class MatStep extends _MatStep implements ErrorStateMatcher {
5151
}
5252

5353
/** Custom error state matcher that additionally checks for validity of interacted form. */
54-
isErrorState(control: NgControl | null, form: FormGroupDirective | NgForm | null): boolean {
54+
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
5555
const originalErrorState = this._errorStateMatcher.isErrorState(control, form);
5656

5757
// Custom error state checks for the validity of form that is not submitted or touched

0 commit comments

Comments
 (0)