Skip to content

Commit c439a61

Browse files
authored
feat: multiply css classes for control error component (#54)
1 parent 6f90fb9 commit c439a61

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ The directive will show all errors for a form field automatically in two instanc
107107

108108
## Inputs
109109

110-
- `controlErrorsClass` - A custom class that'll be added to the control error component, a component that is added after the form field when an error needs to be displayed:
110+
- `controlErrorsClass` - A custom classes that'll be added to the control error component, a component that is added after the form field when an error needs to be displayed:
111111

112112
```html
113113
<input class="form-control" formControlName="city"
114-
placeholder="City" controlErrorsClass="my-class" />
114+
placeholder="City" controlErrorsClass="my-class other-class" />
115115
```
116116

117117
- `controlErrorsTpl` - A custom error template to be used instead of the control error component's default view:

projects/ngneat/error-tailor/src/lib/control-error.component.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ describe('ControlErrorComponent', () => {
6363
expect(spectator.element).toHaveClass('customClassTest');
6464
});
6565

66+
it('should set multiply css classes on host element', () => {
67+
spectator.component.customClass = 'customClassTest1 customClassTest2';
68+
69+
expect(spectator.element).toHaveClass(['customClassTest1', 'customClassTest2']);
70+
});
71+
72+
it('should set multiply css classes as array on host element', () => {
73+
spectator.component.customClass = ['customClassTest1', 'customClassTest2'];
74+
75+
expect(spectator.element).toHaveClass(['customClassTest1', 'customClassTest2']);
76+
});
77+
6678
it('should create passed template and send its context', () => {
6779
const { component } = spectator;
6880
component.createTemplate('fakeTemplate' as any, { testError: 'test' }, 'test error');

projects/ngneat/error-tailor/src/lib/control-error.component.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ValidationErrors } from '@angular/forms';
44
export type ErrorComponentTemplate = TemplateRef<{ $implicit: ValidationErrors; text: string }>;
55

66
export interface ControlErrorComponent {
7-
customClass: string;
7+
customClass: string | string[];
88
text: string | null;
99
createTemplate?(tpl: ErrorComponentTemplate, error: ValidationErrors, text: string): void;
1010
}
@@ -40,8 +40,9 @@ export class DefaultControlErrorComponent implements ControlErrorComponent {
4040
this.cdr.markForCheck();
4141
}
4242

43-
set customClass(className: string) {
44-
this.host.nativeElement.classList.add(className);
43+
set customClass(classes: string | string[]) {
44+
const classesToAdd = Array.isArray(classes) ? classes : classes.split(/\s/);
45+
this.host.nativeElement.classList.add(...classesToAdd);
4546
}
4647

4748
set text(value: string | null) {

projects/ngneat/error-tailor/src/lib/control-error.directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { ErrorsMap } from './types';
2929
})
3030
export class ControlErrorsDirective implements OnInit, OnDestroy {
3131
@Input('controlErrors') customErrors: ErrorsMap = {};
32-
@Input() controlErrorsClass: string | undefined;
32+
@Input() controlErrorsClass: string | string[] | undefined;
3333
@Input() controlErrorsTpl: TemplateRef<any> | undefined;
3434
@Input() controlErrorsOnAsync = true;
3535
@Input() controlErrorsOnBlur = true;

0 commit comments

Comments
 (0)