Skip to content

Commit

Permalink
add filter option to column
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Komarov committed Dec 20, 2023
1 parent 0fcb465 commit fd899c5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
10 changes: 3 additions & 7 deletions src/Demos/FilterRowCustomLogicDemo/FilterRowCustomLogicDemo.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';

import { DataType, Table } from '../../lib';

import FilterRowNumber from '../../lib/Components/FilterRowNumber/FilterRowNumber';
import { FilteringMode } from '../../lib/enums';
import React from 'react';

const dataArray: any[] = [
{ id: 1, name: 'Mike Wazowski', score: 80, prevScores: [60, 65, 70], passed: true, nextTry: new Date(2021, 10, 9) },
Expand Down Expand Up @@ -42,6 +42,7 @@ const FilterRowCustomLogicDemo: React.FC = () => {
filterRowValue: 60,
key: 'prevScores',
style: {width: 120},
filter: (value: number[], filterRowValue: number) => value.includes(Number(filterRowValue)),
title: 'Previous Scores',
}
]}
Expand All @@ -51,11 +52,6 @@ const FilterRowCustomLogicDemo: React.FC = () => {
return value.join();
}
}}
filter= {({ column }) => {
if (column.key === 'prevScores') {
return (value: number[], filterRowValue: number) => value.includes(Number(filterRowValue));
}
}}
filteringMode={FilteringMode.FilterRow}
rowKeyField={'id'}
childComponents={{
Expand Down
1 change: 1 addition & 0 deletions src/lib/Models/Column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class Column {
public filterRowValue?: any;
public headerFilterValues?: any[];
public headerFilterPopupPosition?: PopupPosition;
public filter?: ((value: any, filterValue: any, rowData?: any) => boolean) | void;
public isHeaderFilterPopupShown?: boolean;
public isEditable?: boolean;
public isFilterable?: boolean;
Expand Down
11 changes: 11 additions & 0 deletions src/lib/Utils/FilterUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ describe('FilterUtils', () => {
}];
expect(() => filterData(data, columns)).toThrowError('\'unknownOperator\' has not found in predefinedFilterOperators array, available operators: =, >, <, >=, <=, contains');
});

it('custom column filter', () => {
const columns = [{
dataType: DataType.Number,
filterRowValue: 45,
key: 'score',
filter: (value: any, filterValue: any) => value === filterValue,
}];
const result = filterData(data, columns);
expect(result).toMatchSnapshot();
});
});

[{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Utils/FilterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const filterData = (data: any[], columns: Column[], filter?: FilterFunc):
) {
return initialData;
}
const customFilter = filter?.({ column });
const customFilter = column.filter || filter?.({ column });
const compare = customFilter || getCompare(column);

return initialData.filter((d: any) => {
Expand Down
12 changes: 12 additions & 0 deletions src/lib/Utils/__snapshots__/FilterUtils.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ Array [
]
`;

exports[`FilterUtils filterData custom column filter 1`] = `
Array [
Object {
"date": 2021-11-20T13:00:00.000Z,
"id": 3,
"name": "Tom Williams",
"passed": false,
"score": 45,
},
]
`;

exports[`FilterUtils filterData custom filter 1`] = `
Array [
Object {
Expand Down

0 comments on commit fd899c5

Please sign in to comment.