Skip to content

Commit 32a3371

Browse files
authored
fix: issue with grid when grid table body is clicked and enter key is hit (#2146) (#2187)
1 parent e608cca commit 32a3371

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

src/vaadin-grid-keyboard-navigation-mixin.html

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,6 @@
361361
dstCell.focus();
362362
}
363363

364-
/** @private */
365-
_parseEventPath(path) {
366-
const tableIndex = path.indexOf(this.$.table);
367-
return {
368-
rowGroup: path[tableIndex - 1],
369-
row: path[tableIndex - 2],
370-
cell: path[tableIndex - 3]
371-
};
372-
}
373-
374364
/** @private */
375365
_onInteractionKeyDown(e, key) {
376366
const localTarget = e.composedPath()[0];
@@ -390,9 +380,9 @@
390380
break;
391381
}
392382

393-
const {cell} = this._parseEventPath(e.composedPath());
383+
const {cell} = this._getGridEventLocation(e);
394384

395-
if (this.interacting !== wantInteracting) {
385+
if (this.interacting !== wantInteracting && cell !== null) {
396386
if (wantInteracting) {
397387
const focusTarget = cell._content.querySelector('[focus-target]') ||
398388
cell._content.firstElementChild;
@@ -533,11 +523,10 @@
533523

534524
/** @private */
535525
_onCellFocusIn(e) {
536-
const location = this._getCellFocusEventLocation(e);
526+
const {section, cell} = this._getGridEventLocation(e);
537527
this._detectInteracting(e);
538528

539-
if (location) {
540-
const {section, cell} = location;
529+
if (section && cell) {
541530
this._activeRowGroup = section;
542531
if (this.$.header === section) {
543532
this._headerFocusable = cell;
@@ -574,8 +563,8 @@
574563

575564
/** @private */
576565
_detectFocusedItemIndex(e) {
577-
const {rowGroup, row} = this._parseEventPath(e.composedPath());
578-
if (rowGroup === this.$.items) {
566+
const {section, row} = this._getGridEventLocation(e);
567+
if (section === this.$.items) {
579568
this._focusedItemIndex = row.index;
580569
}
581570
}
@@ -706,33 +695,32 @@
706695
}
707696

708697
/**
709-
* @typedef {Object} CellFocusEventLocation
710-
* @property {HTMLTableSectionElement} section - The grid section element that contains the focused cell (header, body, or footer)
711-
* @property {HTMLElement} cell - The cell element that received focus or is ancestor of the element that received focus
698+
* @typedef {Object} GridEventLocation
699+
* @property {HTMLTableSectionElement | null} section - The table section element that the event occurred in (header, body, or footer),
700+
* or null if the event did not occur in a section
701+
* @property {HTMLTableRowElement | null} row - The row element that the event occurred in, or null if the event did not occur in a row
702+
* @property {HTMLTableCellElement | null} cell - The cell element that the event occurred in, or null if the event did not occur in a
703+
* cell
712704
* @private
713705
*/
714706
/**
715-
* Takes a focus event and returns a location object describing in which
716-
* section of the grid and in or on which cell the focus event occurred.
717-
* The focus event may either target the cell itself or contents of the cell.
718-
* If the event does not target a cell then null is returned.
719-
* @param {FocusEvent} e
720-
* @returns {CellFocusEventLocation | null}
707+
* Takes an event and returns a location object describing in which part of the grid the event occurred.
708+
* The event may either target table section, a row, a cell or contents of a cell.
709+
* @param {Event} e
710+
* @returns {GridEventLocation}
721711
* @private
722712
*/
723-
_getCellFocusEventLocation(e) {
713+
_getGridEventLocation(e) {
724714
const path = e.composedPath();
725715
const tableIndex = path.indexOf(this.$.table);
726716
// Assuming ascending path to table is: [...,] th|td, tr, thead|tbody, table [,...]
727-
const section = tableIndex >= 2 ? path[tableIndex - 1] : null;
717+
const section = tableIndex >= 1 ? path[tableIndex - 1] : null;
718+
const row = tableIndex >= 2 ? path[tableIndex - 2] : null;
728719
const cell = tableIndex >= 3 ? path[tableIndex - 3] : null;
729720

730-
if (!section || !cell) {
731-
return null;
732-
}
733-
734721
return {
735722
section,
723+
row,
736724
cell
737725
};
738726
}

test/keyboard-navigation.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,13 @@
18561856

18571857
expect(grid.hasAttribute('interacting')).to.be.true;
18581858
});
1859+
1860+
it('should not throw error when hit enter after focus on table body', () => {
1861+
expect(() => {
1862+
grid.$.items.focus();
1863+
enter(grid);
1864+
}).not.to.throw(Error);
1865+
});
18591866
});
18601867

18611868
describe('focus events on cell content', () => {

0 commit comments

Comments
 (0)