Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 145 additions & 1 deletion playwright/e2e/submit-form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { APIRequestContext } from '@playwright/test'
import type { APIRequestContext, Page } from '@playwright/test'

import { expect, mergeTests } from '@playwright/test'
import { test as formTest } from '../support/fixtures/form.ts'
Expand Down Expand Up @@ -308,3 +308,147 @@ test('Enter in the last answer moves on to the submit button', async ({
await submitView.getQuestion('Your name').getByRole('textbox').press('Enter')
await expect(submitView.submitButton).toBeFocused()
})

test('Shift+Enter moves on and Ctrl+Enter or Cmd+Enter submits the form', async ({
page,
submitView,
}) => {
const form = await api(page.request, 'POST', '/forms')
await api(page.request, 'PATCH', `/forms/${form.id}`, {
keyValuePairs: { title: 'Ctrl+Enter form' },
})
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
type: 'multiple_unique',
text: 'Favourite color',
}).then((question) =>
api(
page.request,
'POST',
`/forms/${form.id}/questions/${question.id}/options`,
{
optionTexts: ['Red', 'Blue'],
},
),
)
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
type: 'short',
text: 'Your name',
})
await page.goto(`apps/forms/${form.hash}/submit`)

await submitView.checkRadio('Favourite color', 'Red')
await submitView.fillText('Your name', 'Alice')
const name = submitView.getQuestion('Your name').getByRole('textbox')
await name.press('Shift+Enter')
await expect(submitView.submitButton).toBeFocused()
await expect(submitView.successMessage).toBeHidden()
await name.press('Control+Enter')
await expect(submitView.successMessage).toBeVisible()

await page.goto(`apps/forms/${form.hash}/submit`)
await submitView.checkRadio('Favourite color', 'Blue')
await submitView
.getQuestion('Favourite color')
.getByRole('radio', { name: 'Blue' })
.press('Control+Enter')
await expect(submitView.successMessage).toBeVisible()

await page.goto(`apps/forms/${form.hash}/submit`)
await submitView.fillText('Your name', 'Bob')
await submitView
.getQuestion('Your name')
.getByRole('textbox')
.press('Meta+Enter')
await expect(submitView.successMessage).toBeVisible()
})

/**
* Create a form with the short answers "City" and "Country" and open it for submission
*
* @param page the page to open the form in
*/
async function openImeForm(page: Page) {
const form = await api(page.request, 'POST', '/forms')
for (const text of ['City', 'Country']) {
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
type: 'short',
text,
})
}
await page.goto(`apps/forms/${form.hash}/submit`)
}

test('Enter that confirms IME input neither moves on nor submits', async ({
page,
submitView,
}) => {
await openImeForm(page)
const submissions: string[] = []
page.on('request', (request) => {
if (request.url().includes('/submissions')) {
submissions.push(request.method())
}
})

const city = submitView.getQuestion('City').getByRole('textbox')
await city.fill('東京')
for (const composition of [{ isComposing: true }, { keyCode: 229 }]) {
for (const ctrlKey of [false, true]) {
await city.evaluate(
(input, { composition, ctrlKey }) => {
const event = new KeyboardEvent('keydown', {
key: 'Enter',
code: 'Enter',
ctrlKey,
isComposing: composition.isComposing ?? false,
bubbles: true,
cancelable: true,
})
if (composition.keyCode) {
Object.defineProperty(event, 'keyCode', {
value: composition.keyCode,
})
}
input.dispatchEvent(event)
},
{ composition, ctrlKey },
)
await expect(city).toBeFocused()
}
}

await city.press('Enter')
await expect(
submitView.getQuestion('Country').getByRole('textbox'),
).toBeFocused()
await expect(submitView.successMessage).toBeHidden()
expect(submissions).toEqual([])
})

