Skip to content

Commit cade2ce

Browse files
committed
refactor(aria): refine grid state reset logic with dual-axis clamping and add tests
1 parent 753a2ed commit cade2ce

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/aria/private/behaviors/grid/grid.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ describe('Grid', () => {
395395
expect(grid.focusBehavior.activeCoords()).toEqual({row: 1, col: 1});
396396
});
397397

398-
it('should focus the first cell if active cell and coords are no longer valid', () => {
398+
it('should focus the row above when the last row is deleted', () => {
399399
const cellsSignal = signal(createTestGrid(createGridA));
400400
const grid = setupGrid(cellsSignal);
401401
grid.gotoCell(cellsSignal()[2][2]);
@@ -416,8 +416,8 @@ describe('Grid', () => {
416416
expect(grid.focusBehavior.stateStale()).toBe(true);
417417
const result = grid.resetState();
418418
expect(result).toBe(true);
419-
expect(grid.focusBehavior.activeCell()).toBe(newCells[0][0]);
420-
expect(grid.focusBehavior.activeCoords()).toEqual({row: 0, col: 0});
419+
expect(grid.focusBehavior.activeCell()).toBe(newCells[1][1]);
420+
expect(grid.focusBehavior.activeCoords()).toEqual({row: 1, col: 1});
421421
});
422422
});
423423
});

src/aria/private/behaviors/grid/grid.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,18 @@ export class Grid<T extends GridCell> {
342342
return true;
343343
}
344344

345+
// Try clamping the column as well.
346+
const colCount = this.data.getColCount(targetRow);
347+
if (colCount !== undefined) {
348+
const targetCol = Math.min(activeCoords.col, colCount - 1);
349+
if (
350+
targetCol >= 0 &&
351+
this.focusBehavior.focusCoordinates({row: targetRow, col: targetCol})
352+
) {
353+
return true;
354+
}
355+
}
356+
345357
// If that fails, try to find ANY cell in that row.
346358
const firstInRow = this.navigationBehavior.peekFirst(targetRow);
347359
if (firstInRow !== undefined && this.focusBehavior.focusCoordinates(firstInRow)) {

0 commit comments

Comments
 (0)