Skip to content

Commit f574ad0

Browse files
authored
fix(DataTable): Use the right heading level in TableContainer. (#18871)
* fix(TableContainer): use the right heading level Assuming the application is already correctly using <Section>, this will give the table the right heading level (i.e. one level higher than the parent heading). We could also add aria-labelledby={titleId} to the <Section> to give it an implicit role of "region" rather than "presentation". Not sure if a single table is big enough to be considered a region or not. Fixes #18835. * fix(TableContainer): fix snapshot * fix(TableContainer): second try to fix snapshot
1 parent d74af68 commit f574ad0

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

packages/react/src/components/DataTable/TableContainer.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { ReactAttr } from '../../types/common';
1212
import { usePrefix } from '../../internal/usePrefix';
1313
import { useId } from '../../internal/useId';
1414
import { TableContext } from './TableContext';
15+
import { Heading, Section } from '../Heading';
1516

1617
export interface TableContainerProps
1718
extends Omit<ReactAttr<HTMLDivElement>, 'title'> {
@@ -63,15 +64,15 @@ const TableContainer = ({
6364

6465
return (
6566
<TableContext.Provider value={value}>
66-
<div {...rest} className={tableContainerClasses}>
67+
<Section {...rest} className={tableContainerClasses}>
6768
{(title || description) && (
6869
<div className={`${prefix}--data-table-header`}>
6970
{title && (
70-
<h4
71+
<Heading
7172
className={`${prefix}--data-table-header__title`}
7273
id={titleId}>
7374
{title}
74-
</h4>
75+
</Heading>
7576
)}
7677
{description && (
7778
<p
@@ -83,7 +84,7 @@ const TableContainer = ({
8384
</div>
8485
)}
8586
{children}
86-
</div>
87+
</Section>
8788
</TableContext.Provider>
8889
);
8990
};

packages/react/src/components/DataTable/__tests__/TableContainer-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('TableContainer', () => {
4343
);
4444

4545
const headerEl = container.querySelector('[class*="data-table-header"]');
46-
const titleEl = headerEl.querySelector('h4');
46+
const titleEl = headerEl.querySelector('h2');
4747
const descriptionEl = headerEl.querySelector('p');
4848

4949
expect(headerEl).toBeInTheDocument();
@@ -82,7 +82,7 @@ describe('TableContainer', () => {
8282
);
8383

8484
const headerEl = container.querySelector('[class*="data-table-header"]');
85-
const titleEl = headerEl.querySelector('h4');
85+
const titleEl = headerEl.querySelector('h2');
8686
const descriptionEl = headerEl.querySelector('p');
8787

8888
expect(headerEl).toBeInTheDocument();

packages/react/src/components/DataTable/__tests__/__snapshots__/DataTable-test.js.snap

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
exports[`DataTable behaves as expected selection -- radio buttons should not have select-all checkbox 1`] = `
44
<div>
5-
<div
5+
<section
66
class="cds--data-table-container"
77
>
88
<div
99
class="cds--data-table-header"
1010
>
11-
<h4
11+
<h2
1212
class="cds--data-table-header__title"
1313
id="tc-:r7g:-title"
1414
>
1515
DataTable with selection
16-
</h4>
16+
</h2>
1717
</div>
1818
<div
1919
class="cds--data-table-content"
@@ -170,24 +170,24 @@ exports[`DataTable behaves as expected selection -- radio buttons should not hav
170170
</tbody>
171171
</table>
172172
</div>
173-
</div>
173+
</section>
174174
</div>
175175
`;
176176

177177
exports[`DataTable behaves as expected selection -- radio buttons should render 1`] = `
178178
<div>
179-
<div
179+
<section
180180
class="cds--data-table-container"
181181
>
182182
<div
183183
class="cds--data-table-header"
184184
>
185-
<h4
185+
<h2
186186
class="cds--data-table-header__title"
187187
id="tc-:r77:-title"
188188
>
189189
DataTable with selection
190-
</h4>
190+
</h2>
191191
</div>
192192
<div
193193
class="cds--data-table-content"
@@ -344,24 +344,24 @@ exports[`DataTable behaves as expected selection -- radio buttons should render
344344
</tbody>
345345
</table>
346346
</div>
347-
</div>
347+
</section>
348348
</div>
349349
`;
350350

351351
exports[`DataTable behaves as expected selection should render and match snapshot 1`] = `
352352
<div>
353-
<div
353+
<section
354354
class="cds--data-table-container"
355355
>
356356
<div
357357
class="cds--data-table-header"
358358
>
359-
<h4
359+
<h2
360360
class="cds--data-table-header__title"
361361
id="tc-:r2k:-title"
362362
>
363363
DataTable with selection
364-
</h4>
364+
</h2>
365365
</div>
366366
<section
367367
aria-label="data table toolbar"
@@ -788,25 +788,25 @@ exports[`DataTable behaves as expected selection should render and match snapsho
788788
</tbody>
789789
</table>
790790
</div>
791-
</div>
791+
</section>
792792
</div>
793793
`;
794794

795795
exports[`DataTable renders as expected - Component API should render and match snapshot 1`] = `
796796
<div>
797-
<div
797+
<section
798798
class="cds--data-table-container"
799799
data-testid="test-id"
800800
>
801801
<div
802802
class="cds--data-table-header"
803803
>
804-
<h4
804+
<h2
805805
class="cds--data-table-header__title"
806806
id="tc-:re:-title"
807807
>
808808
DataTable with toolbar
809-
</h4>
809+
</h2>
810810
</div>
811811
<section
812812
aria-label="data table toolbar"
@@ -1120,6 +1120,6 @@ exports[`DataTable renders as expected - Component API should render and match s
11201120
</tbody>
11211121
</table>
11221122
</div>
1123-
</div>
1123+
</section>
11241124
</div>
11251125
`;

0 commit comments

Comments
 (0)