forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(input): add custom error state matcher (angular#4750)
* feat(input): Add custom error state matcher * Address comments * Address comments pt. 2 * Use FormControl and only one of incompatible form options * Remove unnecesary async tests and const declarations * Add jsdoc comments to error state matchers
- Loading branch information
1 parent
5295602
commit adbcd67
Showing
7 changed files
with
255 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {InjectionToken} from '@angular/core'; | ||
import {FormControl, FormGroupDirective, Form, NgForm} from '@angular/forms'; | ||
|
||
/** Injection token that can be used to specify the global error options. */ | ||
export const MD_ERROR_GLOBAL_OPTIONS = new InjectionToken<ErrorOptions>('md-error-global-options'); | ||
|
||
export type ErrorStateMatcher = | ||
(control: FormControl, form: FormGroupDirective | NgForm) => boolean; | ||
|
||
export interface ErrorOptions { | ||
errorStateMatcher?: ErrorStateMatcher; | ||
} | ||
|
||
/** Returns whether control is invalid and is either touched or is a part of a submitted form. */ | ||
export function defaultErrorStateMatcher(control: FormControl, form: FormGroupDirective | NgForm) { | ||
const isSubmitted = form && form.submitted; | ||
return !!(control.invalid && (control.touched || isSubmitted)); | ||
} | ||
|
||
/** Returns whether control is invalid and is either dirty or is a part of a submitted form. */ | ||
export function showOnDirtyErrorStateMatcher(control: FormControl, | ||
form: FormGroupDirective | NgForm) { | ||
const isSubmitted = form && form.submitted; | ||
return !!(control.invalid && (control.dirty || isSubmitted)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters