Skip to content

Commit 7d80476

Browse files
feat(section): show one page per section
A form with sections becomes a sequence of pages: each section starts a page, questions before the first section form the first one. Next checks only the questions of the current page, Back keeps the answers, and Submit appears on the last page of the respondent's path, which follows the answers as before. A form without sections stays a single page. A jump into the middle of a section adds its questions to the current page, so existing jumps keep working until jump targets are limited to the start of a section. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent 0d542a5 commit 7d80476

4 files changed

Lines changed: 179 additions & 9 deletions

File tree

‎playwright/e2e/sections.spec.ts‎

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ test.describe('Sections', () => {
8888
])
8989
})
9090

91-
test('A respondent sees the section and submits the questions around it', async ({
91+
test('A respondent goes through the sections page by page', async ({
9292
page,
9393
submitView,
9494
}) => {
@@ -103,14 +103,26 @@ test.describe('Sections', () => {
103103
})
104104
await page.goto(`apps/forms/${form.hash}/submit`)
105105

106+
await expect(submitView.submitButton).toBeVisible()
106107
await submitView.checkRadio('Do you have a pet?', 'Yes')
108+
await expect(submitView.getQuestion('About the pet')).toBeHidden()
109+
await submitView.nextButton.click()
110+
await expect(submitView.getQuestion('Do you have a pet?')).toBeHidden()
107111
await expect(submitView.getQuestion('About the pet')).toBeVisible()
108112
await expect(
109113
page.getByRole('listitem', { name: 'Question number 2' }),
110114
).toContainText('Which pet?')
111115
await submitView.fillText('Which pet?', 'Rex')
116+
117+
await submitView.backButton.click()
118+
await expect(
119+
submitView
120+
.getQuestion('Do you have a pet?')
121+
.getByRole('radio', { name: 'Yes' }),
122+
).toBeChecked()
112123
await submitView.checkRadio('Do you have a pet?', 'No')
113-
await expect(submitView.getQuestion('About the pet')).toBeHidden()
124+
await expect(submitView.getQuestion('Comments')).toBeVisible()
125+
await expect(submitView.nextButton).toHaveCount(0)
114126
await submitView.fillText('Comments', 'None')
115127

116128
const response = await submitView.submit()
@@ -119,4 +131,70 @@ test.describe('Sections', () => {
119131
expect(answers).not.toHaveProperty(String(which.id))
120132
await expect(submitView.successMessage).toBeVisible()
121133
})
134+
135+
test('Each page checks its required questions, an empty section is an info page', async ({
136+
page,
137+
submitView,
138+
}) => {
139+
const form = await api(page.request, 'POST', '/forms')
140+
const name = await api(page.request, 'POST', `/forms/${form.id}/questions`, {
141+
type: 'short',
142+
text: 'Your name',
143+
})
144+
const info = await api(page.request, 'POST', `/forms/${form.id}/questions`, {
145+
type: 'section',
146+
text: 'Before you continue',
147+
})
148+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
149+
type: 'section',
150+
text: 'Details',
151+
})
152+
const city = await api(page.request, 'POST', `/forms/${form.id}/questions`, {
153+
type: 'short',
154+
text: 'City',
155+
})
156+
for (const question of [name, city]) {
157+
await api(
158+
page.request,
159+
'PATCH',
160+
`/forms/${form.id}/questions/${question.id}`,
161+
{ keyValuePairs: { isRequired: true } },
162+
)
163+
}
164+
await api(page.request, 'PATCH', `/forms/${form.id}/questions/${info.id}`, {
165+
keyValuePairs: { description: 'The next page asks for details.' },
166+
})
167+
await page.goto(`apps/forms/${form.hash}/submit`)
168+
169+
await expect(submitView.backButton).toHaveCount(0)
170+
await submitView.nextButton.click()
171+
await expect(submitView.getQuestion('Your name')).toBeVisible()
172+
await expect(submitView.getQuestion('City')).toBeHidden()
173+
await submitView.fillText('Your name', 'Alex')
174+
await submitView.nextButton.click()
175+
176+
await expect(submitView.getQuestion('Before you continue')).toBeVisible()
177+
await expect(page.getByText('The next page asks for details.')).toBeVisible()
178+
await expect(submitView.getQuestion('Your name')).toBeHidden()
179+
await submitView.nextButton.click()
180+
181+
await expect(submitView.getQuestion('City')).toBeVisible()
182+
await expect(submitView.nextButton).toHaveCount(0)
183+
await submitView.backButton.click()
184+
await submitView.backButton.click()
185+
await expect(
186+
submitView.getQuestion('Your name').getByRole('textbox'),
187+
).toHaveValue('Alex')
188+
await submitView.nextButton.click()
189+
await submitView.nextButton.click()
190+
await submitView.fillText('City', 'Berlin')
191+
192+
const response = await submitView.submit()
193+
const { answers } = response.request().postDataJSON()
194+
expect(answers).toEqual({
195+
[name.id]: ['Alex'],
196+
[city.id]: ['Berlin'],
197+
})
198+
await expect(submitView.successMessage).toBeVisible()
199+
})
122200
})

‎playwright/support/sections/SubmitSection.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
import type { Locator, Page, Response } from '@playwright/test'
77

88
export class SubmitSection {
9+
public readonly backButton: Locator
910
public readonly clearFormButton: Locator
11+
public readonly nextButton: Locator
1012
public readonly submitButton: Locator
1113
public readonly successMessage: Locator
1214

1315
constructor(public readonly page: Page) {
16+
this.backButton = this.page.getByRole('button', { name: 'Back' })
1417
this.clearFormButton = this.page.getByRole('button', { name: 'Clear form' })
18+
this.nextButton = this.page.getByRole('button', { name: 'Next' })
1519
this.submitButton = this.page.getByRole('button', { name: 'Submit' })
1620
this.successMessage = this.page
1721
.getByRole('main')

‎src/utils/Sections.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,20 @@ export function getNumbers(
3535
]),
3636
)
3737
}
38+
39+
/**
40+
* The pages of the questions on a path: a page starts at each section, questions before the first section form the first page
41+
*
42+
* @param questions the questions and sections on the path, in their order
43+
*/
44+
export function getPages<T extends { type: string }>(questions: T[]): T[][] {
45+
const pages: T[][] = []
46+
for (const question of questions) {
47+
if (question.type === QUESTION_TYPE_SECTION || pages.length === 0) {
48+
pages.push([question])
49+
} else {
50+
pages[pages.length - 1].push(question)
51+
}
52+
}
53+
return pages
54+
}

‎src/views/Submit.vue‎

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
<KeepAlive v-for="question in validQuestions" :key="question.id">
156156
<component
157157
:is="answerTypes[question.type].component"
158-
v-if="visibleQuestionIds.has(question.id)"
158+
v-if="pageQuestionIds.has(question.id)"
159159
ref="questionRefs"
160160
v-bind="question"
161161
:isRequired="
@@ -186,6 +186,16 @@
186186
</template>
187187
{{ t('forms', 'Clear form') }}
188188
</NcButton>
189+
<NcButton
190+
v-if="pageIndex > 0"
191+
class="submit-button back-button"
192+
type="button"
193+
@click="goToPage(pageIndex - 1)">
194+
<template #icon>
195+
<NcIconSvgWrapper directional :svg="IconArrowBackSvg" />
196+
</template>
197+
{{ t('forms', 'Back') }}
198+
</NcButton>
189199
<NcButton
190200
alignment="center-reverse"
191201
class="submit-button"
@@ -194,9 +204,13 @@
194204
type="submit"
195205
variant="primary">
196206
<template #icon>
197-
<NcIconSvgWrapper :svg="IconSendSvg" />
207+
<NcIconSvgWrapper
208+
:directional="!isLastPage"
209+
:svg="
210+
isLastPage ? IconSendSvg : IconArrowForwardSvg
211+
" />
198212
</template>
199-
{{ t('forms', 'Submit') }}
213+
{{ isLastPage ? t('forms', 'Submit') : t('forms', 'Next') }}
200214
</NcButton>
201215
</div>
202216
</form>
@@ -254,6 +268,8 @@ import type {
254268
GridQuestionValues,
255269
} from '../types/Entities.d.ts'
256270
271+
import IconArrowBack from '@material-symbols/svg-400/outlined/arrow_back.svg?raw'
272+
import IconArrowForward from '@material-symbols/svg-400/outlined/arrow_forward.svg?raw'
257273
import IconCancel from '@material-symbols/svg-400/outlined/block.svg?raw'
258274
import IconCheck from '@material-symbols/svg-400/outlined/check.svg?raw'
259275
import IconRefresh from '@material-symbols/svg-400/outlined/refresh.svg?raw'
@@ -306,7 +322,7 @@ import { PERMISSION_TYPES } from '../models/Permissions.ts'
306322
import { formatList, getJumps, getVisibleQuestions } from '../utils/Jumps.ts'
307323
import logger from '../utils/Logger.ts'
308324
import OcsResponse2Data from '../utils/OcsResponse2Data.ts'
309-
import { getNumbers, QUESTION_TYPE_SECTION } from '../utils/Sections.ts'
325+
import { getNumbers, getPages, QUESTION_TYPE_SECTION } from '../utils/Sections.ts'
310326
import SetWindowTitle from '../utils/SetWindowTitle.ts'
311327
312328
const formsAppName = 'forms'
@@ -508,6 +524,25 @@ export default defineComponent({
508524
509525
const shownNumbers = computed(() => getNumbers(shownQuestions.value))
510526
527+
const pages = computed<SubmitQuestion[][]>(() =>
528+
getPages(shownQuestions.value),
529+
)
530+
const currentPage = ref(0)
531+
const pageIndex = computed<number>(() =>
532+
Math.max(0, Math.min(currentPage.value, pages.value.length - 1)),
533+
)
534+
const pageQuestionIds = computed<Set<number>>(
535+
() =>
536+
new Set(
537+
(pages.value[pageIndex.value] ?? []).map(
538+
(question) => question.id,
539+
),
540+
),
541+
)
542+
const isLastPage = computed<boolean>(
543+
() => pageIndex.value >= pages.value.length - 1,
544+
)
545+
511546
const submittedAnswers = computed<AnswersMap>(() => {
512547
return Object.fromEntries(
513548
Object.entries(answers.value).filter(([questionId]) =>
@@ -1155,12 +1190,18 @@ export default defineComponent({
11551190
* Focus the first shown question whose earlier answer the form no longer offers
11561191
*/
11571192
async function focusAnswerToChooseAgain(): Promise<void> {
1158-
const index = shownQuestions.value.findIndex((question) =>
1159-
answersToChooseAgain.value.has(question.id),
1193+
const page = pages.value.findIndex((questions) =>
1194+
questions.some((question) =>
1195+
answersToChooseAgain.value.has(question.id),
1196+
),
11601197
)
1161-
if (index === -1) {
1198+
if (page === -1) {
11621199
return
11631200
}
1201+
currentPage.value = page
1202+
const index = pages.value[page].findIndex((question) =>
1203+
answersToChooseAgain.value.has(question.id),
1204+
)
11641205
await nextTick()
11651206
formElement.value
11661207
?.querySelectorAll(':scope > ul > li')
@@ -1240,6 +1281,7 @@ export default defineComponent({
12401281
(input) =>
12411282
!(input instanceof HTMLFieldSetElement)
12421283
&& (input as HTMLButtonElement).type !== 'reset'
1284+
&& !input.classList.contains('back-button')
12431285
&& !(
12441286
target.type === 'radio'
12451287
&& (input as HTMLInputElement).name === target.name
@@ -1248,6 +1290,23 @@ export default defineComponent({
12481290
?.focus()
12491291
}
12501292
1293+
/**
1294+
* Show a page of the path and move the focus to the heading of its first question or section
1295+
*
1296+
* @param index the index of the page
1297+
*/
1298+
async function goToPage(index: number): Promise<void> {
1299+
currentPage.value = index
1300+
await nextTick()
1301+
const heading =
1302+
formElement.value?.querySelector<HTMLElement>(':scope > ul > li h3')
1303+
if (heading) {
1304+
heading.tabIndex = -1
1305+
heading.focus({ preventScroll: true })
1306+
heading.closest('li')?.scrollIntoView({ block: 'start' })
1307+
}
1308+
}
1309+
12511310
let invalidShown = false
12521311
12531312
/**
@@ -1342,6 +1401,11 @@ export default defineComponent({
13421401
throw new Error('One question did not validate sucessfully')
13431402
}
13441403
1404+
if (!isLastPage.value) {
1405+
await goToPage(pageIndex.value + 1)
1406+
return
1407+
}
1408+
13451409
// in case no answer is set or all are empty show the confirmation dialog
13461410
if (
13471411
Object.keys(submittedAnswers.value).length === 0
@@ -1451,6 +1515,7 @@ export default defineComponent({
14511515
*/
14521516
function resetData(): void {
14531517
answers.value = {}
1518+
currentPage.value = 0
14541519
clearPathAnnouncement()
14551520
loading.value = false
14561521
showConfirmLeaveDialog.value = false
@@ -1615,6 +1680,12 @@ export default defineComponent({
16151680
validQuestions,
16161681
visibleQuestionIds,
16171682
title,
1683+
goToPage,
1684+
isLastPage,
1685+
pageIndex,
1686+
pageQuestionIds,
1687+
IconArrowBackSvg: IconArrowBack,
1688+
IconArrowForwardSvg: IconArrowForward,
16181689
IconCheckSvg: IconCheck,
16191690
IconScheduleSvg: IconSchedule,
16201691
IconRefreshSvg: IconRefresh,

0 commit comments

Comments
 (0)