Skip to content

Commit 8750247

Browse files
committed
style: form wrapper not setting correct error state
1 parent 9635d55 commit 8750247

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

libs/ui/documentation.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@
17001700
}
17011701
}
17021702
},
1703-
"templateData": "<label for=\"{{ name }}\" class=\"inline-flex\">\n <span class=\"mr-2 flex\">\n <input\n [attr.aria-describedby]=\"name + '-description'\"\n [attr.aria-label]=\"ariaLabel\"\n id=\"{{ name }}\"\n name=\"{{ name }}\"\n type=\"checkbox\"\n [checked]=\"checked\"\n [indeterminate]=\"indeterminate\"\n [ngClass]=\"checkboxClasses\"\n class=\"form-checkbox h-4 w-4 rounded border-gray-300 self-center\"\n (click)=\"onSelect()\"\n />\n </span>\n <span class=\"text-sm text-dark-100 font-medium leading-6 flex\">\n <ng-content select=\"label\"></ng-content>\n <ng-content select=\"icon\"></ng-content>\n <p id=\"{{ name }}-description\" class=\"m-0 font-normal text-gray-500\">\n <ng-content select=\"description\"></ng-content>\n </p>\n </span>\n</label>\n"
1703+
"templateData": "<label for=\"{{ name }}\" class=\"inline-flex\">\n <span class=\"mr-2 flex\">\n <input\n [attr.aria-describedby]=\"name + '-description'\"\n [attr.aria-label]=\"ariaLabel\"\n id=\"{{ name }}\"\n name=\"{{ name }}\"\n type=\"checkbox\"\n [checked]=\"checked\"\n [indeterminate]=\"indeterminate\"\n [ngClass]=\"checkboxClasses\"\n class=\"form-checkbox h-4 w-4 rounded border-gray-300 self-center disabled:bg-gray-50\"\n (click)=\"onSelect()\"\n [disabled]=\"disabled\"\n />\n </span>\n <span class=\"text-sm text-dark-100 font-medium leading-6 flex\">\n <ng-content select=\"label\"></ng-content>\n <ng-content select=\"icon\"></ng-content>\n <p id=\"{{ name }}-description\" class=\"m-0 font-normal text-gray-500\">\n <ng-content select=\"description\"></ng-content>\n </p>\n </span>\n</label>\n"
17041704
},
17051705
{
17061706
"name": "ChipComponent",
@@ -14469,7 +14469,7 @@
1446914469
"deprecated": false,
1447014470
"deprecationMessage": "",
1447114471
"type": "StoryFn<CheckboxComponent>",
14472-
"defaultValue": "(\n args: CheckboxComponent\n) => {\n const formGroup = new FormGroup({\n checkbox: new FormControl(false),\n });\n formGroup.get('checkbox')?.disable();\n args.variant = 'default';\n return {\n component: CheckboxComponent,\n template: `\n <form [formGroup]=\"formGroup\">\n <ui-checkbox [variant]=\"'${args.variant}'\" formControlName=\"checkbox\">\n <ng-container ngProjectAs=\"label\">Form control checkbox</ng-container>\n </ui-checkbox>\n </form>\n <br>\n <p>value: {{formGroup.get('checkbox').value}}</p>\n <p>touched: {{formGroup.get('checkbox').touched}}</p>\n `,\n props: {\n ...args,\n formGroup,\n },\n };\n}",
14472+
"defaultValue": "(\n args: CheckboxComponent\n) => {\n const formGroup = new FormGroup({\n checkbox: new FormControl(false),\n });\n args.variant = 'default';\n return {\n component: CheckboxComponent,\n template: `\n <form [formGroup]=\"formGroup\">\n <ui-checkbox [variant]=\"'${args.variant}'\" formControlName=\"checkbox\">\n <ng-container ngProjectAs=\"label\">Form control checkbox</ng-container>\n </ui-checkbox>\n </form>\n <br>\n <p>value: {{formGroup.get('checkbox').value}}</p>\n <p>touched: {{formGroup.get('checkbox').touched}}</p>\n `,\n props: {\n ...args,\n formGroup,\n },\n };\n}",
1447314473
"rawdescription": "Form control template checkbox",
1447414474
"description": "<p>Form control template checkbox</p>\n"
1447514475
},
@@ -15744,7 +15744,7 @@
1574415744
"deprecated": false,
1574515745
"deprecationMessage": "",
1574615746
"type": "StoryFn<CheckboxComponent>",
15747-
"defaultValue": "(args: CheckboxComponent) => {\n args.variant = 'default';\n return {\n component: CheckboxComponent,\n template: `\n <ui-checkbox [variant]=\"'${args.variant}'\">\n <ng-container ngProjectAs=\"label\">Checkbox text</ng-container>\n <ng-container ngProjectAs=\"description\">Description text text text.</ng-container>\n <ng-container ngProjectAs=\"icon\">\n <ui-icon\n icon=\"info_outline\"\n [inline]=\"true\"\n [size]=\"18\"\n variant=\"grey\"\n ></ui-icon>\n </ng-container>\n </ui-checkbox>\n `,\n props: {\n ...args,\n },\n };\n}",
15747+
"defaultValue": "(args: CheckboxComponent) => {\n args.variant = 'default';\n return {\n component: CheckboxComponent,\n template: `\n <ui-checkbox [variant]=\"'${args.variant}'\">\n <ng-container ngProjectAs=\"label\">Checkbox text</ng-container>\n <ng-container ngProjectAs=\"description\">Description text text text.</ng-container>\n <ng-container ngProjectAs=\"icon\">\n <ui-icon\n icon=\"info_outline\"\n [size]=\"18\"\n variant=\"grey\"\n ></ui-icon>\n </ng-container>\n </ui-checkbox>\n `,\n props: {\n ...args,\n },\n };\n}",
1574815748
"rawdescription": "Template checkbox",
1574915749
"description": "<p>Template checkbox</p>\n"
1575015750
},
@@ -18148,7 +18148,7 @@
1814818148
"deprecated": false,
1814918149
"deprecationMessage": "",
1815018150
"type": "StoryFn<CheckboxComponent>",
18151-
"defaultValue": "(\n args: CheckboxComponent\n) => {\n const formGroup = new FormGroup({\n checkbox: new FormControl(false),\n });\n formGroup.get('checkbox')?.disable();\n args.variant = 'default';\n return {\n component: CheckboxComponent,\n template: `\n <form [formGroup]=\"formGroup\">\n <ui-checkbox [variant]=\"'${args.variant}'\" formControlName=\"checkbox\">\n <ng-container ngProjectAs=\"label\">Form control checkbox</ng-container>\n </ui-checkbox>\n </form>\n <br>\n <p>value: {{formGroup.get('checkbox').value}}</p>\n <p>touched: {{formGroup.get('checkbox').touched}}</p>\n `,\n props: {\n ...args,\n formGroup,\n },\n };\n}",
18151+
"defaultValue": "(\n args: CheckboxComponent\n) => {\n const formGroup = new FormGroup({\n checkbox: new FormControl(false),\n });\n args.variant = 'default';\n return {\n component: CheckboxComponent,\n template: `\n <form [formGroup]=\"formGroup\">\n <ui-checkbox [variant]=\"'${args.variant}'\" formControlName=\"checkbox\">\n <ng-container ngProjectAs=\"label\">Form control checkbox</ng-container>\n </ui-checkbox>\n </form>\n <br>\n <p>value: {{formGroup.get('checkbox').value}}</p>\n <p>touched: {{formGroup.get('checkbox').touched}}</p>\n `,\n props: {\n ...args,\n formGroup,\n },\n };\n}",
1815218152
"rawdescription": "Form control template checkbox",
1815318153
"description": "<p>Form control template checkbox</p>\n"
1815418154
},
@@ -18172,7 +18172,7 @@
1817218172
"deprecated": false,
1817318173
"deprecationMessage": "",
1817418174
"type": "StoryFn<CheckboxComponent>",
18175-
"defaultValue": "(args: CheckboxComponent) => {\n args.variant = 'default';\n return {\n component: CheckboxComponent,\n template: `\n <ui-checkbox [variant]=\"'${args.variant}'\">\n <ng-container ngProjectAs=\"label\">Checkbox text</ng-container>\n <ng-container ngProjectAs=\"description\">Description text text text.</ng-container>\n <ng-container ngProjectAs=\"icon\">\n <ui-icon\n icon=\"info_outline\"\n [inline]=\"true\"\n [size]=\"18\"\n variant=\"grey\"\n ></ui-icon>\n </ng-container>\n </ui-checkbox>\n `,\n props: {\n ...args,\n },\n };\n}",
18175+
"defaultValue": "(args: CheckboxComponent) => {\n args.variant = 'default';\n return {\n component: CheckboxComponent,\n template: `\n <ui-checkbox [variant]=\"'${args.variant}'\">\n <ng-container ngProjectAs=\"label\">Checkbox text</ng-container>\n <ng-container ngProjectAs=\"description\">Description text text text.</ng-container>\n <ng-container ngProjectAs=\"icon\">\n <ui-icon\n icon=\"info_outline\"\n [size]=\"18\"\n variant=\"grey\"\n ></ui-icon>\n </ng-container>\n </ui-checkbox>\n `,\n props: {\n ...args,\n },\n };\n}",
1817618176
"rawdescription": "Template checkbox",
1817718177
"description": "<p>Template checkbox</p>\n"
1817818178
}

