Skip to content

Commit 8e27c65

Browse files
Adding conditional paging
1 parent 3d7f5a5 commit 8e27c65

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/plugin/VStepperForm.vue

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<template #default="{ prev, next }">
3131
<v-stepper-header>
3232
<template
33-
v-for="(page, i) in pages"
33+
v-for="(page, i) in computedPages"
3434
:key="`${getIndex(i)}-step`"
3535
>
3636
<v-stepper-item
@@ -60,7 +60,7 @@
6060
>
6161
<v-stepper-window>
6262
<v-stepper-window-item
63-
v-for="page, i in pages"
63+
v-for="page, i in computedPages"
6464
:key="`${getIndex(i)}-content`"
6565
:reverse-transition="transitionComputed"
6666
:transition="transitionComputed"
@@ -92,7 +92,7 @@
9292
v-else
9393
v-model="modelValue"
9494
:page="page"
95-
:pages="pages"
95+
:pages="computedPages"
9696
:settings="settings"
9797
:summary-columns="summaryColumns"
9898
@goToQuestion="stepperModel = $event"
@@ -263,7 +263,7 @@ function previousPage(prev: () => void): void {
263263
}
264264
265265
const lastPage = computed<boolean>(() => {
266-
return stepperModel.value === Object.keys(pages).length;
266+
return stepperModel.value === Object.keys(computedPages.value).length;
267267
});
268268
269269
@@ -394,7 +394,25 @@ function callbacks() {
394394
whenCallback();
395395
}
396396
397-
// ------------------------ Conditional "when" callback //
397+
// ------------------------ Conditional "when" callbacks //
398+
const computedPages = computed<Page[]>(() => {
399+
Object.values(pages).forEach((page: Page, pageIdx: number) => {
400+
const localPage = page;
401+
localPage.visible = true;
402+
403+
if (localPage.when) {
404+
// eslint-disable-next-line no-unused-vars
405+
const enabledPage: boolean = (localPage.when as (value: any) => boolean)(modelValue.value);
406+
407+
if (pages[pageIdx]) {
408+
pages[pageIdx].visible = enabledPage;
409+
}
410+
}
411+
});
412+
413+
return pages.filter((p: Page) => p.visible);
414+
});
415+
398416
function whenCallback(): void {
399417
Object.values(pages).forEach((page: Page, pageIdx: number) => {
400418
if (page.fields) {

src/plugin/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ export interface Page {
163163
pageFieldColumns?: ResponsiveColumns;
164164
text?: string;
165165
title?: string;
166+
visible?: boolean;
167+
when?: (value: any) => boolean;
166168
}
167169

168170

0 commit comments

Comments
 (0)