Skip to content

Commit 848801a

Browse files
committed
feat(ngxErrors): exportAs directive, expose error methods, deprecate “ngErrors” selector
1 parent 262329b commit 848801a

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

example/app/stock-inventory/components/stock-branch/stock-branch.component.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@
1414
line-height: 1;
1515
padding: 6px 10px;
1616
margin-top: -1px;
17+
}
18+
.required {
19+
border: 1px solid #B52D30;
1720
}

example/app/stock-inventory/components/stock-branch/stock-branch.component.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { FormGroup } from '@angular/forms';
88
<div [formGroup]="parent">
99
<div formGroupName="store">
1010
11-
<input type="text" placeholder="Branch ID" formControlName="branch">
11+
<div>
12+
<input type="text" placeholder="Branch ID" formControlName="branch">
13+
</div>
1214
1315
<div ngxErrors="store.branch">
1416
<div class="error" ngxError="required" [when]="['dirty', 'touched']">
@@ -21,14 +23,23 @@ import { FormGroup } from '@angular/forms';
2123
Unknown branch, please check the ID
2224
</div>
2325
</div>
26+
27+
<h1>
28+
<p>hasErrors: {{ store.hasErrors | json }}</p>
29+
<p>errors: {{ store.errors | json }}</p>
30+
<p>getError('minlength'): {{ store.getError('minlength') | json }}</p>
31+
<p>hasError('required'): {{ store.hasError('required') | json }}</p>
32+
</h1>
2433
25-
<input type="text" placeholder="Manager Code" formControlName="code">
34+
<div>
35+
<input [ngStyle]="{ borderColor: store.hasError('required') ? 'red' : 'grey' }" type="text" placeholder="Manager Code" formControlName="code">
36+
</div>
2637
27-
<div ngxErrors="store.code">
38+
<div ngxErrors="store.code" #store="ngxErrors">
2839
<div class="error" ngxError="required" [when]="['touched']">
2940
Manager ID is required
3041
</div>
31-
<div class="error" [ngxError]="['minlength', 'maxlength']" [when]="['dirty', 'touched']">
42+
<div class="error" [ngxError]="['minlength', 'maxlength']" [when]="['dirty']">
3243
Minlength is 2, max length is 5
3344
</div>
3445
</div>

src/ngxerror.directive.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@ import { ErrorOptions } from './ngxerrors';
1515
import { toArray } from './utils/toArray';
1616

1717
@Directive({
18-
selector: '[ngError],[ngxError]'
18+
selector: '[ngxError]'
1919
})
2020
export class NgxErrorDirective implements OnInit, OnDestroy, DoCheck {
2121

2222
@Input() set ngxError(value: ErrorOptions) {
2323
this.errorNames = toArray(value);
2424
}
2525

26-
@Input() set ngError(value: ErrorOptions) {
27-
this.errorNames = toArray(value);
28-
console.warn('Warning: You are using the [ngErrors] directive which has been deprecated and will be removed in the next release. Use [ngxErrors] instead.');
29-
}
30-
3126
@Input() set when(value: ErrorOptions) {
3227
this.rules = toArray(value);
3328
}
@@ -55,6 +50,7 @@ export class NgxErrorDirective implements OnInit, OnDestroy, DoCheck {
5550
this.states = this._states.asObservable().distinctUntilChanged();
5651

5752
const errors = this.ngxErrors.subject
53+
.filter(Boolean)
5854
.filter(obj => this.errorNames.includes(obj.errorName));
5955

6056
const states = this.states

src/ngxerrors.directive.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,62 @@
1-
import { Directive, Input, OnInit } from '@angular/core';
1+
import { Directive, Input, OnChanges, SimpleChanges } from '@angular/core';
22
import { FormGroupDirective, AbstractControl } from '@angular/forms';
33

44
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
55

66
import { ErrorDetails } from './ngxerrors';
77

88
@Directive({
9-
selector: '[ngErrors],[ngxErrors]'
9+
selector: '[ngxErrors]',
10+
exportAs: 'ngxErrors'
1011
})
11-
export class NgxErrorsDirective implements OnInit {
12+
export class NgxErrorsDirective implements OnChanges {
1213

1314
@Input('ngxErrors')
1415
controlName: string;
1516

16-
@Input('ngErrors') set ngErrors(value) {
17-
this.controlName = value;
18-
console.warn('Warning: You are using the [ngErrors] directive which has been deprecated and will be removed in the next release. Use [ngxErrors] instead.');
19-
}
20-
21-
subject: BehaviorSubject<ErrorDetails>;
17+
subject = new BehaviorSubject<ErrorDetails>(null);
2218

2319
control: AbstractControl;
2420

21+
done: boolean = false;
22+
2523
constructor(
2624
private form: FormGroupDirective
2725
) {}
2826

29-
ngOnInit() {
30-
this.subject = new BehaviorSubject<ErrorDetails>(null);
31-
this.control = this.form.control.get(this.controlName);
32-
this.control.statusChanges.subscribe(this.checkStatus.bind(this));
33-
this.checkStatus();
27+
get errors() {
28+
if (!this.done) return;
29+
return this.control.errors;
30+
}
31+
32+
get hasErrors() {
33+
return !!this.errors;
34+
}
35+
36+
hasError(name: string) {
37+
if (!this.done) return;
38+
return this.control.hasError(name);
39+
}
40+
41+
getError(name: string) {
42+
if (!this.done) return;
43+
return this.control.getError(name);
44+
}
45+
46+
ngAfterViewInit() {
47+
setTimeout(() => {
48+
this.checkStatus();
49+
this.control.statusChanges.subscribe(this.checkStatus.bind(this));
50+
});
51+
}
52+
53+
ngOnChanges(changes: SimpleChanges) {
54+
this.control = this.form.control.get(changes.controlName.currentValue);
3455
}
3556

3657
checkStatus() {
3758
const errors = this.control.errors;
59+
this.done = true;
3860
if (!errors) return;
3961
for (const error in errors) {
4062
this.subject.next({ control: this.control, errorName: error });

0 commit comments

Comments
 (0)