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 ,
@@ -60,16 +61,43 @@ import {
60
61
pattern ,
61
62
ColorInput ,
62
63
NumberInput ,
64
+ CustomInput ,
63
65
} from ' ../../src' ;
64
66
/* } from '../../dist/as-dynamic-forms.esm'; */
65
67
export default defineComponent ({
66
68
name: ' app' ,
67
69
setup() {
68
70
const title = ref (' Vue Dynamic Forms' );
69
71
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
+
70
86
const form = reactive ({
71
87
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
+ ],
73
101
fields: {
74
102
name: {
75
103
label: ' Name' ,
@@ -79,36 +107,20 @@ export default defineComponent({
79
107
email: {
80
108
label: ' Email' ,
81
109
type: ' email' ,
110
+ validations: [emailValidator ],
82
111
} 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: {
94
113
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: {
111
122
label: ' Games' ,
123
+ type: ' select' ,
112
124
customClass: ' w-1/2' ,
113
125
value: ' the-last-of-us' ,
114
126
options: [
@@ -125,19 +137,28 @@ export default defineComponent({
125
137
value: ' Nier Automata' ,
126
138
},
127
139
],
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: {
135
156
label: " Check if you're awesome" ,
136
- name : 'awesomness ',
137
- }) ,
138
- new RadioInput( {
157
+ type : ' checkbox ' ,
158
+ } as CheckboxInput ,
159
+ character: {
139
160
label: ' Select one option' ,
140
- name : 'character ',
161
+ type : ' radio ' ,
141
162
options: [
142
163
{
143
164
key: ' mario' ,
@@ -157,33 +178,62 @@ export default defineComponent({
157
178
disabled: true ,
158
179
},
159
180
],
160
- }) ,
161
- new InputBase<string>( {
181
+ } as RadioInput ,
182
+ customField1: {
162
183
type: ' custom-field' ,
163
184
label: ' Custom Field' ,
164
- name: 'customField1',
165
- }),
166
- new ColorInput({
185
+ } as CustomInput ,
186
+ color: {
167
187
label: ' Color' ,
168
- name : 'color',
188
+ type : ' color' ,
169
189
value: ' #4DBA87' ,
170
- }),
171
- ],
172
- */
190
+ } as ColorInput ,
191
+ },
173
192
});
193
+
174
194
function handleSubmit(values ) {
175
195
console .log (' Values Submitted' , values );
176
196
}
197
+
177
198
function valueChanged(values ) {
178
199
Object .assign (formValues , values );
179
- setTimeout (() => {
180
- form .fields .email .value = ' [email protected] ' ;
181
- }, 2000 );
200
+ console .log (' Values' , values );
182
201
}
202
+
183
203
function handleError(errors ) {
184
- console .error (errors );
204
+ console .error (' Errors ' , errors );
185
205
}
186
206
207
+ onMounted (async () => {
208
+ setTimeout (() => {
209
+ form .fields .email .value = ' [email protected] ' ;
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
+
187
237
return {
188
238
title ,
189
239
form ,
0 commit comments