Skip to content

Commit

Permalink
extract getFocusedCell, getFocusedRow into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen committed Oct 17, 2024
1 parent 024ac8d commit ed9e877
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 124 deletions.
15 changes: 15 additions & 0 deletions packages/grid/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,18 @@ export async function onceResized(grid) {
export const shiftClick = (node) => {
node.dispatchEvent(new MouseEvent('click', { shiftKey: true }));
};

export function getFocusedCellIndex(grid) {
const focusedCell = grid.shadowRoot.activeElement;
if (focusedCell instanceof HTMLTableCellElement) {
return [...focusedCell.parentNode.children].indexOf(focusedCell);
}
return -1;
}

export function getFocusedRowIndex(grid) {
const activeElement = grid.shadowRoot.activeElement;
const focusedRow = activeElement instanceof HTMLTableRowElement ? activeElement : activeElement.parentNode;
const section = focusedRow.parentNode;
return section === grid.$.items ? focusedRow.index : [...section.children].indexOf(focusedRow);
}
45 changes: 15 additions & 30 deletions packages/grid/test/keyboard-navigation-row-focus.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
nextRender,
up as mouseUp,
} from '@vaadin/testing-helpers';
import { flushGrid, getCellContent } from './helpers.js';
import { flushGrid, getCellContent, getFocusedCellIndex, getFocusedRowIndex } from './helpers.js';

let grid, header, footer, body;

Expand Down Expand Up @@ -91,21 +91,6 @@ function shiftTabToFooter() {
grid._footerFocusable.focus();
}

function getFocusedCellIndex() {
const focusedCell = grid.shadowRoot.activeElement;
if (focusedCell instanceof HTMLTableCellElement) {
return Array.from(focusedCell.parentNode.children).indexOf(focusedCell);
}
return -1;
}

function getFocusedRowIndex() {
const activeElement = grid.shadowRoot.activeElement;
const focusedRow = activeElement instanceof HTMLTableRowElement ? activeElement : activeElement.parentNode;
const section = focusedRow.parentNode;
return section === grid.$.items ? focusedRow.index : [...section.children].indexOf(focusedRow);
}

function getTabbableElements(root) {
return [...root.querySelectorAll('[tabindex]:not([tabindex="-1"])')].filter((el) => el.offsetParent !== null);
}
Expand Down Expand Up @@ -228,20 +213,20 @@ describe('keyboard navigation - row focus', () => {
it('should remain in row focus mode on backwards', () => {
backwards();

expect(getFocusedCellIndex()).to.equal(-1);
expect(getFocusedCellIndex(grid)).to.equal(-1);
});

it('should enter cell focus mode on forwards', () => {
forwards();

expect(getFocusedCellIndex()).to.equal(0);
expect(getFocusedCellIndex(grid)).to.equal(0);
});

it('should return to row focus mode on backwards', () => {
forwards();
backwards();

expect(getFocusedCellIndex()).to.equal(-1);
expect(getFocusedCellIndex(grid)).to.equal(-1);
});

it('should expand an expandable row on forwards', () => {
Expand All @@ -258,7 +243,7 @@ describe('keyboard navigation - row focus', () => {
// Ensure we're still in row focus mode
backwards();
forwards();
expect(getFocusedCellIndex()).to.equal(-1);
expect(getFocusedCellIndex(grid)).to.equal(-1);
});

it('should enter cell focus mode on an expanded row on forwards', () => {
Expand All @@ -267,7 +252,7 @@ describe('keyboard navigation - row focus', () => {
backwards();
forwards();
forwards();
expect(getFocusedCellIndex()).to.equal(0);
expect(getFocusedCellIndex(grid)).to.equal(0);
});

it('should enter cell focus mode on a leaf row on forwards', () => {
Expand All @@ -276,7 +261,7 @@ describe('keyboard navigation - row focus', () => {
backwards();
down();
forwards();
expect(getFocusedCellIndex()).to.equal(0);
expect(getFocusedCellIndex(grid)).to.equal(0);
});

it('should collapse an expanded row on backwards', () => {
Expand All @@ -294,7 +279,7 @@ describe('keyboard navigation - row focus', () => {
backwards();
openRowDetails(0);
down();
expect(getFocusedRowIndex()).to.equal(1);
expect(getFocusedRowIndex(grid)).to.equal(1);
});

it('should return to row focus mode on backwards from details cell', () => {
Expand All @@ -310,16 +295,16 @@ describe('keyboard navigation - row focus', () => {
down();
// Go to row navigation mode
backwards();
expect(getFocusedCellIndex()).to.equal(-1);
expect(getFocusedCellIndex(grid)).to.equal(-1);
});

it('should navigate rows after a cell gets click focused', () => {
focusItem(0);
clickItem(0);
down();

expect(getFocusedRowIndex()).to.equal(1);
expect(getFocusedCellIndex()).to.equal(-1);
expect(getFocusedRowIndex(grid)).to.equal(1);
expect(getFocusedCellIndex(grid)).to.equal(-1);
});

it('should navigate cells after a cell gets click focused', () => {
Expand All @@ -328,8 +313,8 @@ describe('keyboard navigation - row focus', () => {
clickItem(0);
down();

expect(getFocusedRowIndex()).to.equal(1);
expect(getFocusedCellIndex()).to.equal(0);
expect(getFocusedRowIndex(grid)).to.equal(1);
expect(getFocusedCellIndex(grid)).to.equal(0);
});

it('should enable navigation mode on down', () => {
Expand All @@ -346,15 +331,15 @@ describe('keyboard navigation - row focus', () => {

home();

expect(getFocusedRowIndex()).to.equal(0);
expect(getFocusedRowIndex(grid)).to.equal(0);
});

it('should focus last row with end', () => {
focusItem(0);

end();

expect(getFocusedRowIndex()).to.equal(4);
expect(getFocusedRowIndex(grid)).to.equal(4);
});
});
});
Expand Down
Loading

0 comments on commit ed9e877

Please sign in to comment.