Skip to content

Commit 2481c3f

Browse files
committed
refactor: preserve context when invoking the error state matcher
1 parent 062ceb9 commit 2481c3f

File tree

7 files changed

+16
-15
lines changed

7 files changed

+16
-15
lines changed

src/lib/core/core.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ export {
128128
MdError,
129129
ErrorStateMatcher,
130130
ErrorOptions,
131-
defaultErrorStateMatcher,
132131
showOnDirtyErrorStateMatcher,
133132
} from './error/index';
134133

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import {FormGroupDirective, NgForm, NgControl} from '@angular/forms';
1212
export type ErrorStateMatcher =
1313
(control: NgControl | null, form: FormGroupDirective | NgForm | null) => boolean;
1414

15-
/** Returns whether control is invalid and is either touched or is a part of a submitted form. */
16-
export const defaultErrorStateMatcher: ErrorStateMatcher = (control, form) => {
17-
return control ? !!(control.invalid && (control.touched || (form && form.submitted))) : false;
18-
};
19-
2015
/** Returns whether control is invalid and is either dirty or is a part of a submitted form. */
2116
export const showOnDirtyErrorStateMatcher: ErrorStateMatcher = (control, form) => {
2217
return control ? !!(control.invalid && (control.dirty || (form && form.submitted))) : false;
@@ -28,5 +23,7 @@ export const showOnDirtyErrorStateMatcher: ErrorStateMatcher = (control, form) =
2823
*/
2924
@Injectable()
3025
export class ErrorOptions {
31-
errorStateMatcher: ErrorStateMatcher = defaultErrorStateMatcher;
26+
isErrorState(control: NgControl | null, form: FormGroupDirective | NgForm | null): boolean {
27+
return control ? !!(control.invalid && (control.touched || (form && form.submitted))) : false;
28+
}
3229
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ describe('MdInputContainer with forms', () => {
845845
providers: [
846846
{
847847
provide: ErrorOptions,
848-
useValue: { errorStateMatcher: globalErrorStateMatcher } }
848+
useValue: { isErrorState: globalErrorStateMatcher } }
849849
]
850850
});
851851

@@ -876,7 +876,7 @@ describe('MdInputContainer with forms', () => {
876876
providers: [
877877
{
878878
provide: ErrorOptions,
879-
useValue: { errorStateMatcher: showOnDirtyErrorStateMatcher }
879+
useValue: { isErrorState: showOnDirtyErrorStateMatcher }
880880
}
881881
]
882882
});

src/lib/input/input-container.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,9 @@ export class MdInputDirective implements OnChanges, OnDestroy, DoCheck {
295295
/** Re-evaluates the error state. This is only relevant with @angular/forms. */
296296
private _updateErrorState() {
297297
const oldState = this._isErrorState;
298-
const errorMatcher = this.errorStateMatcher || this._errorOptions.errorStateMatcher;
299-
const newState = errorMatcher(this._ngControl, this._parentFormGroup || this._parentForm);
298+
const newState = this.errorStateMatcher ?
299+
this.errorStateMatcher(this._ngControl, this._parentFormGroup || this._parentForm) :
300+
this._errorOptions.isErrorState(this._ngControl, this._parentFormGroup || this._parentForm);
300301

301302
if (newState !== oldState) {
302303
this._isErrorState = newState;

src/lib/input/input.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ cause input errors to show when the input is dirty and invalid.
139139
```ts
140140
@NgModule({
141141
providers: [
142-
{provide: ErrorOptions, useValue: { errorStateMatcher: showOnDirtyErrorStateMatcher }}
142+
{provide: ErrorOptions, useValue: { isErrorState: showOnDirtyErrorStateMatcher }}
143143
]
144144
})
145145
```

src/lib/select/select.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2683,7 +2683,7 @@ describe('MdSelect', () => {
26832683

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

26892689
fixture.destroy();

src/lib/select/select.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,12 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
637637

638638
/** Whether the select is in an error state. */
639639
_isErrorState(): boolean {
640-
const errorMatcher = this.errorStateMatcher || this._errorOptions.errorStateMatcher;
641-
return errorMatcher(this._control, this._parentFormGroup || this._parentForm);
640+
if (this.errorStateMatcher) {
641+
return this.errorStateMatcher(this._control, this._parentFormGroup || this._parentForm);
642+
}
643+
644+
return this._errorOptions.isErrorState(this._control,
645+
this._parentFormGroup || this._parentForm);
642646
}
643647

644648
/**

0 commit comments

Comments
 (0)