99 @submited =" handleSubmit"
1010 @changed =" valueChanged"
1111 @error =" handleError"
12- />
12+ >
13+ <template
14+ v-slot :customField1 =" { control , onChange , onFocus , onBlur } "
15+ >
16+ <div class =" avocado-field" >
17+ <input
18+ v-if =" control"
19+ class =" form-control"
20+ v-model =" control.value"
21+ :type =" control.type"
22+ :name =" control.name"
23+ @change =" onChange"
24+ @focus =" onFocus"
25+ @blur =" onBlur"
26+ />
27+ <i >🥑</i >
28+ </div >
29+ </template >
30+ </dynamic-form >
1331 <button
1432 class =" btn bg-teal-500 text-white hover:bg-teal-700 mt-4"
1533 submit =" true"
2745</template >
2846
2947<script lang="ts">
30- import { defineComponent , reactive , ref } from ' vue' ;
31- import {
32- TextInput ,
33- SelectInput ,
34- EmailInput ,
35- FormValidation ,
36- PasswordInput ,
37- email ,
38- pattern ,
39- TextAreaInput ,
40- ColorInput ,
41- } from ' ../../src' ;
48+ import { mockAsync } from ' @/core/utils/helpers' ;
49+ import { defineComponent , onMounted , reactive , ref } from ' vue' ;
50+ import { email , pattern } from ' ../../src' ;
4251
4352export default defineComponent ({
4453 name: ' app' ,
4554 setup() {
4655 const title = ref (' Vue Dynamic Forms' );
4756 const formValues = reactive ({});
57+
58+ const emailValidator = {
59+ validator: email ,
60+ text: ' Email format is incorrect' ,
61+ };
62+
63+ const passwordValidator = {
64+ validator: pattern (
65+ ' ^(?=.*[a-z])(?=.*[A-Z])(?=.*)(?=.*[#$^+=!*()@%&]).{8,10}$' ,
66+ ),
67+ text:
68+ ' Password must contain at least 1 Uppercase, 1 Lowercase, 1 number, 1 special character and min 8 characters max 10' ,
69+ };
70+
4871 const form = reactive ({
4972 id: ' example-form' ,
50- fields: [
51- new TextInput ({
73+ fieldOrder: [
74+ ' name' ,
75+ ' email' ,
76+ ' password' ,
77+ ' console' ,
78+ ' games' ,
79+ ' stock' ,
80+ ' steps' ,
81+ ' character' ,
82+ ' awesomeness' ,
83+ ' color' ,
84+ ' customField1' ,
85+ ],
86+ fields: {
87+ name: {
5288 label: ' Name' ,
89+ type: ' text' ,
5390 value: ' Alvaro' ,
54- }) ,
55- new EmailInput ( {
91+ },
92+ email: {
5693 label: ' Email' ,
57- validations: [new FormValidation (email , ' Email format is incorrect' )],
58- }),
59- new PasswordInput ({
94+ type: ' email' ,
95+ validations: [emailValidator ],
96+ },
97+ password: {
6098 label: ' Password' ,
61- validations: [
62- new FormValidation (
63- pattern (
64- ' ^(?=.*[a-z])(?=.*[A-Z])(?=.*)(?=.*[#$^+=!*()@%&]).{8,10}$' ,
65- ),
66- ' Password must contain at least 1 Uppercase, 1 Lowercase, 1 number, 1 special character and min 8 characters max 10' ,
67- ),
68- ],
69- }),
70- new SelectInput <string >({
99+ type: ' password' ,
100+ validations: [passwordValidator ],
101+ },
102+ stock: {
103+ label: ' Stock' ,
104+ type: ' number' ,
105+ },
106+ games: {
71107 label: ' Games' ,
108+ type: ' select' ,
72109 customClass: ' w-1/2' ,
110+ value: ' the-last-of-us' ,
73111 options: [
74112 {
75113 key: ' the-last-of-us' ,
@@ -84,17 +122,58 @@ export default defineComponent({
84122 value: ' Nier Automata' ,
85123 },
86124 ],
87- }),
88- new TextAreaInput ({
89- label: ' Bio' ,
90- cols: 20 ,
91- rows: 5 ,
92- }),
93- new ColorInput ({
125+ },
126+ console: {
127+ label: ' Console (Async Options)' ,
128+ type: ' select' ,
129+ customClass: ' w-1/2' ,
130+ options: [],
131+ },
132+ steps: {
133+ label: ' Number' ,
134+ type: ' number' ,
135+ min: 5 ,
136+ max: 60 ,
137+ step: 5 ,
138+ value: 5 ,
139+ },
140+ awesomeness: {
141+ label: " Check if you're awesome" ,
142+ type: ' checkbox' ,
143+ },
144+ character: {
145+ label: ' Select one option' ,
146+ type: ' radio' ,
147+ options: [
148+ {
149+ key: ' mario' ,
150+ value: ' Mario' ,
151+ },
152+ {
153+ key: ' crash-bandicoot' ,
154+ value: ' Crash Bandicoot' ,
155+ },
156+ {
157+ key: ' sonic' ,
158+ value: ' Sonic' ,
159+ },
160+ {
161+ key: ' banjo-kazooie' ,
162+ value: ' Banjo Kazooie' ,
163+ disabled: true ,
164+ },
165+ ],
166+ },
167+ customField1: {
168+ type: ' custom-field' ,
169+ label: ' Custom Field' ,
170+ },
171+ color: {
94172 label: ' Color' ,
173+ type: ' color' ,
95174 value: ' #4DBA87' ,
96- }) ,
97- ] ,
175+ },
176+ } ,
98177 });
99178 function handleSubmit(values ) {
100179 console .log (' Values Submitted' , values );
@@ -107,6 +186,32 @@ export default defineComponent({
107186 alert (errors );
108187 }
109188
189+ onMounted (async () => {
190+ try {
191+ const consoleOptions = await mockAsync (true , 4000 , [
192+ {
193+ key: ' playstation' ,
194+ value: ' Playstation' ,
195+ },
196+ {
197+ key: ' nintendo' ,
198+ value: ' Nintendo' ,
199+ },
200+ {
201+ key: ' xbox' ,
202+ value: ' Xbox' ,
203+ },
204+ ]);
205+ form .fields .console .options = consoleOptions as {
206+ key: string ;
207+ value: string ;
208+ disabled? : boolean ;
209+ }[];
210+ } catch (e ) {
211+ console .error (e );
212+ }
213+ });
214+
110215 return {
111216 title ,
112217 form ,
@@ -118,4 +223,19 @@ export default defineComponent({
118223 },
119224});
120225 </script >
121- <style lang="scss"></style >
226+ <style lang="scss">
227+ .avocado-field {
228+ position : relative ;
229+
230+ .form-control {
231+ border-color : #aec64c ;
232+ background-color : #e2eb5d52 ;
233+ border-radius : 16px ;
234+ }
235+ i {
236+ position : absolute ;
237+ top : 5px ;
238+ right : 15px ;
239+ }
240+ }
241+ </style >
0 commit comments