Skip to content

Commit 1f46479

Browse files
authored
fix: do not consider label and hidden elements focusable (#5263)
1 parent 30566c5 commit 1f46479

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎packages/grid/src/vaadin-grid-active-item-mixin.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,15 @@ export const isFocusable = (target) => {
109109
}
110110
const focusables = Array.from(
111111
target.parentNode.querySelectorAll(
112-
'[tabindex], button, input, select, textarea, object, iframe, label, a[href], area[href]',
112+
'[tabindex], button, input, select, textarea, object, iframe, a[href], area[href]',
113113
),
114114
).filter((element) => {
115115
const part = element.getAttribute('part');
116116
return !(part && part.includes('body-cell'));
117117
});
118118

119119
const isFocusableElement = focusables.includes(target);
120-
return !target.disabled && isFocusableElement;
120+
return (
121+
!target.disabled && isFocusableElement && target.offsetParent && getComputedStyle(target).visibility !== 'hidden'
122+
);
121123
};

‎packages/grid/test/keyboard-navigation.test.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import '../vaadin-grid.js';
2020
import '../vaadin-grid-tree-column.js';
2121
import '../vaadin-grid-column-group.js';
2222
import '../vaadin-grid-selection-column.js';
23+
import { isElementFocused } from '@vaadin/component-base/src/focus-utils.js';
2324
import {
2425
flushGrid,
2526
getCell,
@@ -1606,6 +1607,30 @@ describe('keyboard navigation', () => {
16061607
spy.restore();
16071608
});
16081609

1610+
it('should focus the first actually focusable element when entering interaction mode', () => {
1611+
const content = getCellContent(getRowCell(0, 1));
1612+
const contentElements = fixtureSync(`
1613+
<div>
1614+
<label for="disabled-input">Label</label>
1615+
<input id="disabled-input" disabled style="width: 20px">
1616+
<input style="visibility: hidden; width: 20px;">
1617+
<div hidden>
1618+
<input>
1619+
</div>
1620+
<input id="focusable" style="width: 20px">
1621+
</div>
1622+
`);
1623+
content.textContent = '';
1624+
content.append(...contentElements.childNodes);
1625+
const focusable = content.querySelector('#focusable');
1626+
1627+
right(); // Focus the cell with input.
1628+
1629+
enter();
1630+
1631+
expect(isElementFocused(focusable)).to.be.true;
1632+
});
1633+
16091634
it('should exit interaction mode from focused single-line input with enter', () => {
16101635
const cell = getRowCell(0, 1);
16111636
const input = getCellContent(cell).children[0];

0 commit comments

Comments
 (0)