From 03fbf316e5d5695fcdf7e8e8af1f009cb78665e4 Mon Sep 17 00:00:00 2001 From: Rishi Mehta Date: Wed, 16 Oct 2024 14:58:53 +0530 Subject: [PATCH] Allow press of enter on dropdown and submit --- ui.frontend/src/view/FormContainer.js | 4 ++- .../test-module/specs/formcontainer.cy.js | 26 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ui.frontend/src/view/FormContainer.js b/ui.frontend/src/view/FormContainer.js index bfa2a5e0ee..3c44ae9c8b 100644 --- a/ui.frontend/src/view/FormContainer.js +++ b/ui.frontend/src/view/FormContainer.js @@ -47,7 +47,9 @@ class FormContainer { #preventDefaultSubmit(){ if(this._element) { this._element.addEventListener('keydown', (event) => { - if (event.key === 'Enter') { + const target = event.target; + const isSubmitOrReset = target.tagName === 'INPUT' && (target.type === 'submit' || target.type === 'reset'); + if (event.key === 'Enter' && target.tagName !== 'SELECT' && target.tagName !== 'BUTTON' && !isSubmitOrReset) { event.preventDefault(); } }); diff --git a/ui.tests/test-module/specs/formcontainer.cy.js b/ui.tests/test-module/specs/formcontainer.cy.js index 71b881b0af..361ba46f93 100644 --- a/ui.tests/test-module/specs/formcontainer.cy.js +++ b/ui.tests/test-module/specs/formcontainer.cy.js @@ -290,16 +290,38 @@ describe('Page/Form Authoring', function () { context("Check default behaviour in Form Editor", function () { const pagePath = "/content/forms/af/core-components-it/samples/numberinput/validation.html"; - + let formContainerModel = null beforeEach(function () { - cy.previewForm(pagePath); + cy.previewForm(pagePath).then(p => { + formContainerModel = p; + }) }); it('check the preventDefaultSubmit method by simulating keydown event on the form', function () { cy.get('.cmp-adaptiveform-container').then((formContainer) => { + const [id, fieldView] = Object.entries(formContainerModel._fields)[0] + cy.stub(formContainer[0], 'onsubmit').as('submit'); cy.get('form').trigger('keydown', {key: 'Enter'}); cy.get('@submit').should('not.be.called'); + + // Trigger enter on a text input (should prevent submission) + cy.get('input[type="text"]').first().type('{enter}'); + cy.get(`#${id} .cmp-adaptiveform-numberinput__errormessage`).should('not.have.text', 'This is required numberinput'); + + + }); + }); + + + it('button click should work on press of enter key', function () { + cy.get('.cmp-adaptiveform-container').then((formContainer) => { + const [id, fieldView] = Object.entries(formContainerModel._fields)[0] + + // Trigger enter on a button (should not prevent submission) + cy.get('button').first().type('{enter}'); + cy.get(`#${id} .cmp-adaptiveform-numberinput__errormessage`).should('have.text', 'This is required numberinput'); + }); });