Skip to content

Commit

Permalink
fix: display Header's names in 'Filter by ...' text in DataTable fi…
Browse files Browse the repository at this point in the history
…lters (#2757)

Co-authored-by: Kyrylo Hudym-Levkovych <kyr.hudym@kyrs-MacBook-Pro.local>
  • Loading branch information
khudym and Kyrylo Hudym-Levkovych authored Oct 31, 2023
1 parent b284ec5 commit 89fd59e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/DataTable/FilterStatus.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import Button from '../Button';
function FilterStatus({
className, variant, size, clearFiltersText, buttonClassName, showFilteredFields,
}) {
const { state, setAllFilters } = useContext(DataTableContext);
const { state, setAllFilters, headers } = useContext(DataTableContext);
if (!setAllFilters) {
return null;
}

const filterNames = state.filters ? state.filters.map((filter) => filter.id) : [];
const headersMap = headers.reduce((cur, acc) => {
cur[acc.id] = acc.Header;
return cur;
}, {});
const filterNames = state.filters ? state.filters.map((filter) => headersMap[filter.id]) : [];
const filterTexts = <p>Filtered by {filterNames.join(', ')}</p>;

return (
Expand Down
3 changes: 2 additions & 1 deletion src/DataTable/tests/FilterStatus.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import FilterStatus from '../FilterStatus';
import DataTableContext from '../DataTableContext';

const filterNames = ['color', 'breed', 'discipline'];
const headers = [{ id: 'color', Header: 'color' }, { id: 'breed', Header: 'breed' }, { id: 'discipline', Header: 'discipline' }];
const filters = filterNames.map((name) => ({ id: name }));
const instance = { state: { filters }, setAllFilters: () => {} };
const instance = { state: { filters }, setAllFilters: () => {}, headers };
const filterProps = {
buttonClassName: 'buttonClass',
variant: 'variant',
Expand Down
2 changes: 2 additions & 0 deletions src/DataTable/tests/SmartStatus.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import RowStatus from '../RowStatus';
import SelectionStatus from '../selection/SelectionStatus';

const filters = [{ id: 'name' }, { id: 'age' }];
const headers = [{ id: 'name', Header: 'name' }, { id: 'age', Header: 'age' }];
const filterNames = ['name', 'age'];
const itemCount = 101;
const instance = {
Expand All @@ -22,6 +23,7 @@ const instance = {
SelectionStatusComponent: SelectionStatus,
FilterStatusComponent: FilterStatus,
RowStatusComponent: RowStatus,
headers,
};

// eslint-disable-next-line react/prop-types
Expand Down

0 comments on commit 89fd59e

Please sign in to comment.