Skip to content

Commit

Permalink
Merge pull request #284 from komarovalexander/dev
Browse files Browse the repository at this point in the history
Fix custom data filter for date column
  • Loading branch information
komarovalexander authored Feb 13, 2023
2 parents 53d4bad + a604357 commit c0ce850
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 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": "7.10.0",
"version": "7.10.1",
"license": "MIT",
"repository": "github:komarovalexander/ka-table",
"homepage": "https://komarovalexander.github.io/ka-table/#/overview",
Expand Down
27 changes: 24 additions & 3 deletions src/lib/Utils/FilterUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { DataType, FilterOperatorName } from '../enums';
import { Column } from '../Models/Column';
import { SearchFunc } from '../types';
import {
filterData, getDefaultOperatorForType, getRowEditableCells, predefinedFilterOperators, searchData,
filterData,
getDefaultOperatorForType,
getRowEditableCells,
predefinedFilterOperators,
searchData,
} from './FilterUtils';

import { Column } from '../Models/Column';
import { SearchFunc } from '../types';

describe('FilterUtils', () => {
it('getRowEditableCells should return required cells from table EditableCells', () => {
const rowEditableCells = getRowEditableCells(10, [{
Expand Down Expand Up @@ -104,6 +109,22 @@ describe('FilterUtils', () => {
expect(result).toMatchSnapshot();
});

it('custom filter: column with data type = "date" should not change filterRowValue', () => {
const columns = [{
dataType: DataType.Date,
filterRowValue: ['date 1'],
key: 'date',
}];
const result = filterData(data, columns, ({ column }) => {
if (column.key === 'date'){
return (value, filterValue) => {
return filterValue[0] === 'date 1' && value === data[0].date
};
}
});
expect(result).toMatchSnapshot();
});

it('should throw an error in case of unknown filterOperator', () => {
const columns = [{
filterRowOperator: 'unknownOperator',
Expand Down
17 changes: 10 additions & 7 deletions src/lib/Utils/FilterUtils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ITableProps } from '../';
import defaultOptions from '../defaultOptions';
import { DataType, FilterOperatorName } from '../enums';
import { FilterFunc, FormatFunc, SearchFunc } from '../types';

import { Column } from '../Models/Column';
import { EditableCell } from '../Models/EditableCell';
import { FilterOperator } from '../Models/FilterOperator';
import { FilterFunc, FormatFunc, SearchFunc } from '../types';
import { isEmpty } from './CommonUtils';
import { getValueByColumn } from './DataUtils';
import { ITableProps } from '../';
import { convertToColumnTypes } from './TypeUtils';
import defaultOptions from '../defaultOptions';
import { getValueByColumn } from './DataUtils';
import { isEmpty } from './CommonUtils';

export const getRowEditableCells = (rowKeyValue: any, editableCells: EditableCell[]): EditableCell[] => {
return editableCells.filter((c) => c.rowKeyValue === rowKeyValue);
Expand Down Expand Up @@ -75,11 +76,13 @@ export const filterData = (data: any[], columns: Column[], filter?: FilterFunc):
) {
return initialData;
}
const compare = filter?.({ column }) || getCompare(column);
const customFilter = filter?.({ column });
const compare = customFilter || getCompare(column);

return initialData.filter((d: any) => {
let fieldValue = getValueByColumn(d, column);
let conditionValue = column.filterRowValue;
if (column.dataType === DataType.Date) {
if (column.dataType === DataType.Date && !customFilter) {
fieldValue = fieldValue == null ? fieldValue : new Date(fieldValue).setHours(0, 0, 0, 0);
conditionValue = conditionValue == null ? conditionValue : new Date(conditionValue).setHours(0, 0, 0, 0);
}
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 @@ -111,6 +111,18 @@ Array [
]
`;

exports[`FilterUtils filterData custom filter: column with data type = "date" should not change filterRowValue 1`] = `
Array [
Object {
"date": 2021-12-20T09:00:00.000Z,
"id": 1,
"name": "Mike Wazowski",
"passed": true,
"score": 80,
},
]
`;

exports[`FilterUtils filterData one item 1`] = `
Array [
Object {
Expand Down

0 comments on commit c0ce850

Please sign in to comment.