Skip to content

Commit 5c6b0a4

Browse files
committed
refactor: rename match to isErrorState
1 parent 3db132c commit 5c6b0a4

File tree

6 files changed

+27
-24
lines changed

6 files changed

+27
-24
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import {FormGroupDirective, NgForm, NgControl} from '@angular/forms';
1212
/** Error state matcher that matches when a control is invalid and dirty. */
1313
@Injectable()
1414
export class ShowOnDirtyErrorStateMatcher implements ErrorStateMatcher {
15-
match(control: NgControl | null, form: FormGroupDirective | NgForm | null): boolean {
15+
isErrorSate(control: NgControl | null, form: FormGroupDirective | NgForm | null): boolean {
1616
return control ? !!(control.invalid && (control.dirty || (form && form.submitted))) : false;
1717
}
1818
}
1919

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

src/lib/input/input-container.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,9 @@ describe('MdInputContainer with forms', () => {
842842
declarations: [
843843
MdInputContainerWithFormErrorMessages
844844
],
845-
providers: [{ provide: ErrorStateMatcher, useValue: { match: globalErrorStateMatcher } }]
845+
providers: [
846+
{ provide: ErrorStateMatcher, useValue: { isErrorSate: globalErrorStateMatcher } }
847+
]
846848
});
847849

848850
let fixture = TestBed.createComponent(MdInputContainerWithFormErrorMessages);
@@ -1199,7 +1201,7 @@ class MdInputContainerWithCustomErrorStateMatcher {
11991201

12001202
errorState = false;
12011203
customErrorStateMatcher: ErrorStateMatcher = {
1202-
match: () => this.errorState
1204+
isErrorSate: () => this.errorState
12031205
};
12041206
}
12051207

src/lib/input/input-container.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,9 @@ export class MdInputDirective implements OnChanges, OnDestroy, DoCheck {
294294

295295
/** Re-evaluates the error state. This is only relevant with @angular/forms. */
296296
private _updateErrorState() {
297-
const oldState = this._isErrorState;
298-
const matcher = this.errorStateMatcher || this._globalErrorStateMatcher;
299-
const newState = matcher.match(this._ngControl, this._parentFormGroup || this._parentForm);
297+
let oldState = this._isErrorState;
298+
let matcher = this.errorStateMatcher || this._globalErrorStateMatcher;
299+
let newState = matcher.isErrorSate(this._ngControl, this._parentFormGroup || this._parentForm);
300300

301301
if (newState !== oldState) {
302302
this._isErrorState = newState;

src/lib/input/input.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ A placeholder for the input can be specified in one of two ways: either using th
6262
attribute on the `input` or `textarea`, or using an `md-placeholder` element in the
6363
`md-input-container`. Using both will raise an error.
6464

65-
Global default placeholder options can be specified by setting the `MD_PLACEHOLDER_GLOBAL_OPTIONS` provider. This setting will apply to all components that support the floating placeholder.
65+
Global default placeholder options can be specified by setting the `MD_PLACEHOLDER_GLOBAL_OPTIONS`
66+
provider. This setting will apply to all components that support the floating placeholder.
6667

6768
```ts
6869
@NgModule({
@@ -110,12 +111,12 @@ warn color.
110111

111112
### Custom Error Matcher
112113

113-
By default, error messages are shown when the control is invalid and either the user has interacted with
114-
(touched) the element or the parent form has been submitted. If you wish to override this
114+
By default, error messages are shown when the control is invalid and either the user has interacted
115+
with (touched) the element or the parent form has been submitted. If you wish to override this
115116
behavior (e.g. to show the error as soon as the invalid control is dirty or when a parent form group
116117
is invalid), you can use the `errorStateMatcher` property of the `mdInput`. To use this property,
117-
create a function in your component class that returns a boolean. A result of `true` will display
118-
the error messages.
118+
create an `ErrorStateMatcher` object in your component class that has a `isErrorSate` function which
119+
returns a boolean. A result of `true` will display the error messages.
119120

120121
```html
121122
<md-input-container>
@@ -126,22 +127,22 @@ the error messages.
126127

127128
```ts
128129
class MyErrorStateMatcher implements ErrorStateMatcher {
129-
match(control: NgControl | null, form: FormGroupDirective | NgForm | null): boolean {
130+
isErrorSate(control: NgControl | null, form: FormGroupDirective | NgForm | null): boolean {
130131
// Error when invalid control is dirty, touched, or submitted
131132
const isSubmitted = form && form.submitted;
132-
return !!(control.invalid && (control.dirty || control.touched || isSubmitted)));
133+
return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted)));
133134
}
134135
}
135136
```
136137

137-
A global error state matcher can be specified by setting the `ErrorOptions` provider. This applies
138-
to all inputs. For convenience, `ShowOnDirtyErrorStateMatcher` is available in order to globally
139-
cause input errors to show when the input is dirty and invalid.
138+
A global error state matcher can be specified by setting the `ErrorStateMatcher` provider. This
139+
applies to all inputs. For convenience, `ShowOnDirtyErrorStateMatcher` is available in order to
140+
globally cause input errors to show when the input is dirty and invalid.
140141

141142
```ts
142143
@NgModule({
143144
providers: [
144-
{provide: ErrorOptions, useClass: ShowOnDirtyErrorStateMatcher}
145+
{provide: ErrorStateMatcher, useClass: ShowOnDirtyErrorStateMatcher}
145146
]
146147
})
147148
```

src/lib/select/select.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2674,24 +2674,24 @@ describe('MdSelect', () => {
26742674
expect(component.control.invalid).toBe(false);
26752675
expect(component.select._isErrorState()).toBe(false);
26762676

2677-
customErrorFixture.componentInstance.errorStateMatcher = { match: matcher };
2677+
customErrorFixture.componentInstance.errorStateMatcher = { isErrorSate: matcher };
26782678
customErrorFixture.detectChanges();
26792679

26802680
expect(component.select._isErrorState()).toBe(true);
26812681
expect(matcher).toHaveBeenCalled();
26822682
});
26832683

26842684
it('should be able to override the error matching behavior via the injection token', () => {
2685-
const errorOptions: ErrorStateMatcher = {
2686-
match: jasmine.createSpy('error state matcher').and.returnValue(true)
2685+
const errorStateMatcher: ErrorStateMatcher = {
2686+
isErrorSate: jasmine.createSpy('error state matcher').and.returnValue(true)
26872687
};
26882688

26892689
fixture.destroy();
26902690

26912691
TestBed.resetTestingModule().configureTestingModule({
26922692
imports: [MdSelectModule, ReactiveFormsModule, FormsModule, NoopAnimationsModule],
26932693
declarations: [SelectInsideFormGroup],
2694-
providers: [{ provide: ErrorStateMatcher, useValue: errorOptions }],
2694+
providers: [{ provide: ErrorStateMatcher, useValue: errorStateMatcher }],
26952695
});
26962696

26972697
const errorFixture = TestBed.createComponent(SelectInsideFormGroup);
@@ -2700,7 +2700,7 @@ describe('MdSelect', () => {
27002700
errorFixture.detectChanges();
27012701

27022702
expect(component.select._isErrorState()).toBe(true);
2703-
expect(errorOptions.match).toHaveBeenCalled();
2703+
expect(errorStateMatcher.isErrorSate).toHaveBeenCalled();
27042704
});
27052705
});
27062706

src/lib/select/select.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
638638
/** Whether the select is in an error state. */
639639
_isErrorState(): boolean {
640640
const matcher = this.errorStateMatcher || this._globalErrorStateMatcher;
641-
return matcher.match(this._control, this._parentFormGroup || this._parentForm);
641+
return matcher.isErrorSate(this._control, this._parentFormGroup || this._parentForm);
642642
}
643643

644644
/**

0 commit comments

Comments
 (0)