Skip to content

Commit bdaf6b5

Browse files
authored
fix(toolbar): prevent keyboard focus on hidden toolbar elements (#2359)
Fixes #663
1 parent f35b3b9 commit bdaf6b5

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

src/DataTable/ToolbarBatchActions.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
7070
prevActive = active;
7171
}
72+
$: inertProps = showActions ? {} : { inert: true };
7273
7374
let overflowVisible = false;
7475
@@ -99,6 +100,7 @@
99100
<div
100101
class:bx--batch-actions={true}
101102
class:bx--batch-actions--active={showActions}
103+
{...inertProps}
102104
{...$$restProps}
103105
>
104106
<div class:bx--batch-summary={true}>
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1-
<div class:bx--toolbar-content={true}>
1+
<script>
2+
import { getContext } from "svelte";
3+
4+
const ctx = getContext("DataTable") ?? {};
5+
6+
let batchSelectedIds = [];
7+
8+
if (ctx?.batchSelectedIds) {
9+
ctx.batchSelectedIds.subscribe((value) => {
10+
batchSelectedIds = value;
11+
});
12+
}
13+
14+
$: hasBatchSelection = batchSelectedIds.length > 0;
15+
$: inertProps = hasBatchSelection ? { inert: true } : {};
16+
</script>
17+
18+
<div class:bx--toolbar-content={true} {...inertProps}>
219
<slot />
320
</div>

tests/DataTable/DataTableBatchSelectionToolbar.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,74 @@ describe("DataTableBatchSelectionToolbar", () => {
165165
screen.getByRole("checkbox", { name: "Select all rows" }),
166166
).toHaveFocus();
167167
});
168+
169+
it("applies inert to batch actions when no rows are selected", () => {
170+
const { container } = render(DataTableBatchSelectionToolbar, {
171+
props: {
172+
selectedRowIds: [],
173+
},
174+
});
175+
176+
const batchActions = container.querySelector(".bx--batch-actions");
177+
expect(batchActions).toHaveAttribute("inert");
178+
});
179+
180+
it("removes inert from batch actions when rows are selected", () => {
181+
const { container } = render(DataTableBatchSelectionToolbar, {
182+
props: {
183+
selectedRowIds: ["a", "b"],
184+
},
185+
});
186+
187+
const batchActions = container.querySelector(".bx--batch-actions");
188+
expect(batchActions).not.toHaveAttribute("inert");
189+
});
190+
191+
it("applies inert to toolbar content when rows are selected", async () => {
192+
const { container } = render(DataTableBatchSelectionToolbar, {
193+
props: {
194+
selectedRowIds: ["a", "b"],
195+
},
196+
});
197+
198+
const toolbarContent = container.querySelector(".bx--toolbar-content");
199+
expect(toolbarContent).toHaveAttribute("inert");
200+
});
201+
202+
it("removes inert from toolbar content when no rows are selected", () => {
203+
const { container } = render(DataTableBatchSelectionToolbar, {
204+
props: {
205+
selectedRowIds: [],
206+
},
207+
});
208+
209+
const toolbarContent = container.querySelector(".bx--toolbar-content");
210+
expect(toolbarContent).not.toHaveAttribute("inert");
211+
});
212+
213+
it("toggles inert attributes when selection changes", async () => {
214+
const { container, component } = render(DataTableBatchSelectionToolbar, {
215+
props: {
216+
selectedRowIds: [],
217+
},
218+
});
219+
220+
const batchActions = container.querySelector(".bx--batch-actions");
221+
const toolbarContent = container.querySelector(".bx--toolbar-content");
222+
223+
expect(batchActions).toHaveAttribute("inert");
224+
expect(toolbarContent).not.toHaveAttribute("inert");
225+
226+
component.$set({ selectedRowIds: ["a", "b"] });
227+
await tick();
228+
229+
expect(batchActions).not.toHaveAttribute("inert");
230+
expect(toolbarContent).toHaveAttribute("inert");
231+
232+
component.$set({ selectedRowIds: [] });
233+
await tick();
234+
235+
expect(batchActions).toHaveAttribute("inert");
236+
expect(toolbarContent).not.toHaveAttribute("inert");
237+
});
168238
});

0 commit comments

Comments
 (0)