12
12
</h2 >
13
13
</v-col >
14
14
</v-row >
15
+ <!-- <v-row>
16
+ <v-col>
17
+ stepperMode: {{ stepperModel }}
18
+ <br />
19
+ currentPageHasErrors: {{ currentPageHasErrors }}
20
+ <br />
21
+ errorPageIndexes: {{ errorPageIndexes }}
22
+ <br />
23
+ triggerValidation: {{ triggerValidation }}
24
+ </v-col>
25
+ </v-row> -->
15
26
</v-container >
16
27
17
28
<v-container
28
39
<v-stepper-header >
29
40
<template
30
41
v-for =" (page , i ) in pages "
31
- :key =" ` ${i }-step ` "
42
+ :key =" ` ${getIndex ( i ) }-step ` "
32
43
>
33
44
<v-stepper-item
34
45
:color =" settings.color"
35
46
:disabled =" headerItemDisabled(page)"
36
47
:edit-icon =" page.isReview ? '$complete' : settings.editIcon"
37
48
:editable =" page.editable"
38
49
elevation =" 0"
50
+ :error =" page.error"
39
51
:title =" page.title"
40
52
:value =" getIndex(i)"
41
53
></v-stepper-item >
42
-
43
54
<v-divider
44
55
v-if =" getIndex(i) !== Object.keys(pages).length"
45
56
:key =" getIndex(i)"
51
62
<v-stepper-window >
52
63
<v-stepper-window-item
53
64
v-for =" page, i in pages"
54
- :key =" `${i }-content`"
65
+ :key =" `${getIndex(i) }-content`"
55
66
:reverse-transition =" transition"
56
67
:transition =" transition"
57
68
:value =" getIndex(i)"
58
69
>
59
70
<v-container >
60
71
<PageContainer
61
72
v-if =" !page.isReview"
73
+ :key =" `${getIndex(i)}-page`"
62
74
v-model =" modelValue"
63
75
:index =" getIndex(i)"
64
76
:page =" page"
65
77
:settings =" settings"
78
+ :triggerValidation =" triggerValidation"
66
79
:validateSchema =" validateSchema"
67
80
@next =" validatePage(next)"
68
- @validate =" onSubmit "
81
+ @validate =" onValidate "
69
82
/>
70
83
<PageReviewContainer
71
84
v-else
84
97
<v-stepper-actions v-if =" !settings.hideActions" >
85
98
<template #next >
86
99
<v-btn
100
+ v-if =" !lastPage"
87
101
:color =" settings.color"
88
- :disabled =" ((stepperActionsDisabled === 'next' || settings.disabled) as boolean) "
102
+ :disabled =" nextButtonDisabled "
89
103
:size =" navButtonSize"
90
104
@click =" validatePage(next)"
91
105
/>
106
+ <v-btn
107
+ :color =" settings.color"
108
+ :disabled =" fieldsHaveErrors && errorPageIndexes.includes(stepperModel - 1)"
109
+ :size =" navButtonSize"
110
+ type =" submit"
111
+ >Submit</v-btn >
92
112
</template >
93
113
94
114
<template #prev >
102
122
103
123
<v-row >
104
124
<v-col >
105
- <v-btn type =" submit" >Submit</v-btn >
106
125
</v-col >
107
126
</v-row >
108
127
</form >
@@ -151,8 +170,9 @@ const injectedOptions = inject(globalOptions, {});
151
170
const props = withDefaults (defineProps <Props >(), AllProps );
152
171
153
172
const stepperProps = reactive <Settings >(useMergeProps (attrs , injectedOptions , props ));
154
- const { direction, pages, title, width } = toRefs (props );
155
- const originalPages = JSON .parse (JSON .stringify (pages .value ));
173
+ const { direction, title, width } = toRefs (props );
174
+ const pages = reactive (props .pages );
175
+ const originalPages = JSON .parse (JSON .stringify (pages ));
156
176
157
177
const settings = ref <Settings >({
158
178
altLabels: stepperProps .altLabels ,
@@ -190,58 +210,14 @@ const settings = ref<Settings>({
190
210
console .log (' settings.value' , settings .value );
191
211
192
212
193
- // const { handleSubmit } = useForm({
194
- // validationSchema: toTypedSchema(
195
- // yupObject({
196
- // }),
197
- // ),
198
- // });
199
-
200
- // const onSubmit = handleSubmit(item => {
201
- // console.log('onSubmit handleSubmit', item);
202
- // });
203
-
204
213
const allFieldsArray = ref <Field []>([]);
205
214
206
- Object .values (pages . value ).forEach ((p : Page ) => {
215
+ Object .values (pages ).forEach ((p : Page ) => {
207
216
Object .values (p .fields ).forEach ((field : Field ) => {
208
217
allFieldsArray .value .push (field as Field );
209
218
});
210
219
});
211
220
212
- const validateSchema = useGetValidationSchema (allFieldsArray .value );
213
- console .log (' validateSchema' , validateSchema );
214
-
215
- // const initialValues = {};
216
- // allFieldsArray.value.forEach(item => {
217
- // initialValues[item.name] = item.label || "";
218
- // });
219
-
220
- // const yepSchema = allFieldsArray.value.reduce(useCreateYupSchema, { validationType: 'required', validations: allFieldsArray.value });
221
- // console.log('yepSchema', yepSchema);
222
- // const validateSchema = yupObject().shape(yepSchema);
223
- // console.log('validateSchema', validateSchema);
224
-
225
- // const foo = useCreateYupSchema(allFieldsArray.value, {});
226
-
227
- // console.log(foo);
228
-
229
- function onSubmit() {
230
- console .log (' onSubmit' );
231
- // useValidation({
232
- // fields: allFieldsArray.value,
233
- // });
234
- }
235
-
236
-
237
- // function validateField(field: Field) {
238
- // console.log('validateField', field);
239
- // console.log('allFieldsArray', allFieldsArray.value);
240
- // useValidation({
241
- // fields: allFieldsArray.value,
242
- // });
243
- // }
244
-
245
221
246
222
// const StepperComponent = markRaw(props.direction === 'vertical' ? VStepperVertical : VStepper);
247
223
// console.log('StepperComponent', StepperComponent);
@@ -256,9 +232,6 @@ onMounted(() => {
256
232
// });
257
233
258
234
summaryColumnErrorCheck ();
259
-
260
-
261
-
262
235
});
263
236
264
237
@@ -272,7 +245,7 @@ watchDeep(modelValue, () => {
272
245
const stepperModel = ref (1 );
273
246
274
247
watch (stepperModel , () => {
275
- if (stepperModel .value === pages .value . length ) {
248
+ if (stepperModel .value === pages .length ) {
276
249
formCompleted .value = true ;
277
250
}
278
251
});
@@ -287,18 +260,18 @@ const stepperActionsDisabled = computed(() => {
287
260
return stepperModel .value === 1 ? ' prev' : stepperModel .value === Object .keys (props .pages ).length ? ' next' : undefined ;
288
261
});
289
262
290
- // TODO: Make this disabled if the previous page is not editable //
291
- const canReviewPreviousButtonDisabled = computed (() => {
292
- // console.log('canReviewPreviousButtonDisabled');
263
+ const nextButtonDisabled = computed (() => {
264
+ const foo = ((stepperActionsDisabled .value === ' next' || settings .value .disabled ) as boolean );
293
265
294
- return stepperModel .value === pages .value .length && ! props .canReview ;
295
- });
266
+ const currentPageHasError = errorPageIndexes .value .includes (stepperModel .value - 1 );
296
267
297
- // const previousButtonDisabled = computed(() => {
298
- // return stepperModel.value === 1;
299
- // });
268
+ return currentPageHasError || foo ;
269
+ });
300
270
301
- // console.log('previousButtonDisabled', previousButtonDisabled.value);
271
+ // TODO: Make this disabled if the previous page is not editable //
272
+ const canReviewPreviousButtonDisabled = computed (() => {
273
+ return stepperModel .value === pages .length && ! props .canReview ;
274
+ });
302
275
303
276
function nextPage(next : () => void ): void {
304
277
console .log (' nextPage' , next );
@@ -316,6 +289,10 @@ function previousPage(prev: () => void): void {
316
289
prev ();
317
290
}
318
291
292
+ const lastPage = computed (() => {
293
+ return stepperModel .value === Object .keys (pages ).length ;
294
+ });
295
+
319
296
320
297
// TODO: This needs some more work and add a setting to not allow users to jump ahead in the questions //
321
298
function headerItemDisabled(page : Page ): boolean {
@@ -331,36 +308,159 @@ function headerItemDisabled(page: Page): boolean {
331
308
}
332
309
333
310
334
- // ------------------------------------------------ Callback & Validation //
311
+ // & ------------------------------------------------ Validation //
312
+ const validateSchema = useGetValidationSchema (allFieldsArray .value );
313
+ const triggerValidation = ref (false );
314
+ const fieldsHaveErrors = ref (false );
315
+
316
+ console .log (' validateSchema' , validateSchema );
317
+
318
+
319
+
320
+ // function validateButtonDisabled() {
321
+ // const foo = JSON.parse(JSON.stringify(pages));
322
+ // console.log('foo', foo);
323
+ // fieldsHaveErrors.value = Object.values(pages).some((page: Page) => Object.values(page.fields).some((field: Field) => field.error));
324
+
325
+ // console.log('fieldsHaveErrors', fieldsHaveErrors.value);
335
326
327
+ // }
328
+
329
+
330
+ // TODO: This needs to be fixed //
331
+ const currentPageHasErrors = ref (false );
332
+ const errorPageIndexes = ref <number []>([]);
336
333
337
- // const pagesValidation = ref((pages.value)
338
- // .map((_, i) => ({ page: i + 1, valid: false })));
334
+ function checkForPageErrors() {
335
+ errorPageIndexes .value = [];
336
+
337
+ Object .values (pages ).forEach ((p : Page , i : number ) => {
338
+ const errorField = Object .values (p .fields ).find ((field : Field ) => field .error );
339
+
340
+ if (errorField ) {
341
+ // eslint-disable-next-line no-param-reassign
342
+ p .error = true ;
343
+ errorPageIndexes .value .push (i );
344
+ return ;
345
+ }
346
+
347
+ // eslint-disable-next-line no-param-reassign
348
+ p .error = false ;
349
+ });
350
+
351
+ // Reset the error //
352
+ const stepperPage = pages [stepperModel .value - 1 ];
353
+
354
+ if (stepperPage ) {
355
+ stepperPage .error = false ;
356
+ }
357
+
358
+ currentPageHasErrors .value = errorPageIndexes .value .includes (stepperModel .value - 1 );
359
+ }
360
+
361
+
362
+ // TODO: Figure out why this triggers twice for each field on the page //
363
+ function onValidate(val ) {
364
+ console .group (' onValidate' );
365
+ console .log (val );
366
+
367
+ const { errors, fieldName } = val ;
368
+ let fieldHasErrors = false ;
369
+
370
+ if (errors ) {
371
+ fieldHasErrors = errors [fieldName ] || false ;
372
+ }
373
+
374
+ const fieldPageIdx = Object .values (pages ).findIndex ((page : Page ) => Object .values (page .fields ).find ((f : Field ) => f .name === fieldName ));
375
+ const page = pages [fieldPageIdx ];
376
+
377
+ console .log (' page' , page );
378
+
379
+ if (! page ) {
380
+ console .groupEnd ();
381
+
382
+ return ;
383
+ }
384
+
385
+ const field = page .fields .find ((f : Field ) => f .name === val .fieldName );
386
+
387
+ console .log (' field' , field );
388
+
389
+ if (! field ) {
390
+ console .groupEnd ();
391
+ return ;
392
+ }
393
+
394
+ console .log (' fieldHasErrors' , fieldHasErrors );
395
+
396
+ field .error = fieldHasErrors ? true : false ;
397
+ page .error = field .error ;
398
+ currentPageHasErrors .value = field .error ;
399
+ fieldsHaveErrors .value = field .error ;
400
+
401
+ console .log (' pages' , pages );
402
+
403
+ console .log (currentPageHasErrors .value );
404
+
405
+ checkForPageErrors ();
406
+
407
+ // Resets so it can be triggered again //
408
+ triggerValidation .value = false ;
409
+
410
+ console .groupEnd ();
411
+
412
+ // validateButtonDisabled();
413
+
414
+ }
415
+
416
+
417
+
418
+ function onSubmit(val ) {
419
+ triggerValidation .value = true ;
420
+ checkForPageErrors ();
421
+
422
+ console .log (' onSubmit' , val );
423
+ if (fieldsHaveErrors .value ) {
424
+ return ;
425
+ }
426
+ // useValidation({
427
+ // fields: allFieldsArray.value,
428
+ // });
429
+ }
339
430
340
- // console.log('pagesValidation', pagesValidation.value);
341
431
342
432
function validatePage(event : () => void ): void {
343
433
console .log (' validatePage' , event );
434
+ triggerValidation .value = true ;
344
435
345
- console . log ( ' stepperModel ' , stepperModel . value );
346
- nextPage ( event );
347
- }
436
+ setTimeout (() => {
437
+ // Resets so it can be triggered again //
438
+ triggerValidation . value = false ;
348
439
440
+ if (currentPageHasErrors .value ) {
441
+ return ;
442
+ }
349
443
444
+ nextPage (event );
445
+ }, 250 );
446
+
447
+ // nextPage(event);
448
+ }
350
449
351
450
451
+ // ------------------------------------------------ Callbacks //
352
452
function callbacks() {
353
453
whenCallback ();
354
454
}
355
455
356
456
357
457
// ------------------------ Conditional "when" callback //
358
458
function whenCallback() {
359
- Object .values (pages . value ).forEach ((page : Page , pageIdx : number ) => {
459
+ Object .values (pages ).forEach ((page : Page , pageIdx : number ) => {
360
460
Object .values (page .fields as Field []).forEach ((field : Field , fieldIdx ) => {
361
461
if (field .when ) {
362
462
const enabledField: boolean = field .when (modelValue .value );
363
- const indexPage = pages . value [pageIdx ];
463
+ const indexPage = pages [pageIdx ];
364
464
365
465
if (indexPage ?.fields [fieldIdx ]) {
366
466
indexPage .fields [fieldIdx ].type = enabledField ? originalPages [pageIdx ].fields [fieldIdx ].type : ' hidden' ;
0 commit comments