-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(react-table): allow conditional filters on
useTable
(#5862)
- Loading branch information
Showing
10 changed files
with
630 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
"@refinedev/react-table": patch | ||
--- | ||
|
||
fix: updated column filter transformation logic to handle conditional filters | ||
|
||
`useTable` hook was ignoring the conditional filters with `"and"` and `"or"` operators, causing custom filtering logic inside the table to not work as expected and omitting the filters from the query. This PR enables working with conditionenal filters and fixes the disappearing filters issue. | ||
|
||
To customize the `key` value of the conditional filter, you can use the `filterKey` property in the column's `meta` property. This property will be used as the key for the filter when setting the filter value to the table from `@refinedev/core`'s filter state and when setting the filter value to the filter state from the table. | ||
|
||
```tsx | ||
// An example of how to use the `filterKey` property in the column's `meta` property | ||
const columns: ColumnDef<IPost> = [ | ||
{ | ||
id: "title", | ||
header: "Title", | ||
accessorKey: "title", | ||
meta: { | ||
// This is optional, if not defined column id will be used as the key | ||
filterKey: "titleFilter", | ||
// If operator is not `'eq'` or `'in'`, make sure to set the `filterOperator` property | ||
filterOperator: "and", | ||
}, | ||
}, | ||
]; | ||
``` | ||
|
||
Resolves [#5856](https://github.com/refinedev/refine/issues/5856) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 221 additions & 0 deletions
221
packages/react-table/src/utils/column-filters-to-crud-filters/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
import { ColumnDef, ColumnFilter } from "@tanstack/react-table"; | ||
import { columnFiltersToCrudFilters } from "."; | ||
|
||
describe("columnFiltersToCrudFilters", () => { | ||
it("should return with crud filters type", () => { | ||
const columns: ColumnDef<any, any>[] = [ | ||
{ | ||
id: "name", | ||
header: "Name", | ||
accessorKey: "name", | ||
}, | ||
{ | ||
header: "Age", | ||
accessorKey: "age", | ||
}, | ||
]; | ||
|
||
const columnFilters: ColumnFilter[] = [ | ||
{ id: "name", value: "John" }, | ||
{ id: "age", value: 30 }, | ||
]; | ||
|
||
const crudFilters = columnFiltersToCrudFilters({ | ||
columns, | ||
columnFilters, | ||
}); | ||
|
||
expect(crudFilters).toEqual([ | ||
{ field: "name", value: "John", operator: "eq" }, | ||
{ field: "age", value: 30, operator: "eq" }, | ||
]); | ||
}); | ||
|
||
it("should set operator to 'in' if value is an array", () => { | ||
const columns: ColumnDef<any, any>[] = [ | ||
{ | ||
id: "name", | ||
header: "Name", | ||
accessorKey: "name", | ||
}, | ||
{ | ||
header: "Age", | ||
accessorKey: "age", | ||
}, | ||
]; | ||
|
||
const columnFilters: ColumnFilter[] = [ | ||
{ id: "name", value: ["John", "Doe"] }, | ||
{ id: "age", value: 30 }, | ||
]; | ||
|
||
const crudFilters = columnFiltersToCrudFilters({ | ||
columns, | ||
columnFilters, | ||
}); | ||
|
||
expect(crudFilters).toEqual([ | ||
{ field: "name", value: ["John", "Doe"], operator: "in" }, | ||
{ field: "age", value: 30, operator: "eq" }, | ||
]); | ||
}); | ||
|
||
it("should respect filterOperator from column meta", () => { | ||
const columns: ColumnDef<any, any>[] = [ | ||
{ | ||
id: "name", | ||
header: "Name", | ||
accessorKey: "name", | ||
meta: { | ||
filterOperator: "contains", | ||
}, | ||
}, | ||
{ | ||
header: "Age", | ||
accessorKey: "age", | ||
}, | ||
]; | ||
|
||
const columnFilters: ColumnFilter[] = [ | ||
{ id: "name", value: "John" }, | ||
{ id: "age", value: 30 }, | ||
]; | ||
|
||
const crudFilters = columnFiltersToCrudFilters({ | ||
columns, | ||
columnFilters, | ||
}); | ||
|
||
expect(crudFilters).toEqual([ | ||
{ field: "name", value: "John", operator: "contains" }, | ||
{ field: "age", value: 30, operator: "eq" }, | ||
]); | ||
}); | ||
|
||
it("should transform and/or filters to conditional filters", () => { | ||
const columns: ColumnDef<any, any>[] = [ | ||
{ | ||
id: "name", | ||
header: "Name", | ||
accessorKey: "name", | ||
meta: { | ||
filterOperator: "or", | ||
}, | ||
}, | ||
{ | ||
id: "age", | ||
header: "Age", | ||
accessorKey: "age", | ||
meta: { | ||
filterOperator: "and", | ||
}, | ||
}, | ||
]; | ||
|
||
const columnFilters: ColumnFilter[] = [ | ||
{ | ||
id: "name", | ||
value: [ | ||
{ field: "name", operator: "eq", value: "John" }, | ||
{ field: "name", operator: "eq", value: "Doe" }, | ||
], | ||
}, | ||
|
||
{ | ||
id: "age", | ||
value: [ | ||
{ field: "age", operator: "gte", value: 18 }, | ||
{ field: "age", operator: "lte", value: 65 }, | ||
], | ||
}, | ||
]; | ||
|
||
const crudFilters = columnFiltersToCrudFilters({ | ||
columns, | ||
columnFilters, | ||
}); | ||
|
||
expect(crudFilters).toEqual([ | ||
{ | ||
key: "name", | ||
operator: "or", | ||
value: [ | ||
{ field: "name", operator: "eq", value: "John" }, | ||
{ field: "name", operator: "eq", value: "Doe" }, | ||
], | ||
}, | ||
{ | ||
key: "age", | ||
operator: "and", | ||
value: [ | ||
{ field: "age", operator: "gte", value: 18 }, | ||
{ field: "age", operator: "lte", value: 65 }, | ||
], | ||
}, | ||
]); | ||
}); | ||
|
||
it("should respect custom filter key for conditional filters", () => { | ||
const columns: ColumnDef<any, any>[] = [ | ||
{ | ||
id: "name", | ||
header: "Name", | ||
accessorKey: "name", | ||
meta: { | ||
filterOperator: "or", | ||
}, | ||
}, | ||
{ | ||
id: "age", | ||
header: "Age", | ||
accessorKey: "age", | ||
meta: { | ||
filterOperator: "and", | ||
filterKey: "customKey", | ||
}, | ||
}, | ||
]; | ||
|
||
const columnFilters: ColumnFilter[] = [ | ||
{ | ||
id: "name", | ||
value: [ | ||
{ field: "name", operator: "eq", value: "John" }, | ||
{ field: "name", operator: "eq", value: "Doe" }, | ||
], | ||
}, | ||
|
||
{ | ||
id: "age", | ||
value: [ | ||
{ field: "age", operator: "gte", value: 18 }, | ||
{ field: "age", operator: "lte", value: 65 }, | ||
], | ||
}, | ||
]; | ||
|
||
const crudFilters = columnFiltersToCrudFilters({ | ||
columns, | ||
columnFilters, | ||
}); | ||
|
||
expect(crudFilters).toEqual([ | ||
{ | ||
key: "name", | ||
operator: "or", | ||
value: [ | ||
{ field: "name", operator: "eq", value: "John" }, | ||
{ field: "name", operator: "eq", value: "Doe" }, | ||
], | ||
}, | ||
{ | ||
key: "customKey", | ||
operator: "and", | ||
value: [ | ||
{ field: "age", operator: "gte", value: 18 }, | ||
{ field: "age", operator: "lte", value: 65 }, | ||
], | ||
}, | ||
]); | ||
}); | ||
}); |
Oops, something went wrong.