Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit b2f1488

Browse files
fix: 🐛 providing errorsModule in the lazy app module
1 parent 87d624a commit b2f1488

16 files changed

+104
-101
lines changed

projects/ngx-errors/src/lib/error-state-matchers.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
ShowOnTouchedErrorStateMatcher,
1414
} from './error-state-matchers';
1515

16-
@Injectable({ providedIn: 'root' })
16+
@Injectable()
1717
export class ErrorStateMatchers {
1818
private matchers: { [key: string]: ErrorStateMatcher } = {};
1919

projects/ngx-errors/src/lib/error.directive.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,13 @@ describe('ErrorDirective', () => {
8383
Given(() => (showWhen = undefined as any));
8484

8585
function createDirectiveWithConfig(
86-
showErrorsWhenInput: string,
86+
showErrorsWhenInput: string | undefined,
8787
showMaxErrors?: number
8888
) {
89-
const config: IErrorsConfiguration = { showErrorsWhenInput };
89+
const config = new ErrorsConfiguration();
90+
if (showErrorsWhenInput) {
91+
config.showErrorsWhenInput = showErrorsWhenInput;
92+
}
9093
if (showMaxErrors !== undefined) {
9194
config.showMaxErrors = showMaxErrors;
9295
}

projects/ngx-errors/src/lib/error.directive.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ export class ErrorDirective implements AfterViewInit, OnDestroy {
4949
err: any = {};
5050

5151
constructor(
52+
private config: ErrorsConfiguration,
5253
private errorStateMatchers: ErrorStateMatchers,
5354
private overriddenShowWhen: OverriddenShowWhen,
5455
private cdr: ChangeDetectorRef,
5556
// ErrorsDirective is actually required.
5657
// use @Optional so that we can throw a custom error
57-
@Optional() private errorsDirective: ErrorsDirective,
58-
@Optional() private config: ErrorsConfiguration
58+
@Optional() private errorsDirective: ErrorsDirective
5959
) {}
6060

6161
ngAfterViewInit() {
@@ -180,7 +180,7 @@ export class ErrorDirective implements AfterViewInit, OnDestroy {
180180
return;
181181
}
182182

183-
this.showWhen = this.config?.showErrorsWhenInput ?? 'touched';
183+
this.showWhen = this.config.showErrorsWhenInput;
184184

185185
if (
186186
this.showWhen === 'formIsSubmitted' &&

projects/ngx-errors/src/lib/errors.directive.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from '@angular/forms';
88
import { createDirectiveFactory, SpectatorDirective } from '@ngneat/spectator';
99
import { first } from 'rxjs/operators';
10+
import { ErrorsConfiguration } from './errors-configuration';
1011
import { ErrorsDirective } from './errors.directive';
1112
import {
1213
ControlNotFoundError,
@@ -32,6 +33,7 @@ describe('ErrorsDirective ', () => {
3233
directive: ErrorsDirective,
3334
host: TestHostComponent,
3435
imports: [ReactiveFormsModule],
36+
providers: [ErrorsConfiguration],
3537
});
3638

3739
it('should throw if no control is provided', () => {

projects/ngx-errors/src/lib/errors.directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class ErrorsDirective implements AfterViewInit {
9393

9494
const visible =
9595
!errorCouldBeHidden &&
96-
(!this.config?.showMaxErrors ||
96+
(!this.config.showMaxErrors ||
9797
visibleCount <= this.config.showMaxErrors);
9898

9999
arr.push({ key, hidden: !visible });
@@ -107,7 +107,7 @@ export class ErrorsDirective implements AfterViewInit {
107107
constructor(
108108
@Optional() @SkipSelf() public parentForm: FormGroupDirective | null,
109109
@Optional() @SkipSelf() private parentFormGroupName: FormGroupName | null,
110-
@Optional() private config: ErrorsConfiguration
110+
private config: ErrorsConfiguration
111111
) {}
112112

113113
ngAfterViewInit() {

projects/ngx-errors/src/lib/errors.module.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ModuleWithProviders, NgModule } from '@angular/core';
1+
import { ModuleWithProviders, NgModule, Provider } from '@angular/core';
22
import { ReactiveFormsModule } from '@angular/forms';
33
import {
44
ErrorStateMatcher,
@@ -35,16 +35,19 @@ function mergeErrorsConfiguration(
3535
return { ...defaultConfig, ...config };
3636
}
3737

38+
export const ERROR_STATE_MATCHER_PROVIDERS: Provider[] = [
39+
ErrorStateMatchers,
40+
ShowOnTouchedErrorStateMatcher,
41+
ShowOnDirtyErrorStateMatcher,
42+
ShowOnTouchedAndDirtyErrorStateMatcher,
43+
ShowOnSubmittedErrorStateMatcher,
44+
];
45+
3846
@NgModule({
3947
imports: [ReactiveFormsModule],
4048
declarations: [...declarationsAndExports],
4149
exports: [...declarationsAndExports],
42-
providers: [
43-
ShowOnTouchedErrorStateMatcher,
44-
ShowOnDirtyErrorStateMatcher,
45-
ShowOnTouchedAndDirtyErrorStateMatcher,
46-
ShowOnSubmittedErrorStateMatcher,
47-
],
50+
providers: [ErrorsConfiguration, ...ERROR_STATE_MATCHER_PROVIDERS],
4851
})
4952
export class NgxErrorsModule {
5053
static configure(
@@ -53,6 +56,7 @@ export class NgxErrorsModule {
5356
return {
5457
ngModule: NgxErrorsModule,
5558
providers: [
59+
...ERROR_STATE_MATCHER_PROVIDERS,
5660
{
5761
provide: ErrorsConfiguration,
5862
useValue: mergeErrorsConfiguration(config),
Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,13 @@
1-
import { Injectable, NgModule } from '@angular/core';
2-
import {
3-
AbstractControl,
4-
FormGroupDirective,
5-
FormsModule,
6-
NgForm,
7-
} from '@angular/forms';
8-
import { MatInputModule } from '@angular/material/input';
1+
import { NgModule } from '@angular/core';
92
import { BrowserModule } from '@angular/platform-browser';
103
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
11-
import {
12-
CUSTOM_ERROR_STATE_MATCHERS,
13-
ErrorStateMatcher,
14-
NgxErrorsModule,
15-
} from '@ngspot/ngx-errors';
164
import { AppRoutingModule } from './app-routing.module';
175
import { AppComponent } from './app.component';
186
import { HomeComponent } from './home/home.component';
197

20-
@Injectable({ providedIn: 'root' })
21-
export class ShowOnDimaErrorStateMatcher implements ErrorStateMatcher {
22-
isErrorState(
23-
control: AbstractControl | null,
24-
_form: FormGroupDirective | NgForm | null
25-
): boolean {
26-
return !!(control && control.value === 'dima');
27-
}
28-
}
29-
30-
@Injectable({ providedIn: 'root' })
31-
export class ShowOnFiveErrorStateMatcher implements ErrorStateMatcher {
32-
isErrorState(
33-
control: AbstractControl | null,
34-
_form: FormGroupDirective | NgForm | null
35-
): boolean {
36-
return !!(control && control.value && control.value.length === 5);
37-
}
38-
}
39-
408
@NgModule({
41-
imports: [
42-
BrowserModule,
43-
AppRoutingModule,
44-
BrowserAnimationsModule,
45-
FormsModule,
46-
MatInputModule,
47-
NgxErrorsModule.configure({
48-
showErrorsWhenInput: 'dima',
49-
}),
50-
],
9+
imports: [BrowserModule, AppRoutingModule, BrowserAnimationsModule],
5110
declarations: [AppComponent, HomeComponent],
52-
providers: [
53-
{
54-
provide: CUSTOM_ERROR_STATE_MATCHERS,
55-
useFactory: (
56-
showOnDirtyErrorStateMatcher: ShowOnDimaErrorStateMatcher,
57-
showOnFiveErrorStateMatcher: ShowOnFiveErrorStateMatcher
58-
) => {
59-
return {
60-
dima: showOnDirtyErrorStateMatcher,
61-
five: showOnFiveErrorStateMatcher,
62-
};
63-
},
64-
deps: [ShowOnDimaErrorStateMatcher, ShowOnFiveErrorStateMatcher],
65-
},
66-
],
6711
bootstrap: [AppComponent],
6812
})
6913
export class AppModule {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './show-on-dima-error-state-matcher';
2+
export * from './show-on-five-error-state-matcher';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Provider } from '@angular/core';
2+
import { CUSTOM_ERROR_STATE_MATCHERS } from '@ngspot/ngx-errors';
3+
import { ShowOnDimaErrorStateMatcher } from './show-on-dima-error-state-matcher';
4+
import { ShowOnFiveErrorStateMatcher } from './show-on-five-error-state-matcher';
5+
6+
export const CUSTOM_ERROR_STATE_MATCHERS_PROVIDER: Provider = {
7+
provide: CUSTOM_ERROR_STATE_MATCHERS,
8+
useFactory: (
9+
showOnDirtyErrorStateMatcher: ShowOnDimaErrorStateMatcher,
10+
showOnFiveErrorStateMatcher: ShowOnFiveErrorStateMatcher
11+
) => {
12+
return {
13+
dima: showOnDirtyErrorStateMatcher,
14+
five: showOnFiveErrorStateMatcher,
15+
};
16+
},
17+
deps: [ShowOnDimaErrorStateMatcher, ShowOnFiveErrorStateMatcher],
18+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Injectable } from '@angular/core';
2+
import { AbstractControl, FormGroupDirective, NgForm } from '@angular/forms';
3+
import { ErrorStateMatcher } from '@ngspot/ngx-errors';
4+
5+
@Injectable({ providedIn: 'root' })
6+
export class ShowOnDimaErrorStateMatcher implements ErrorStateMatcher {
7+
isErrorState(
8+
control: AbstractControl | null,
9+
_form: FormGroupDirective | NgForm | null
10+
): boolean {
11+
return !!(control && control.value === 'dima');
12+
}
13+
}

0 commit comments

Comments
 (0)