45
45
</template >
46
46
47
47
<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' ;
49
50
import {
50
51
TextInput ,
51
52
SelectInput ,
52
53
EmailInput ,
53
54
FormValidation ,
54
55
PasswordInput ,
55
- TextAreaInput ,
56
56
CheckboxInput ,
57
57
RadioInput ,
58
- InputBase ,
59
58
email ,
60
59
pattern ,
61
60
ColorInput ,
62
61
NumberInput ,
63
- /* } from '../../src'; */
64
- } from ' ../../dist/as-dynamic-forms.esm' ;
62
+ CustomInput ,
63
+ } from ' ../../src' ;
64
+ /* } from '../../dist/as-dynamic-forms.esm'; */
65
65
export default defineComponent ({
66
66
name: ' app' ,
67
67
setup() {
68
68
const title = ref (' Vue Dynamic Forms' );
69
69
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
+
70
84
const form = reactive ({
71
85
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
+ ],
73
99
fields: {
74
100
name: {
75
101
label: ' Name' ,
@@ -79,36 +105,20 @@ export default defineComponent({
79
105
email: {
80
106
label: ' Email' ,
81
107
type: ' email' ,
108
+ validations: [emailValidator ],
82
109
} 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: {
94
111
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: {
111
120
label: ' Games' ,
121
+ type: ' select' ,
112
122
customClass: ' w-1/2' ,
113
123
value: ' the-last-of-us' ,
114
124
options: [
@@ -125,19 +135,28 @@ export default defineComponent({
125
135
value: ' Nier Automata' ,
126
136
},
127
137
],
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: {
135
154
label: " Check if you're awesome" ,
136
- name : 'awesomness ',
137
- }) ,
138
- new RadioInput( {
155
+ type : ' checkbox ' ,
156
+ } as CheckboxInput ,
157
+ character: {
139
158
label: ' Select one option' ,
140
- name : 'character ',
159
+ type : ' radio ' ,
141
160
options: [
142
161
{
143
162
key: ' mario' ,
@@ -157,33 +176,58 @@ export default defineComponent({
157
176
disabled: true ,
158
177
},
159
178
],
160
- }) ,
161
- new InputBase<string>( {
179
+ } as RadioInput ,
180
+ customField1: {
162
181
type: ' custom-field' ,
163
182
label: ' Custom Field' ,
164
- name: 'customField1',
165
- }),
166
- new ColorInput({
183
+ } as CustomInput ,
184
+ color: {
167
185
label: ' Color' ,
168
- name : 'color',
186
+ type : ' color' ,
169
187
value: ' #4DBA87' ,
170
- }),
171
- ],
172
- */
188
+ } as ColorInput ,
189
+ },
173
190
});
191
+
174
192
function handleSubmit(values ) {
175
193
console .log (' Values Submitted' , values );
176
194
}
195
+
177
196
function valueChanged(values ) {
178
197
Object .assign (formValues , values );
179
- setTimeout (() => {
180
- form .fields .email .value = ' alvaro.saburido@gmail.com' ;
181
- }, 2000 );
198
+ console .log (' Values' , values );
182
199
}
200
+
183
201
function handleError(errors ) {
184
- console .error (errors );
202
+ console .error (' Errors ' , errors );
185
203
}
186
204
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
+
187
231
return {
188
232
title ,
189
233
form ,
0 commit comments