12
12
</h2 >
13
13
</v-col >
14
14
</v-row >
15
- <!-- < v-row>
15
+ <v-row >
16
16
<v-col >
17
17
stepperMode: {{ stepperModel }}
18
18
<br />
19
19
currentPageHasErrors: {{ currentPageHasErrors }}
20
20
<br />
21
21
errorPageIndexes: {{ errorPageIndexes }}
22
- <br />
23
- triggerValidation: {{ triggerValidation }}
24
22
</v-col >
25
- </v-row> -->
23
+ </v-row >
26
24
</v-container >
27
25
28
26
<v-container
75
73
:index =" getIndex(i)"
76
74
:page =" page"
77
75
:settings =" settings"
78
- :triggerValidation =" triggerValidation"
79
76
:validateSchema =" validateSchema"
80
77
@next =" validatePage(next)"
81
- @validate =" onValidate"
78
+ @validate =" onValidate($event, next) "
82
79
/>
83
80
<PageReviewContainer
84
81
v-else
138
135
// import { VStepperVertical } from 'vuetify/labs/VStepperVertical';
139
136
import { AllProps } from ' ./utils/props' ;
140
137
import { useDisplay } from ' vuetify' ;
141
- import {
138
+ import type {
139
+ EmitValidateEventPayload ,
142
140
Field ,
143
141
Page ,
144
142
Props ,
143
+ SchemaField ,
145
144
Settings ,
146
145
SummaryColumns ,
147
146
} from ' @/plugin/types' ;
@@ -150,11 +149,15 @@ import {
150
149
useStepperContainerClasses ,
151
150
} from ' ./composables/classes' ;
152
151
import componentEmits from ' ./utils/emits' ;
152
+ import { TriggerValidationBus } from ' ./utils/globals' ;
153
153
import { globalOptions } from ' ./' ;
154
154
import PageContainer from ' ./components/shared/PageContainer.vue' ;
155
155
import PageReviewContainer from ' ./components/shared/PageReviewContainer.vue' ;
156
156
import { useMergeProps } from ' ./composables/helpers' ;
157
- import { watchDeep } from ' @vueuse/core' ;
157
+ import {
158
+ useEventBus ,
159
+ watchDeep ,
160
+ } from ' @vueuse/core' ;
158
161
import {
159
162
useGetValidationSchema ,
160
163
} from ' ./composables/validation' ;
@@ -308,146 +311,118 @@ function headerItemDisabled(page: Page): boolean {
308
311
}
309
312
310
313
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
314
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
315
325
- // console.log('fieldsHaveErrors', fieldsHaveErrors.value);
316
+ // & ------------------------------------------------ Validation //
317
+ const validateSchema = useGetValidationSchema (allFieldsArray .value as SchemaField []);
318
+ const fieldsHaveErrors = ref (false );
319
+ let triggerValidationBus: unknown | any ;
326
320
327
- // }
321
+ onMounted (() => {
322
+ triggerValidationBus = useEventBus <string >(TriggerValidationBus );
323
+ });
328
324
329
325
330
- // TODO: This needs to be fixed //
331
326
const currentPageHasErrors = ref (false );
332
327
const errorPageIndexes = ref <number []>([]);
333
328
334
- function checkForPageErrors() {
335
- errorPageIndexes .value = [];
336
329
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
- }
330
+ function setPageToError(pageIndex : EmitValidateEventPayload [' pageIndex' ], page ? : Page ): void {
331
+ currentPageHasErrors .value = true ;
346
332
333
+ if (page ) {
347
334
// 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 ;
335
+ page .error = true ;
356
336
}
357
337
358
- currentPageHasErrors .value = errorPageIndexes .value .includes (stepperModel .value - 1 );
338
+ if (! errorPageIndexes .value .includes (pageIndex )) {
339
+ errorPageIndexes .value .push (pageIndex );
340
+ }
359
341
}
360
342
361
343
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 );
344
+ function onValidate(payload : EmitValidateEventPayload , event : () => void ) {
345
+ // console.group('onValidate');
346
+ // console.log('payload', payload);
366
347
367
- const { errors, fieldName } = val ;
368
- let fieldHasErrors = false ;
369
-
370
- if (errors ) {
371
- fieldHasErrors = errors [fieldName ] || false ;
372
- }
348
+ const page = pages [payload .pageIndex ];
373
349
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 );
350
+ // console.log('page', page);
378
351
379
352
if (! page ) {
380
- console .groupEnd ();
381
-
382
353
return ;
383
354
}
384
355
385
- const field = page .fields .find ((f : Field ) => f .name === val .fieldName );
356
+ const currentFieldIndex = page .fields .findIndex ((f ) => f .name === payload .fieldName );
386
357
387
- console .log (' field' , field );
388
-
389
- if (! field ) {
390
- console .groupEnd ();
358
+ // Do not continue if field is not the last field //
359
+ if (currentFieldIndex !== page .fields .length - 1 ) {
391
360
return ;
392
361
}
393
362
394
- console . log ( ' fieldHasErrors ' , fieldHasErrors ) ;
363
+ let fieldsError = false ;
395
364
396
- field .error = fieldHasErrors ? true : false ;
397
- page .error = field .error ;
398
- currentPageHasErrors .value = field .error ;
399
- fieldsHaveErrors .value = field .error ;
365
+ Object .values (page .fields ).forEach ((field : Field ) => {
366
+ if (field .error === true || ! Object .prototype .hasOwnProperty .call (field , ' error' )) {
367
+ fieldsError = true ;
368
+ }
369
+ });
400
370
401
- console .log (' pages' , pages );
402
371
403
- console .log (currentPageHasErrors .value );
372
+ if (fieldsError ) {
373
+ // page.error = payload.error;
374
+ setPageToError (payload .pageIndex );
375
+ return ;
376
+ }
404
377
405
- checkForPageErrors ();
378
+ if (payload .action === ' page' ) {
379
+ page .error = payload .error === true ;
406
380
407
- // Resets so it can be triggered again //
408
- triggerValidation . value = false ;
381
+ if ( page . error ) {
382
+ setPageToError ( payload . pageIndex , page ) ;
409
383
410
- console .groupEnd ();
384
+ // console.groupEnd();
385
+ return ;
386
+ }
411
387
412
- // validateButtonDisabled();
388
+ currentPageHasErrors .value = false ;
389
+ }
413
390
414
- }
391
+ const index = errorPageIndexes . value . indexOf ( payload . pageIndex );
415
392
393
+ if (index > - 1 ) {
394
+ errorPageIndexes .value .splice (index , 1 );
395
+ }
416
396
417
397
418
- function onSubmit(val ) {
419
- triggerValidation .value = true ;
420
- checkForPageErrors ();
398
+ // debugger;
421
399
422
- console .log (' onSubmit' , val );
423
- if (fieldsHaveErrors .value ) {
400
+ // const field = page.fields.find((f) => f.name === payload.fieldName);
401
+
402
+ if (fieldsError ) {
403
+ // console.log('XXXX fieldsError', fieldsError);
404
+ // console.groupEnd();
424
405
return ;
425
406
}
426
- // useValidation({
427
- // fields: allFieldsArray.value,
428
- // });
429
- }
430
407
408
+ currentPageHasErrors .value = false ;
431
409
432
- function validatePage( event : () => void ) : void {
433
- console . log ( ' validatePage ' , event );
434
- triggerValidation . value = true ;
410
+ if ( payload . nextPage && stepperModel . value === payload . pageIndex + 1 ) {
411
+ nextPage ( event );
412
+ }
435
413
436
- setTimeout (() => {
437
- // Resets so it can be triggered again //
438
- triggerValidation .value = false ;
414
+ // console.groupEnd();
415
+ }
439
416
440
- if (currentPageHasErrors .value ) {
441
- return ;
442
- }
443
417
444
- nextPage (event );
445
- }, 250 );
418
+ function validatePage (event : () => void ) : void {
419
+ console . log ( ' validatePage ' , event );
446
420
447
- // nextPage(event);
421
+ triggerValidationBus .emit ({
422
+ pageIndex: stepperModel .value ,
423
+ });
448
424
}
449
425
450
-
451
426
// ------------------------------------------------ Callbacks //
452
427
function callbacks() {
453
428
whenCallback ();
@@ -473,11 +448,24 @@ function whenCallback() {
473
448
}
474
449
475
450
476
- // ------------------------------------------------ Submit Form //
451
+ // -------------------------------------------------- Submit //
452
+ function onSubmit(val ) {
453
+ // checkForPageErrors();
454
+
455
+ console .log (' onSubmit' , val );
456
+ if (fieldsHaveErrors .value ) {
457
+ return ;
458
+ }
459
+ // useValidation({
460
+ // fields: allFieldsArray.value,
461
+ // });
462
+ }
463
+
464
+
477
465
function submitForm() {
478
- console .log (' VStepperForm submitForm' );
466
+ // console.log('VStepperForm submitForm');
479
467
emit (' submit' );
480
- }
468
+ };
481
469
482
470
483
471
// ------------------------------------------------ Class & Styles //
0 commit comments