Skip to content

Commit 0cd4d12

Browse files
author
shleewhite
committed
fix: restore tabindex correctly when switch between mode
1 parent 05f40b8 commit 0cd4d12

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

packages/components/src/components/hds/advanced-table/helpers.ts

+10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import { focusable, tabbable } from 'tabbable';
88
import type { HdsAdvancedTableTdSignature } from './td';
99
import type { HdsAdvancedTableThSignature } from './th';
1010

11+
export const onFocusTrapDeactivate = (cell: HTMLDivElement) => {
12+
const cellTabbableChildren = tabbable(cell);
13+
14+
for (const child of cellTabbableChildren) {
15+
child.setAttribute('tabindex', '-1')
16+
}
17+
18+
cell.setAttribute('tabindex', '0')
19+
}
20+
1121
export const didInsertGridCell = (
1222
cell:
1323
| HdsAdvancedTableThSignature['Element']

packages/components/src/components/hds/advanced-table/td.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { focusable, type FocusableElement } from 'tabbable';
1111

1212
import type { HdsAdvancedTableHorizontalAlignment } from './types.ts';
1313
import { HdsAdvancedTableHorizontalAlignmentValues } from './types.ts';
14-
import { didInsertGridCell, handleGridCellKeyPress } from './helpers.ts';
14+
import { didInsertGridCell, handleGridCellKeyPress, onFocusTrapDeactivate } from './helpers.ts';
1515

1616
export const ALIGNMENTS: string[] = Object.values(
1717
HdsAdvancedTableHorizontalAlignmentValues
@@ -77,6 +77,7 @@ export default class HdsAdvancedTableTd extends Component<HdsAdvancedTableTdSign
7777

7878
@action onFocusTrapDeactivate(): void {
7979
this._shouldTrapFocus = false;
80+
onFocusTrapDeactivate(this._element)
8081
}
8182

8283
@action enableFocusTrap(): void {

packages/components/src/components/hds/advanced-table/th-sort.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {
2121
HdsAdvancedTableThSortOrderLabels,
2222
} from './types.ts';
2323
import type { HdsAdvancedTableThButtonSortSignature } from './th-button-sort';
24-
import { didInsertGridCell, handleGridCellKeyPress } from './helpers.ts';
24+
import { didInsertGridCell, handleGridCellKeyPress, onFocusTrapDeactivate } from './helpers.ts';
2525

2626
export const ALIGNMENTS: string[] = Object.values(
2727
HdsAdvancedTableHorizontalAlignmentValues
@@ -92,6 +92,7 @@ export default class HdsAdvancedTableThSort extends Component<HdsAdvancedTableTh
9292

9393
@action onFocusTrapDeactivate(): void {
9494
this._shouldTrapFocus = false;
95+
onFocusTrapDeactivate(this._element)
9596
}
9697

9798
@action enableFocusTrap(): void {

packages/components/src/components/hds/advanced-table/th.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
HdsAdvancedTableScope,
1616
} from './types.ts';
1717
import { HdsAdvancedTableHorizontalAlignmentValues } from './types.ts';
18-
import { didInsertGridCell, handleGridCellKeyPress } from './helpers.ts';
18+
import { didInsertGridCell, handleGridCellKeyPress, onFocusTrapDeactivate } from './helpers.ts';
1919

2020
export const ALIGNMENTS: string[] = Object.values(
2121
HdsAdvancedTableHorizontalAlignmentValues
@@ -104,6 +104,8 @@ export default class HdsAdvancedTableTh extends Component<HdsAdvancedTableThSign
104104

105105
@action onFocusTrapDeactivate(): void {
106106
this._shouldTrapFocus = false;
107+
onFocusTrapDeactivate(this._element)
108+
107109
}
108110

109111
@action enableFocusTrap(): void {

showcase/tests/integration/components/hds/advanced-table/helpers-test.js

+9
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,15 @@ module(
331331
// cells should not be focusable anymore
332332
assert.dom('.hds-advanced-table__th[tabindex="0"]').doesNotExist();
333333
assert.dom('.hds-advanced-table__td[tabindex="0"]').doesNotExist();
334+
335+
await triggerKeyEvent(firstCellSortButton, 'keydown', 'Escape');
336+
assert.dom(firstCell).hasAttribute('tabindex', '0');
337+
assert.dom(firstCellSortButton).hasAttribute('tabindex', '-1');
338+
339+
// content within cells should not be focusable anymore
340+
assert
341+
.dom('[data-advanced-table-child-focusable=""][tabindex="0"]')
342+
.doesNotExist();
334343
});
335344

336345
test('it should trap focus inside a cell when not in navigation mode', async function (assert) {

0 commit comments

Comments
 (0)