Skip to content

Commit cdab2a5

Browse files
authored
fix: do not scroll to the first column on focus (#6406)
1 parent 4c8a3f9 commit cdab2a5

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎packages/grid/src/vaadin-grid-keyboard-navigation-mixin.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,26 @@ export const KeyboardNavigationMixin = (superClass) =>
682682
}
683683
}
684684

685-
return tabOrder[index];
685+
let focusStepTarget = tabOrder[index];
686+
687+
// If the target focusable is tied to a column that is not visible,
688+
// find the first visible column and update the target in order to
689+
// prevent scrolling to the start of the row.
690+
if (focusStepTarget && focusStepTarget._column && !this.__isColumnInViewport(focusStepTarget._column)) {
691+
const firstVisibleColumn = this._getColumnsInOrder().find((column) => this.__isColumnInViewport(column));
692+
if (firstVisibleColumn) {
693+
if (focusStepTarget === this._headerFocusable) {
694+
focusStepTarget = firstVisibleColumn._headerCell;
695+
} else if (focusStepTarget === this._itemsFocusable) {
696+
const rowIndex = focusStepTarget._column._cells.indexOf(focusStepTarget);
697+
focusStepTarget = firstVisibleColumn._cells[rowIndex];
698+
} else if (focusStepTarget === this._footerFocusable) {
699+
focusStepTarget = firstVisibleColumn._footerCell;
700+
}
701+
}
702+
}
703+
704+
return focusStepTarget;
686705
}
687706

688707
/** @private */

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,34 @@ describe('keyboard navigation', () => {
11181118
left();
11191119
expect(grid.$.table.scrollLeft).to.equal(0);
11201120
});
1121+
1122+
it('should not scroll to the start when tabbed from header to body', () => {
1123+
tabToHeader();
1124+
end();
1125+
tab();
1126+
expect(grid.$.table.scrollLeft).to.be.at.least(100);
1127+
});
1128+
1129+
it('should not scroll to the start when shift-tabbed from footer to body', () => {
1130+
shiftTabToFooter();
1131+
end();
1132+
shiftTab();
1133+
expect(grid.$.table.scrollLeft).to.be.at.least(100);
1134+
});
1135+
1136+
it('should not scroll to the start when tabbed from body to footer', () => {
1137+
tabToBody();
1138+
end();
1139+
tab();
1140+
expect(grid.$.table.scrollLeft).to.be.at.least(100);
1141+
});
1142+
1143+
it('should not scroll to the start when shift-tabbed from body to header', () => {
1144+
tabToBody();
1145+
end();
1146+
shiftTab();
1147+
expect(grid.$.table.scrollLeft).to.be.at.least(100);
1148+
});
11211149
});
11221150

11231151
describe('vertical scrolling', () => {

0 commit comments

Comments
 (0)