Skip to content

Commit bbca4cb

Browse files
committed
refactor: preserve context when invoking the error state matcher
1 parent fe6e3d2 commit bbca4cb

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
@@ -898,7 +898,7 @@ describe('MdInputContainer with forms', () => {
898898
providers: [
899899
{
900900
provide: ErrorOptions,
901-
useValue: { errorStateMatcher: globalErrorStateMatcher } }
901+
useValue: { isErrorState: globalErrorStateMatcher } }
902902
]
903903
});
904904

@@ -929,7 +929,7 @@ describe('MdInputContainer with forms', () => {
929929
providers: [
930930
{
931931
provide: ErrorOptions,
932-
useValue: { errorStateMatcher: showOnDirtyErrorStateMatcher }
932+
useValue: { isErrorState: showOnDirtyErrorStateMatcher }
933933
}
934934
]
935935
});

src/lib/input/input-container.ts

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

306307
if (newState !== oldState) {
307308
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
@@ -2698,7 +2698,7 @@ describe('MdSelect', () => {
26982698

26992699
it('should be able to override the error matching behavior via the injection token', () => {
27002700
const errorOptions: ErrorOptions = {
2701-
errorStateMatcher: jasmine.createSpy('error state matcher').and.returnValue(true)
2701+
isErrorState: jasmine.createSpy('error state matcher').and.returnValue(true)
27022702
};
27032703

27042704
fixture.destroy();

src/lib/select/select.ts

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

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

645649
/**

0 commit comments

Comments
 (0)