Skip to content

Commit

Permalink
fix: correctly display filtered items count in Select all button in t…
Browse files Browse the repository at this point in the history
…he DataTable component (#2929)

* fix: fixed the Select all button in the DataTable component

* refactor: refactoring after review
  • Loading branch information
PKulkoRaccoonGang authored Dec 22, 2023
1 parent 9a16ba1 commit 16a006b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/DataTable/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ function DataTable({

const enhancedInstance = {
...instance,
manualFilters,
itemCount,
numBreakoutFilters,
bulkActions,
Expand Down
4 changes: 2 additions & 2 deletions src/DataTable/selection/BaseSelectionStatus.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ function BaseSelectionStatus({
}) {
const {
itemCount, filteredRows, isPaginated, state,
isSelectable, maxSelectedRows,
isSelectable, maxSelectedRows, manualFilters,
} = useContext(DataTableContext);
const hasAppliedFilters = state?.filters?.length > 0;
const isAllRowsSelected = numSelectedRows === itemCount;
const filteredItems = filteredRows?.length || itemCount;
const filteredItems = manualFilters ? itemCount : (filteredRows?.length || itemCount);
const hasMaxSelectedRows = isSelectable && maxSelectedRows;

const intlAllSelectedText = allSelectedText || (
Expand Down
31 changes: 31 additions & 0 deletions src/DataTable/tests/DataTable.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { IntlProvider } from 'react-intl';

import DataTable from '..';
import DataTableContext from '../DataTableContext';
import { TextFilter } from '../..';
import { SELECT_ALL_TEST_ID } from '../selection/data/constants';

const additionalColumns = [
{
Expand Down Expand Up @@ -198,6 +200,35 @@ describe('<DataTable />', () => {

expect(spinner).toBeTruthy();
});
it('displays the total number of items when applying filter and selecting all items', async () => {
const propsWithSelection = {
...props,
isSelectable: true,
isFilterable: true,
manualFilters: true,
defaultColumnValues: { Filter: TextFilter },
isPaginated: true,
initialState: { pageSize: 3, pageIndex: 0 },
pageCount: 3,
fetchData: jest.fn(),
};

render(<DataTableWrapper {...propsWithSelection} />);
const filtersButton = screen.getByRole('button', { name: 'Filters' });

await userEvent.click(filtersButton);

const searchFormControl = screen.getByPlaceholderText('Search coat color');
await userEvent.type(searchFormControl, 'brown tabby');

const selectAllCheckBox = screen.getByTitle('Toggle All Current Page Rows Selected');
await userEvent.click(selectAllCheckBox);

const selectAllButton = screen.getByTestId(SELECT_ALL_TEST_ID);
// A filtered array is returned from the backend,
// and the element counter displays its length.
expect(selectAllButton).toHaveTextContent('Select all 7');
});

describe('[legacy] controlled table selections', () => {
it('passes initial controlledTableSelections to context', async () => {
Expand Down

0 comments on commit 16a006b

Please sign in to comment.