Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TableContainer): conditionally render header components #18738

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions packages/react/src/components/DataTable/TableContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright IBM Corp. 2016, 2023
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -64,16 +64,22 @@ const TableContainer = ({
return (
<TableContext.Provider value={value}>
<div {...rest} className={tableContainerClasses}>
{title && (
{(title || description) && (
<div className={`${prefix}--data-table-header`}>
<h4 className={`${prefix}--data-table-header__title`} id={titleId}>
{title}
</h4>
<p
className={`${prefix}--data-table-header__description`}
id={descriptionId}>
{description}
</p>
{title && (
<h4
className={`${prefix}--data-table-header__title`}
id={titleId}>
{title}
</h4>
)}
{description && (
<p
className={`${prefix}--data-table-header__description`}
id={descriptionId}>
{description}
</p>
)}
</div>
)}
{children}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Copyright IBM Corp. 2016, 2023
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import React from 'react';
import { TableContainer } from '../';

Expand All @@ -31,4 +31,80 @@ describe('TableContainer', () => {
const { container } = render(<TableContainer data-testid="test" />);
expect(container.firstChild).toHaveAttribute('data-testid', 'test');
});

describe('Header', () => {
it('should render a header with only a `title`', () => {
const title = 'Random title';

const { container } = render(
<TableContainer title={title}>
<div>Child content</div>
</TableContainer>
);

const headerEl = container.querySelector('[class*="data-table-header"]');
const titleEl = headerEl.querySelector('h4');
const descriptionEl = headerEl.querySelector('p');

expect(headerEl).toBeInTheDocument();
expect(headerEl.childElementCount).toBe(1);
expect(titleEl).toHaveTextContent(title);
expect(descriptionEl).toBeNull();
});

it('should render a header with only a `description`', () => {
const description = 'Random description';

const { container } = render(
<TableContainer description={description}>
<div>Child content</div>
</TableContainer>
);

const headerEl = container.querySelector('[class*="data-table-header"]');
const titleEl = headerEl.querySelector('h4');
const descriptionEl = headerEl.querySelector('p');

expect(headerEl).toBeInTheDocument();
expect(headerEl.childElementCount).toBe(1);
expect(descriptionEl).toHaveTextContent(description);
expect(titleEl).toBeNull();
});

it('should render a header with both a `title` and a `description`', () => {
const title = 'Random title';
const description = 'Random description';

const { container } = render(
<TableContainer title={title} description={description}>
<div>Child content</div>
</TableContainer>
);

const headerEl = container.querySelector('[class*="data-table-header"]');
const titleEl = headerEl.querySelector('h4');
const descriptionEl = headerEl.querySelector('p');

expect(headerEl).toBeInTheDocument();
expect(headerEl.childElementCount).toBe(2);
expect(titleEl).toHaveTextContent(title);
expect(headerEl.firstChild).toHaveTextContent(title);
expect(descriptionEl).toHaveTextContent(description);
expect(headerEl.lastChild).toHaveTextContent(description);
});

it('should not render a header when neither a `title` nor a `description` is provided', () => {
const { container } = render(
<TableContainer>
<div data-testid="child-content">Child content</div>
</TableContainer>
);

const headerEl = container.querySelector('[class*="data-table-header"]');
const childContent = screen.getByTestId('child-content');

expect(childContent).toHaveTextContent('Child content');
expect(headerEl).not.toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ exports[`DataTable behaves as expected selection -- radio buttons should not hav
>
DataTable with selection
</h4>
<p
class="cds--data-table-header__description"
id="tc-:r71:-description"
/>
</div>
<div
class="cds--data-table-content"
Expand Down Expand Up @@ -192,10 +188,6 @@ exports[`DataTable behaves as expected selection -- radio buttons should render
>
DataTable with selection
</h4>
<p
class="cds--data-table-header__description"
id="tc-:r6o:-description"
/>
</div>
<div
class="cds--data-table-content"
Expand Down Expand Up @@ -370,10 +362,6 @@ exports[`DataTable behaves as expected selection should render and match snapsho
>
DataTable with selection
</h4>
<p
class="cds--data-table-header__description"
id="tc-:r2e:-description"
/>
</div>
<section
aria-label="data table toolbar"
Expand Down Expand Up @@ -819,10 +807,6 @@ exports[`DataTable renders as expected - Component API should render and match s
>
DataTable with toolbar
</h4>
<p
class="cds--data-table-header__description"
id="tc-:rd:-description"
/>
</div>
<section
aria-label="data table toolbar"
Expand Down
Loading