libs/ui/src/lib/form-wrapper/form-wrapper.directive.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,10 @@ export class FormWrapperDirective
284284
this.removeDisableState();
285285
}
286286
// Error state
287-
if (this.control.control.validator) {
288-
if (status === 'INVALID') {
289-
this.setInvalidState();
290-
} else {
291-
this.removeInvalidState();
292-
}
287+
if (status === 'INVALID') {
288+
this.setInvalidState();
289+
} else {
290+
this.removeInvalidState();
293291
}
294292
// Required state
295293
const isRequired = this.control?.control?.hasValidator(

libs/ui/src/lib/form-wrapper/form-wrapper.stories.ts

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SelectMenuModule } from '../select-menu/select-menu.module';
77
import { AutocompleteModule } from '../autocomplete/autocomplete.module';
88
import { ButtonModule } from '../button/button.module';
99
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
10+
import { StorybookTranslateModule } from '../../storybook-translate.module';
1011

1112
export default {
1213
title: 'Components/Form Wrapper',
@@ -22,6 +23,7 @@ export default {
2223
AutocompleteModule,
2324
ButtonModule,
2425
ReactiveFormsModule,
26+
StorybookTranslateModule,
2527
],
2628
}),
2729
],
@@ -47,18 +49,29 @@ const options = [
4749
];
4850

