3535 >
3636 <v-stepper-item
3737 :color =" settings.color"
38- :disabled =" headerItemDisabled(page)"
3938 :edit-icon =" page.isReview ? '$complete' : settings.editIcon"
40- :editable =" page.editable "
39+ :editable =" headerItemDisabled( page) "
4140 elevation =" 0"
4241 :error =" page.error"
4342 :title =" page.title"
4443 :value =" getIndex(i)"
4544 ></v-stepper-item >
4645 <v-divider
47- v-if =" getIndex(i) !== Object.keys(pages ).length"
46+ v-if =" getIndex(i) !== Object.keys(computedPages ).length"
4847 :key =" getIndex(i)"
4948 ></v-divider >
5049 </template >
@@ -247,9 +246,14 @@ const nextButtonDisabled = computed(() => {
247246 return fieldsHaveErrors .value || isDisabled ;
248247});
249248
250- // TODO: Make this disabled if the previous page is not editable //
251249const canReviewPreviousButtonDisabled = computed <boolean >(() => {
252- return stepperModel .value === pages .length && ! props .canReview ;
250+ const previousPage = computedPages .value [stepperModel .value - 2 ];
251+
252+ if (previousPage ) {
253+ return previousPage .editable === false ;
254+ }
255+
256+ return stepperModel .value === computedPages .value .length && ! props .editable ;
253257});
254258
255259
@@ -269,12 +273,23 @@ const lastPage = computed<boolean>(() => {
269273
270274// TODO: This needs some more work and add a setting to not allow users to jump ahead in the questions //
271275function headerItemDisabled(page : Page ): boolean {
272- const totalSteps = Object .keys (pages ).length ;
276+ const totalSteps = Object .keys (computedPages . value ).length ;
273277 const lastStep = totalSteps - 1 ;
274278
275- // If you're on the last page
279+ if (page .editable === false && currentPageHasErrors .value ) {
280+ return true ;
281+ }
282+
283+ const currentPageIdx = stepperModel .value - 1 ;
284+ const pageIdx = computedPages .value .findIndex ((p ) => p === page );
285+
286+ if (page .editable !== false && pageIdx < currentPageIdx ) {
287+ return true ;
288+ }
289+
290+ // If you're on the last step (not review page) //
276291 if (stepperModel .value === lastStep ) {
277- return ! page .isReview && (! settings .value .canReview ) && (! page .editable && settings .value ?.editable !== false );
292+ return ! page .isReview && (! settings .value .editable ) && (! page .editable && settings .value ?.editable !== false );
278293 }
279294
280295 return false ;
@@ -297,6 +312,8 @@ function runValidation(
297312 validate ()
298313 .then ((response : ValidateResult ) => {
299314 const errors = response .errors as unknown as ValidateResult [' errors' ];
315+
316+
300317 checkForPageErrors (errors , source , next );
301318 })
302319 .catch ((error : Error ) => {
@@ -321,13 +338,13 @@ function removePageError(pageIndex: number): void {
321338// ------------------------ Check the if the page has errors //
322339function checkForPageErrors(errors : ValidateResult [' errors' ], source : string , next = () => { }): void {
323340 const currentPage = stepperModel .value - 1 ;
324- const page = pages [currentPage ];
341+ const page = computedPages . value [currentPage ];
325342
326343 if (! page ) {
327344 return ;
328345 }
329346
330- const pageIndex = pages .findIndex ((p ) => p === page );
347+ const pageIndex = computedPages . value .findIndex ((p ) => p === page );
331348 const pageFields = page ?.fields ?? [];
332349 const hasErrorInField = Object .keys (errors as object []).some (errorKey => pageFields .some (field => field .name === errorKey ));
333350
@@ -364,16 +381,36 @@ function setPageToError(pageIndex: number, page?: Page, source = 'submit'): void
364381let debounceTimer: ReturnType <typeof setTimeout >;
365382
366383function onFieldValidate(field : Field , next : () => void ): void {
367- const errors = parentForm .value ?.errors as unknown as ValidateResult [' errors' ];
384+ const errors = parentForm .value ?.errors as ValidateResult [' errors' ];
368385 const shouldAutoPage = (field .autoPage || settings .value .autoPage ? next : null ) as () => void ;
369386
370- // If autoPage debounce next //
387+ // If autoPage //
371388 if (field ?.autoPage || settings .value ?.autoPage ) {
372- clearTimeout (debounceTimer );
373389
374- debounceTimer = setTimeout (() => {
375- checkForPageErrors (errors , ' field' , shouldAutoPage );
376- }, (field ?.autoPageDelay ?? settings .value ?.autoPageDelay ));
390+ if (parentForm .value ) {
391+ // First validate the page before proceeding to the next page //
392+ (parentForm .value as { validate: () => Promise <ValidateResult >; }).validate ()
393+ .then ((res : ValidateResult ) => {
394+ if (res .valid ) {
395+ // debounce next //
396+ clearTimeout (debounceTimer );
397+ debounceTimer = setTimeout (() => {
398+ checkForPageErrors (errors , ' field' , shouldAutoPage );
399+ }, (field ?.autoPageDelay ?? settings .value ?.autoPageDelay ));
400+
401+ return ;
402+ }
403+
404+ const currentPage = stepperModel .value - 1 ;
405+ const page = computedPages .value [currentPage ];
406+ const pageIndex = computedPages .value .findIndex ((p ) => p === page );
407+
408+ setPageToError (pageIndex , page , ' validating' );
409+ })
410+ .catch ((error : Error ) => {
411+ console .error (' Error' , error );
412+ });
413+ }
377414
378415 return ;
379416 }
@@ -384,7 +421,6 @@ function onFieldValidate(field: Field, next: () => void): void {
384421
385422// -------------------------------------------------- Submit //
386423function onSubmit(values : any ): void {
387- console .log (' %c%s' , ' color: #00ff00; font-weight: bold;' , ' onSubmit SUBMIT FORM \n ' , values );
388424 emit (' submit' , values );
389425}
390426
@@ -414,12 +450,12 @@ const computedPages = computed<Page[]>(() => {
414450});
415451
416452function whenCallback(): void {
417- Object .values (pages ).forEach ((page : Page , pageIdx : number ) => {
453+ Object .values (computedPages . value ).forEach ((page : Page , pageIdx : number ) => {
418454 if (page .fields ) {
419455 Object .values (page .fields ).forEach ((field : Field , fieldIdx ) => {
420456 if (field .when ) {
421457 const enabledField: boolean = field .when (modelValue .value );
422- const indexPage = pages [pageIdx ];
458+ const indexPage = computedPages . value [pageIdx ];
423459
424460 if (indexPage ?.fields && indexPage ?.fields [fieldIdx ]) {
425461 indexPage .fields [fieldIdx ].type = enabledField ? originalPages [pageIdx ].fields [fieldIdx ].type : ' hidden' ;
0 commit comments