@@ -7,6 +7,7 @@ import { SelectMenuModule } from '../select-menu/select-menu.module';
77import { AutocompleteModule } from '../autocomplete/autocomplete.module' ;
88import { ButtonModule } from '../button/button.module' ;
99import { FormControl , FormGroup , ReactiveFormsModule } from '@angular/forms' ;
10+ import { StorybookTranslateModule } from '../../storybook-translate.module' ;
1011
1112export 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 */
5456const 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 */
6881const 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 */
88113const 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) => {
115154const 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