4951
/** Form group to test story with disable option */
50-
const formControl = new FormGroup({
52+
const formGroup = new FormGroup({
5153
name: new FormControl(''),
5254
});
5355
/** Callback to test story with disable option */
5456
const toggleDisabled = () => {
57+
const formControl = formGroup.controls.name;
5558
if (formControl.disabled) {
5659
formControl.enable();
5760
} else {
5861
formControl.disable();
5962
}
6063
};
6164

65+
/** Callback to set status of form control to invalid */
66+
const toggleInvalid = () => {
67+
const formControl = formGroup.controls.name;
68+
if (formControl.valid) {
69+
formControl.setErrors({ customError: true });
70+
} else {
71+
formControl.setErrors(null);
72+
}
73+
};
74+
6275
/**
6376
* Template to create form wrapper component's story
6477
*
@@ -67,14 +80,26 @@ const toggleDisabled = () => {
6780
*/
6881
const Template: StoryFn<any> = (args: any) => {
6982
return {
70-
template: `<div uiFormFieldDirective [outline]="${args.outline}">
83+
template: `
84+
<div [formGroup]="formGroup">
85+
<div uiFormFieldDirective [outline]="${args.outline}">
7186
<label>Phone Number</label>
72-
<input type="text" name="account-number" id="account-number" placeholder="000-00-0000"/>
87+
<input type="text" name="account-number" id="account-number" placeholder="000-00-0000" formControlName="name"/>
7388
<ui-spinner [size]="'medium'" uiSuffix></ui-spinner>
7489
<ui-icon icon="search" uiPrefix></ui-icon>
75-
</div>`,
90+
</div></div>
91+
<ui-button (click)="toggleDisabled()">
92+
Enable/disabled
93+
</ui-button>
94+
<ui-button (click)="toggleInvalid()">
95+
Valid/Invalid
96+
</ui-button>
97+
`,
7698
props: {
7799
...args,
100+
toggleInvalid,
101+
toggleDisabled,
102+
formGroup,
78103
},
79104
};
80105
};
@@ -87,9 +112,12 @@ const Template: StoryFn<any> = (args: any) => {
87112
*/
88113
const TemplateSelect: StoryFn<any> = (args: any) => {
89114
return {
90-
template: `<div uiFormFieldDirective [outline]="${args.outline}">
115+
template: `
116+
<div [formGroup]="formGroup">
117+
<div uiFormFieldDirective [outline]="${args.outline}">
91118
<label>Choose language</label>
92119
<ui-select-menu
120+
formControlName="name"
93121
[multiselect]="false"
94122
[disabled]="false">
95123
<ui-select-option *ngFor="let option of options" [value]="option">
@@ -98,10 +126,21 @@ const TemplateSelect: StoryFn<any> = (args: any) => {
98126
</ui-select-menu>
99127
<ui-spinner [size]="'medium'" uiSuffix></ui-spinner>
100128
<ui-icon icon="search" uiPrefix></ui-icon>
101-
</div>`,
129+
</div>
130+
</div>
131+
<ui-button (click)="toggleDisabled()">
132+
Enable/disabled
133+
</ui-button>
134+
<ui-button (click)="toggleInvalid()">
135+
Valid/Invalid
136+
</ui-button>
137+
`,
102138
props: {
103139
...args,
104140
options,
141+
toggleInvalid,
142+
toggleDisabled,
143+
formGroup,
105144
},
106145
};
107146
};
@@ -115,7 +154,7 @@ const TemplateSelect: StoryFn<any> = (args: any) => {
115154
const TemplateAutocomplete: StoryFn<any> = (args: any) => {
116155
return {
117156
template: `
118-
<div [formGroup]="formControl">
157+
<div [formGroup]="formGroup">
119158
<div uiFormFieldDirective [outline]="${args.outline}">
120159
<label>Choose language</label>
121160
<input
@@ -143,12 +182,16 @@ const TemplateAutocomplete: StoryFn<any> = (args: any) => {
143182
<ui-button (click)="toggleDisabled()">
144183
Enable/disabled
145184
</ui-button>
185+
<ui-button (click)="toggleInvalid()">
186+
Valid/Invalid
187+
</ui-button>
146188
`,
147189
props: {
148190
...args,
149191
options,
150192
toggleDisabled,
151-
formControl,
193+
toggleInvalid,
194+
formGroup,
152195
},
153196
};
154197
};

0 commit comments

Comments
 (0)