From 830e9d1cd190bef445bb88f1b3f75ceea51e069f Mon Sep 17 00:00:00 2001 From: Ugur Saglam <106508695+ugur-vaadin@users.noreply.github.com> Date: Mon, 15 Apr 2024 17:01:11 +0300 Subject: [PATCH] fix: do not move focus when focused in on grid via clicking backport (#2243) --- src/vaadin-grid-keyboard-navigation-mixin.html | 16 +++++++++------- test/keyboard-navigation.html | 13 +++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/vaadin-grid-keyboard-navigation-mixin.html b/src/vaadin-grid-keyboard-navigation-mixin.html index 158cd4bb5..4b62589e9 100644 --- a/src/vaadin-grid-keyboard-navigation-mixin.html +++ b/src/vaadin-grid-keyboard-navigation-mixin.html @@ -499,13 +499,15 @@ if (rootTarget === this.$.table || rootTarget === this.$.focusexit) { - // The focus enters the top (bottom) of the grid, meaning that user has - // tabbed (shift-tabbed) into the grid. Move the focus to - // the first (the last) focusable. - this._predictFocusStepTarget( - rootTarget, - rootTarget === this.$.table ? 1 : -1 - ).focus(); + if (!this._isMousedown) { + // The focus enters the top (bottom) of the grid, meaning that user has + // tabbed (shift-tabbed) into the grid. Move the focus to + // the first (the last) focusable. + this._predictFocusStepTarget( + rootTarget, + rootTarget === this.$.table ? 1 : -1 + ).focus(); + } this._setInteracting(false); } else { this._detectInteracting(e); diff --git a/test/keyboard-navigation.html b/test/keyboard-navigation.html index 230665cc1..3b5bb8767 100644 --- a/test/keyboard-navigation.html +++ b/test/keyboard-navigation.html @@ -602,6 +602,19 @@ expect(grid.shadowRoot.activeElement).to.equal(tabbableElements[3]); }); + it('should not enter grid on table click', () => { + const tabbableElements = getTabbableElements(grid.shadowRoot); + + // Click and focusin on table element + mouseDown(tabbableElements[0]); + const event = new CustomEvent('focusin', {bubbles: true, composed: true}); + event.relatedTarget = focusable; + tabbableElements[0].dispatchEvent(event); + + // Expect no focus on header cell + expect(grid.shadowRoot.activeElement).to.be.null; + }); + it('should set native focus to header on header cell click', () => { const tabbableElements = getTabbableElements(grid.shadowRoot); focusFirstHeaderCell();