Skip to content

Commit

Permalink
Fix search by multiple columns with the same values
Browse files Browse the repository at this point in the history
  • Loading branch information
komarovalexander committed Nov 25, 2020
1 parent 2996984 commit c2b4bca
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ka-table",
"version": "6.0.0",
"version": "6.0.1",
"license": "MIT",
"repository": "github:komarovalexander/ka-table",
"homepage": "https://komarovalexander.github.io/ka-table/#/overview",
Expand Down
6 changes: 6 additions & 0 deletions src/lib/Utils/FilterUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ describe('FilterUtils', () => {
{ id: 4, name: 'Kurt Cobain', score: 75, passed: true },
{ id: 5, name: 'Marshall Bruce', score: 77, passed: true },
{ id: 6, name: 'Sunny Fox', score: 33, passed: false },
{ id: 7, name: 'Falsey False', score: 33, passed: false },
];
it('by string', () => {
const result = searchData(columns, data, 'Mike', search);
Expand All @@ -219,6 +220,11 @@ describe('FilterUtils', () => {
expect(result).toMatchSnapshot();
});

it('should add item only once', () => {
const result = searchData(columns, data, 'false', search);
expect(result).toMatchSnapshot();
});

it('should not find value by search handler', () => {
const result = searchData(columns, data, 'tru', search);
expect(result).toMatchSnapshot();
Expand Down
9 changes: 5 additions & 4 deletions src/lib/Utils/FilterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export const getRowEditableCells = (rowKeyValue: any, editableCells: EditableCel
};

export const searchData = (columns: Column[], data: any[], searchText: string, search?: SearchFunc): any[] => {
return columns.reduce((initialData: any[], c: Column) => {
const searched = columns.reduce((initialData: any[], c: Column) => {
const filterFunction = (item: any) => {
if (initialData.indexOf(item) >= 0) {
return false;
}
const searchContent = search && search({ column: c, searchText, rowData: item });
if (searchContent != null) {
return searchContent;
}
if (initialData.indexOf(item) >= 0) {
return false;
}
const columnValue = getValueByColumn(item, c);
if (columnValue == null) {
return false;
Expand All @@ -32,6 +32,7 @@ export const searchData = (columns: Column[], data: any[], searchText: string, s
};
return initialData.concat(data.filter(filterFunction));
}, []);
return data.filter(d => searched.indexOf(d) >= 0);
};

export const filterAndSearchData = (props: ITableProps) => {
Expand Down
29 changes: 29 additions & 0 deletions src/lib/Utils/__snapshots__/FilterUtils.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,35 @@ Array [
]
`;

exports[`FilterUtils searchData should add item only once 1`] = `
Array [
Object {
"id": 2,
"name": "Billi Bob",
"passed": false,
"score": 55,
},
Object {
"id": 3,
"name": "Tom Williams",
"passed": false,
"score": 45,
},
Object {
"id": 6,
"name": "Sunny Fox",
"passed": false,
"score": 33,
},
Object {
"id": 7,
"name": "Falsey False",
"passed": false,
"score": 33,
},
]
`;

exports[`FilterUtils searchData should find value by search handler 1`] = `
Array [
Object {
Expand Down

0 comments on commit c2b4bca

Please sign in to comment.