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 ,
5253 EmailInput ,
5354 FormValidation ,
5455 PasswordInput ,
55- TextAreaInput ,
5656 CheckboxInput ,
5757 RadioInput ,
58- InputBase ,
5958 email ,
6059 pattern ,
6160 ColorInput ,
6261 NumberInput ,
63- /* } from '../../src'; */
64- } from ' ../../dist/as-dynamic-forms.esm' ;
62+ CustomInput ,
63+ } from ' ../../src' ;
64+ /* } from '../../dist/as-dynamic-forms.esm'; */
6565export default defineComponent ({
6666 name: ' app' ,
6767 setup() {
6868 const title = ref (' Vue Dynamic Forms' );
6969 const formValues = reactive ({});
70+
71+ const emailValidator: FormValidation = {
72+ validator: email ,
73+ text: ' Email format is incorrect' ,
74+ };
75+
76+ const passwordValidator: FormValidation = {
77+ validator: pattern (
78+ ' ^(?=.*[a-z])(?=.*[A-Z])(?=.*)(?=.*[#$^+=!*()@%&]).{8,10}$' ,
79+ ),
80+ text:
81+ ' Password must contain at least 1 Uppercase, 1 Lowercase, 1 number, 1 special character and min 8 characters max 10' ,
82+ };
83+
7084 const form = reactive ({
7185 id: ' example-form' ,
72- fieldOrder: [' name' ],
86+ fieldOrder: [
87+ ' name' ,
88+ ' email' ,
89+ ' password' ,
90+ ' console' ,
91+ ' games' ,
92+ ' stock' ,
93+ ' steps' ,
94+ ' character' ,
95+ ' awesomeness' ,
96+ ' color' ,
97+ ' customField1' ,
98+ ],
7399 fields: {
74100 name: {
75101 label: ' Name' ,
@@ -79,36 +105,20 @@ export default defineComponent({
79105 email: {
80106 label: ' Email' ,
81107 type: ' email' ,
108+ validations: [emailValidator ],
82109 } 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({
110+ password: {
94111 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>({
112+ type: ' password' ,
113+ validations: [passwordValidator ],
114+ } as PasswordInput ,
115+ stock: {
116+ label: ' Stock' ,
117+ type: ' number' ,
118+ } as NumberInput ,
119+ games: {
111120 label: ' Games' ,
121+ type: ' select' ,
112122 customClass: ' w-1/2' ,
113123 value: ' the-last-of-us' ,
114124 options: [
@@ -125,19 +135,28 @@ export default defineComponent({
125135 value: ' Nier Automata' ,
126136 },
127137 ],
128- }),
129- new TextAreaInput({
130- label: 'Bio',
131- cols: 20,
132- rows: 5,
133- }),
134- new CheckboxInput({
138+ } as SelectInput ,
139+ console: {
140+ label: ' Console (Async Options)' ,
141+ type: ' select' ,
142+ customClass: ' w-1/2' ,
143+ options: [],
144+ } as SelectInput ,
145+ steps: {
146+ label: ' Number' ,
147+ type: ' number' ,
148+ min: 5 ,
149+ max: 60 ,
150+ step: 5 ,
151+ value: 5 ,
152+ } as NumberInput ,
153+ awesomeness: {
135154 label: " Check if you're awesome" ,
136- name : 'awesomness ',
137- }) ,
138- new RadioInput( {
155+ type : ' checkbox ' ,
156+ } as CheckboxInput ,
157+ character: {
139158 label: ' Select one option' ,
140- name : 'character ',
159+ type : ' radio ' ,
141160 options: [
142161 {
143162 key: ' mario' ,
@@ -157,33 +176,58 @@ export default defineComponent({
157176 disabled: true ,
158177 },
159178 ],
160- }) ,
161- new InputBase<string>( {
179+ } as RadioInput ,
180+ customField1: {
162181 type: ' custom-field' ,
163182 label: ' Custom Field' ,
164- name: 'customField1',
165- }),
166- new ColorInput({
183+ } as CustomInput ,
184+ color: {
167185 label: ' Color' ,
168- name : 'color',
186+ type : ' color' ,
169187 value: ' #4DBA87' ,
170- }),
171- ],
172- */
188+ } as ColorInput ,
189+ },
173190 });
191+
174192 function handleSubmit(values ) {
175193 console .log (' Values Submitted' , values );
176194 }
195+
177196 function valueChanged(values ) {
178197 Object .assign (formValues , values );
179- setTimeout (() => {
180- form .fields .email .value = ' alvaro.saburido@gmail.com' ;
181- }, 2000 );
198+ console .log (' Values' , values );
182199 }
200+
183201 function handleError(errors ) {
184- console .error (errors );
202+ console .error (' Errors ' , errors );
185203 }
186204
205+ onMounted (async () => {
206+ try {
207+ const consoleOptions = await mockAsync (true , 4000 , [
208+ {
209+ key: ' playstation' ,
210+ value: ' Playstation' ,
211+ },
212+ {
213+ key: ' nintendo' ,
214+ value: ' Nintendo' ,
215+ },
216+ {
217+ key: ' xbox' ,
218+ value: ' Xbox' ,
219+ },
220+ ]);
221+ form .fields .console .options = consoleOptions as {
222+ key: string ;
223+ value: string ;
224+ disabled? : boolean ;
225+ }[];
226+ } catch (e ) {
227+ console .error (e );
228+ }
229+ });
230+
187231 return {
188232 title ,
189233 form ,
0 commit comments