test('Enter with the IME key code 229 keeps the focus in the answer', async ({
browserName,
page,
submitView,
}) => {
test.skip(
browserName !== 'chromium',
'Trusted IME key events need the Chromium DevTools protocol',
)

await openImeForm(page)
const city = submitView.getQuestion('City').getByRole('textbox')
await city.fill('東京')
const client = await page.context().newCDPSession(page)
await client.send('Input.dispatchKeyEvent', {
type: 'rawKeyDown',
key: 'Enter',
code: 'Enter',
windowsVirtualKeyCode: 229,
})
await expect(city).toBeFocused()

await city.press('Enter')
await expect(
submitView.getQuestion('Country').getByRole('textbox'),
).toBeFocused()
})
3 changes: 1 addition & 2 deletions src/components/Questions/QuestionLinearScale.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@
type="radio"
:required="isRequired"
@invalid.prevent="validate"
@update:modelValue="onChange"
@keydown.enter.exact.prevent="onKeydownEnter" />
@update:modelValue="onChange" />
</div>
</fieldset>
<template v-if="!readOnly">
Expand Down
10 changes: 2 additions & 8 deletions src/components/Questions/QuestionLong.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
:name="name || undefined"
@invalid.prevent="validate"
@input="onInput"
@keypress="autoSizeText"
@keydown.ctrl.enter="onKeydownCtrlEnter" />
@keypress="autoSizeText" />
</div>
<template #insert>
<slot name="insert" />
Expand All @@ -56,7 +55,7 @@ export default defineComponent({
},

props: QUESTION_PROPS,
emits: [...QUESTION_EMITS, 'update:values', 'keydown'],
emits: [...QUESTION_EMITS, 'update:values'],

setup(props, { emit }) {
const textarea = ref<HTMLTextAreaElement | null>(null)
Expand Down Expand Up @@ -123,10 +122,6 @@ export default defineComponent({
emit('update:values', [field.value])
}

const onKeydownCtrlEnter = (event: KeyboardEvent): void => {
emit('keydown', event)
}

return {
...question,
rootElement,
Expand All @@ -136,7 +131,6 @@ export default defineComponent({
autoSizeText,
validate,
onInput,
onKeydownCtrlEnter,
}
},
})
Expand Down
6 changes: 2 additions & 4 deletions src/components/Questions/QuestionMultiple.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@
:type="isUnique ? 'radio' : 'checkbox'"
:required="checkRequired"
@invalid.prevent="validate"
@update:modelValue="onChange"
@keydown.enter.exact.prevent="onKeydownEnter">
@update:modelValue="onChange">
{{ answer.text }}
</NcCheckboxRadioSwitch>
<div v-if="allowOtherAnswer" class="question__other-answer">
Expand All @@ -102,8 +101,7 @@
:required="checkRequired"
class="question__label"
@invalid.prevent="validate"
@update:modelValue="onChangeOther"
@keydown.enter.exact.prevent="onKeydownEnter">
@update:modelValue="onChangeOther">
{{ t('forms', 'Other:') }}
</NcCheckboxRadioSwitch>
<NcInputField
Expand Down
3 changes: 1 addition & 2 deletions src/components/Questions/QuestionShort.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
:type="validationObject.inputType"
:step="validationObject.inputType === 'number' ? 'any' : undefined"
@invalid.prevent="validate"
@input="onInput"
@keydown.enter.exact.prevent="onKeydownEnter" />
@input="onInput" />
<NcActions
v-if="!readOnly"
:id="validationTypeMenuId"
Expand Down
6 changes: 0 additions & 6 deletions src/composables/useQuestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const QUESTION_EMITS = [
'update:values',
'delete',
'clone',
'keydown',
'moveDown',
'moveUp',
]
Expand Down Expand Up @@ -257,10 +256,6 @@ export function useQuestion(props: QuestionPropsLike, options: UseQuestionOption
options.emit('clone')
}

const onKeydownEnter = (event: KeyboardEvent): void => {
options.emit('keydown', event)
}

const focus = (): void => {
const raw = options.rootElement?.value
const element = (raw && '$el' in raw ? raw.$el : raw) as
Expand Down Expand Up @@ -383,7 +378,6 @@ export function useQuestion(props: QuestionPropsLike, options: UseQuestionOption
onValuesChange,
onDelete,
onClone,
onKeydownEnter,
focus,
shuffleArray,
saveQuestionProperty,
Expand Down
58 changes: 36 additions & 22 deletions src/views/Submit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
v-else
ref="formElement"
@invalid.capture="onInvalid"
@keydown.enter="onKeydownEnter"
@submit.prevent="onSubmit">
<ul>
<component
Expand All @@ -159,8 +160,6 @@
:index="index + 1"
:maxStringLengths="maxStringLengths"
:values="answers[question.id]"
@keydown.enter="onKeydownEnter"
@keydown.ctrl.enter="onKeydownCtrlEnter"
@update:values="
(values: AnswerValue) => onUpdate(question, values)
" />
Expand All @@ -181,6 +180,7 @@
<NcButton
alignment="center-reverse"
class="submit-button"
aria-keyshortcuts="Control+Enter Meta+Enter"
:disabled="loading"
type="submit"
variant="primary">
Expand Down Expand Up @@ -971,24 +971,47 @@ export default defineComponent({
}

/**
* On Enter, focus next form-element, skipping the button that clears the form
* Last form element is the submit button, the form submits on enter then
* Submit the form on Ctrl+Enter or Cmd+Enter in any input or text area.
* On Enter with other or no modifiers in a single-line input, focus the
* next form control instead of submitting, skipping field sets and the
* button that clears the form. Enter that confirms an IME composition
* is left to the input method.
*
* @param event The fired event.
*/
function onKeydownEnter(
event: KeyboardEvent & { originalTarget?: EventTarget | null },
): void {
function onKeydownEnter(event: KeyboardEvent): void {
const target = event.target
if (
event.defaultPrevented
|| event.isComposing
|| event.keyCode === 229
|| !(
target instanceof HTMLInputElement
|| target instanceof HTMLTextAreaElement
)
) {
return
}
if (event.ctrlKey || event.metaKey) {
event.preventDefault()
formElement.value?.requestSubmit()
return
}
if (target instanceof HTMLTextAreaElement || target.type === 'file') {
return
}
event.preventDefault()

const formInputs = Array.from(
formElement.value?.elements ?? [],
) as HTMLElement[]
const sourceInputIndex = formInputs.findIndex(
(input) => input === (event.originalTarget ?? event.target),
)

formInputs
.slice(sourceInputIndex + 1)
.find((input) => (input as HTMLButtonElement).type !== 'reset')
.slice(formInputs.indexOf(target) + 1)
.find(
(input) =>
!(input instanceof HTMLFieldSetElement)
&& (input as HTMLButtonElement).type !== 'reset',
)
?.focus()
}

Expand All @@ -1015,14 +1038,6 @@ export default defineComponent({
input.closest('.question')?.scrollIntoView({ block: 'center' })
}

/**
* Ctrl+Enter typically fires submit on forms.
* Some inputs do automatically, while some need explicit handling
*/
function onKeydownCtrlEnter(): void {
formElement.value?.requestSubmit()
}

/*
* Methods for catching unwanted unload events
*/
Expand Down Expand Up @@ -1335,7 +1350,6 @@ export default defineComponent({
isMaxSubmissionsReached,
loading,
onInvalid,
onKeydownCtrlEnter,
onKeydownEnter,
onSubmit,
onUpdate,
Expand Down
Loading