Skip to content

Commit ca9443d

Browse files
authored
Fixing enter keydown event on forms (#1653)
* link fix * Link fix * Fixing issue
1 parent c791fca commit ca9443d

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

ui.frontend/src/view/FormContainer.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,25 @@ class FormContainer {
4141
}
4242

4343
/**
44-
* Prevents the default behavior of the Enter key on components within the formContainer
44+
* Prevents the default behavior of the Enter key on text input components within the formContainer
4545
* from triggering a form submission and redirecting to the Thank-You Page.
46+
* Preserves accessibility by allowing Enter key to work normally on interactive elements.
4647
*/
4748
#preventDefaultSubmit(){
4849
if(this._element) {
4950
this._element.addEventListener('keydown', (event) => {
51+
if (event.key !== 'Enter') {
52+
return;
53+
}
54+
5055
const target = event.target;
51-
const isSubmitOrReset = target.tagName === 'INPUT' && (target.type === 'submit' || target.type === 'reset');
52-
if (event.key === 'Enter' && target.tagName !== 'SELECT' && target.tagName !== 'BUTTON' && !isSubmitOrReset) {
56+
57+
// Only prevent default on text-like inputs that would cause unwanted form submission
58+
// Allow Enter to work normally on buttons, links, elements with roles, and other interactive elements
59+
const shouldPreventSubmission = target.tagName === 'INPUT' &&
60+
['text', 'password', 'email', 'url', 'search', 'tel', 'number'].includes(target.type);
61+
62+
if (shouldPreventSubmission) {
5363
event.preventDefault();
5464
}
5565
});

ui.tests/test-module/specs/formcontainer.cy.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,5 +385,36 @@ describe('Page/Form Authoring', function () {
385385
cy.get('.cmp-adaptiveform-numberinput__widget').eq(0).should('be.visible');
386386
});
387387
});
388+
389+
it('should not prevent default behavior on links with enter key press', function () {
390+
cy.get('.cmp-adaptiveform-container').then((formContainer) => {
391+
// Create a test link dynamically within the form container
392+
const testLink = document.createElement('a');
393+
testLink.href = '#test-link';
394+
testLink.textContent = 'Test Link';
395+
testLink.id = 'test-accessibility-link';
396+
testLink.setAttribute('tabindex', '0');
397+
formContainer[0].appendChild(testLink);
398+
399+
// Test that Enter keydown event on link is NOT prevented by FormContainer
400+
cy.get('#test-accessibility-link').focus().then(($link) => {
401+
const linkElement = $link[0];
402+
403+
// Create and dispatch a keydown event
404+
const enterKeyEvent = new KeyboardEvent('keydown', {
405+
key: 'Enter',
406+
code: 'Enter',
407+
bubbles: true,
408+
cancelable: true
409+
});
410+
411+
// Dispatch the event and check if preventDefault was called
412+
const result = linkElement.dispatchEvent(enterKeyEvent);
413+
414+
// If result is true, preventDefault was NOT called (which is what we want for links)
415+
expect(result).to.be.true;
416+
});
417+
});
418+
});
388419
});
389420
});

0 commit comments

Comments
 (0)