4545</template >
4646
4747<script lang="ts">
48- import { defineComponent , reactive , ref } from ' vue' ;
48+ import { mockAsync } from ' @/core/utils/helpers' ;
49+ import { defineComponent , onMounted , reactive , ref } from ' vue' ;
4950import {
5051 TextInput ,
5152 SelectInput ,
@@ -60,16 +61,43 @@ import {
6061 pattern ,
6162 ColorInput ,
6263 NumberInput ,
64+ CustomInput ,
6365} from ' ../../src' ;
6466/* } from '../../dist/as-dynamic-forms.esm'; */
6567export default defineComponent ({
6668 name: ' app' ,
6769 setup() {
6870 const title = ref (' Vue Dynamic Forms' );
6971 const formValues = reactive ({});
72+
73+ const emailValidator: FormValidation = {
74+ validator: email ,
75+ text: ' Email format is incorrect' ,
76+ };
77+
78+ const passwordValidator: FormValidation = {
79+ validator: pattern (
80+ ' ^(?=.*[a-z])(?=.*[A-Z])(?=.*)(?=.*[#$^+=!*()@%&]).{8,10}$' ,
81+ ),
82+ text:
83+ ' Password must contain at least 1 Uppercase, 1 Lowercase, 1 number, 1 special character and min 8 characters max 10' ,
84+ };
85+
7086 const form = reactive ({
7187 id: ' example-form' ,
72- fieldOrder: [' name' ],
88+ fieldOrder: [
89+ ' name' ,
90+ ' email' ,
91+ ' password' ,
92+ ' console' ,
93+ ' games' ,
94+ ' stock' ,
95+ ' steps' ,
96+ ' character' ,
97+ ' awesomeness' ,
98+ ' color' ,
99+ ' customField1' ,
100+ ],
73101 fields: {
74102 name: {
75103 label: ' Name' ,
@@ -79,36 +107,20 @@ export default defineComponent({
79107 email: {
80108 label: ' Email' ,
81109 type: ' email' ,
110+ validations: [emailValidator ],
82111 } as EmailInput ,
83- },
84- /* fields: [
85- new TextInput({
86- label: 'Name',
87- value: 'Alvaro',
88- }),
89- new EmailInput({
90- label: 'Email',
91- validations: [new FormValidation(email, 'Email format is incorrect')],
92- }),
93- new PasswordInput({
112+ password: {
94113 label: ' Password' ,
95- validations: [
96- new FormValidation(
97- pattern(
98- '^(?=.*[a-z])(?=.*[A-Z])(?=.*)(?=.*[#$^+=!*()@%&]).{8,10}$',
99- ),
100- 'Password must contain at least 1 Uppercase, 1 Lowercase, 1 number, 1 special character and min 8 characters max 10',
101- ),
102- ],
103- }),
104- new NumberInput({
105- label: 'Number',
106- min: 5,
107- max: 60,
108- step: 5,
109- }),
110- new SelectInput<string>({
114+ type: ' password' ,
115+ validations: [passwordValidator ],
116+ } as PasswordInput ,
117+ stock: {
118+ label: ' Stock' ,
119+ type: ' number' ,
120+ } as NumberInput ,
121+ games: {
111122 label: ' Games' ,
123+ type: ' select' ,
112124 customClass: ' w-1/2' ,
113125 value: ' the-last-of-us' ,
114126 options: [
@@ -125,19 +137,28 @@ export default defineComponent({
125137 value: ' Nier Automata' ,
126138 },
127139 ],
128- }),
129- new TextAreaInput({
130- label: 'Bio',
131- cols: 20,
132- rows: 5,
133- }),
134- new CheckboxInput({
140+ } as SelectInput ,
141+ console: {
142+ label: ' Console (Async Options)' ,
143+ type: ' select' ,
144+ customClass: ' w-1/2' ,
145+ options: [],
146+ } as SelectInput ,
147+ steps: {
148+ label: ' Number' ,
149+ type: ' number' ,
150+ min: 5 ,
151+ max: 60 ,
152+ step: 5 ,
153+ value: 5 ,
154+ } as NumberInput ,
155+ awesomeness: {
135156 label: " Check if you're awesome" ,
136- name : 'awesomness ',
137- }) ,
138- new RadioInput( {
157+ type : ' checkbox ' ,
158+ } as CheckboxInput ,
159+ character: {
139160 label: ' Select one option' ,
140- name : 'character ',
161+ type : ' radio ' ,
141162 options: [
142163 {
143164 key: ' mario' ,
@@ -157,33 +178,62 @@ export default defineComponent({
157178 disabled: true ,
158179 },
159180 ],
160- }) ,
161- new InputBase<string>( {
181+ } as RadioInput ,
182+ customField1: {
162183 type: ' custom-field' ,
163184 label: ' Custom Field' ,
164- name: 'customField1',
165- }),
166- new ColorInput({
185+ } as CustomInput ,
186+ color: {
167187 label: ' Color' ,
168- name : 'color',
188+ type : ' color' ,
169189 value: ' #4DBA87' ,
170- }),
171- ],
172- */
190+ } as ColorInput ,
191+ },
173192 });
193+
174194 function handleSubmit(values ) {
175195 console .log (' Values Submitted' , values );
176196 }
197+
177198 function valueChanged(values ) {
178199 Object .assign (formValues , values );
179- setTimeout (() => {
180- form .fields .email .value = ' alvaro.saburido@gmail.com' ;
181- }, 2000 );
200+ console .log (' Values' , values );
182201 }
202+
183203 function handleError(errors ) {
184- console .error (errors );
204+ console .error (' Errors ' , errors );
185205 }
186206
207+ onMounted (async () => {
208+ setTimeout (() => {
209+ form .fields .email .value = ' alvaro.saburido@gmail.com' ;
210+ }, 2000 );
211+
212+ try {
213+ const consoleOptions = await mockAsync (true , 4000 , [
214+ {
215+ key: ' playstation' ,
216+ value: ' Playstation' ,
217+ },
218+ {
219+ key: ' nintendo' ,
220+ value: ' Nintendo' ,
221+ },
222+ {
223+ key: ' xbox' ,
224+ value: ' Xbox' ,
225+ },
226+ ]);
227+ form .fields .console .options = consoleOptions as {
228+ key: string ;
229+ value: string ;
230+ disabled? : boolean ;
231+ }[];
232+ } catch (e ) {
233+ console .error (e );
234+ }
235+ });
236+
187237 return {
188238 title ,
189239 form ,
0 commit comments