This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate with MatInput; support arbitrary showWhen
- Loading branch information
1 parent
27e6bbf
commit 55fd44a
Showing
21 changed files
with
555 additions
and
93 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
12 changes: 12 additions & 0 deletions
12
projects/ngx-errors/src/lib/custom-error-state-matchers.ts
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,12 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { ErrorStateMatcher } from '@angular/material/core'; | ||
|
||
export type CustomErrorStateMatchers = { [key: string]: ErrorStateMatcher }; | ||
|
||
/** | ||
* Provides a way to add to available options for when to display an error for | ||
* an invalid control. Options that come by default are | ||
* `'touched'`, `'dirty'`, `'touchedAndDirty'`, `'formIsSubmitted'`. | ||
*/ | ||
export const CUSTOM_ERROR_STATE_MATCHERS = | ||
new InjectionToken<CustomErrorStateMatchers>('CUSTOM_ERROR_STATE_MATCHERS'); |
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,81 @@ | ||
import { Inject, Injectable, Optional } from '@angular/core'; | ||
import { AbstractControl, FormGroupDirective, NgForm } from '@angular/forms'; | ||
import { | ||
ErrorStateMatcher, | ||
ShowOnDirtyErrorStateMatcher, | ||
} from '@angular/material/core'; | ||
import { | ||
CustomErrorStateMatchers, | ||
CUSTOM_ERROR_STATE_MATCHERS, | ||
} from './custom-error-state-matchers'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class ErrorStateMatchers { | ||
private matchers: { [key: string]: ErrorStateMatcher } = {}; | ||
|
||
constructor( | ||
showOnTouchedErrorStateMatcher: ShowOnTouchedErrorStateMatcher, | ||
showOnDirtyErrorStateMatcher: ShowOnDirtyErrorStateMatcher, | ||
showOnTouchedAndDirtyErrorStateMatcher: ShowOnTouchedAndDirtyErrorStateMatcher, | ||
showOnSubmittedErrorStateMatcher: ShowOnSubmittedErrorStateMatcher, | ||
@Optional() | ||
@Inject(CUSTOM_ERROR_STATE_MATCHERS) | ||
customErrorStateMatchers: CustomErrorStateMatchers | ||
) { | ||
this.matchers['touched'] = showOnTouchedErrorStateMatcher; | ||
this.matchers['dirty'] = showOnDirtyErrorStateMatcher; | ||
this.matchers['touchedAndDirty'] = showOnTouchedAndDirtyErrorStateMatcher; | ||
this.matchers['formIsSubmitted'] = showOnSubmittedErrorStateMatcher; | ||
if (customErrorStateMatchers) { | ||
this.matchers = { ...this.matchers, ...customErrorStateMatchers }; | ||
} | ||
} | ||
|
||
get(showWhen: string): ErrorStateMatcher | undefined { | ||
return this.matchers[showWhen]; | ||
} | ||
|
||
validKeys(): string[] { | ||
return Object.keys(this.matchers); | ||
} | ||
} | ||
|
||
@Injectable() | ||
export class ShowOnTouchedErrorStateMatcher { | ||
isErrorState( | ||
control: AbstractControl | null, | ||
form: FormGroupDirective | NgForm | null | ||
): boolean { | ||
return !!( | ||
control && | ||
control.invalid && | ||
(control.touched || (form && form.submitted)) | ||
); | ||
} | ||
} | ||
|
||
@Injectable() | ||
export class ShowOnTouchedAndDirtyErrorStateMatcher | ||
implements ErrorStateMatcher | ||
{ | ||
isErrorState( | ||
control: AbstractControl | null, | ||
form: FormGroupDirective | NgForm | null | ||
): boolean { | ||
return !!( | ||
control && | ||
control.invalid && | ||
((control.dirty && control.touched) || (form && form.submitted)) | ||
); | ||
} | ||
} | ||
|
||
@Injectable() | ||
export class ShowOnSubmittedErrorStateMatcher implements ErrorStateMatcher { | ||
isErrorState( | ||
control: AbstractControl | null, | ||
form: FormGroupDirective | NgForm | null | ||
): boolean { | ||
return !!(control && control.invalid && form && form.submitted); | ||
} | ||
} |
Oops, something went wrong.