Skip to content

Commit 7990a21

Browse files
fix: prevent default action of Enter key in login form (#12273) (#12283)
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
1 parent d479a32 commit 7990a21

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

packages/login/src/vaadin-login-form-mixin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ export const LoginFormMixin = (superClass) =>
189189
/** @protected */
190190
_handleInputKeydown(e) {
191191
if (e.key === 'Enter') {
192+
// Prevent default so that the browser does not apply the Enter activation
193+
// behavior to an element that receives focus while this event is handled.
194+
e.preventDefault();
195+
192196
const { currentTarget: inputActive } = e;
193197
const nextInput = inputActive.id === 'vaadinLoginUsername' ? this._passwordField : this._userNameField;
194198
// eslint-disable-next-line no-restricted-syntax

packages/login/test/login-form.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from '@vaadin/chai-plugins';
2+
import { sendKeys } from '@vaadin/test-runner-commands';
23
import { enter, fixtureSync, nextRender, nextUpdate, tap } from '@vaadin/testing-helpers';
34
import sinon from 'sinon';
45
import '../src/vaadin-login-form.js';
@@ -251,6 +252,33 @@ describe('no autofocus', () => {
251252
});
252253
});
253254

255+
describe('Enter key default action', () => {
256+
let login, button, vaadinLoginPassword;
257+
258+
beforeEach(async () => {
259+
const wrapper = fixtureSync(`
260+
<div>
261+
<vaadin-login-form no-autofocus></vaadin-login-form>
262+
<button></button>
263+
</div>
264+
`);
265+
[login, button] = wrapper.children;
266+
await nextRender();
267+
vaadinLoginPassword = fillUsernameAndPassword(login).vaadinLoginPassword;
268+
});
269+
270+
it('should not activate an element focused while handling Enter keydown', async () => {
271+
const clickSpy = sinon.spy();
272+
button.addEventListener('click', clickSpy);
273+
login.addEventListener('login', () => button.focus());
274+
275+
vaadinLoginPassword.focus();
276+
await sendKeys({ press: 'Enter' });
277+
278+
expect(clickSpy).to.be.not.called;
279+
});
280+
});
281+
254282
describe('error message', () => {
255283
let login, formWrapper;
256284

0 commit comments

Comments
 (0)