Skip to content

Commit 2936688

Browse files
authored
fix(mui): add support for unique id to transformCrudFiltersToFilterModel (#6756)
1 parent 5dd509f commit 2936688

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

.changeset/rare-teachers-drive.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@refinedev/mui": patch
3+
---
4+
5+
Add support for unique id to `transformCrudFiltersToFilterModel` when filters have the same field and operator.
6+
7+
[Resolves #6710](https://github.com/refinedev/refine/issues/6710)

packages/mui/src/definitions/dataGrid/index.spec.ts

+46
Original file line numberDiff line numberDiff line change
@@ -437,4 +437,50 @@ describe("transformCrudFiltersToFilterModel", () => {
437437
logicOperator: "and",
438438
});
439439
});
440+
441+
it("Should have unique IDs for filters with the same field and operator", () => {
442+
const crudFilters: CrudFilters = [
443+
{
444+
operator: "or",
445+
value: [
446+
{
447+
field: "status",
448+
operator: "eq",
449+
value: "draft",
450+
},
451+
{
452+
field: "status",
453+
operator: "eq",
454+
value: "published",
455+
},
456+
],
457+
},
458+
];
459+
460+
const columnsLookup = {
461+
status: "string",
462+
};
463+
464+
const filterModel: GridFilterModel = {
465+
items: [
466+
{
467+
field: "status",
468+
operator: "equals",
469+
value: "draft",
470+
id: "statuseq",
471+
},
472+
{
473+
field: "status",
474+
operator: "equals",
475+
value: "published",
476+
id: "statuseq2",
477+
},
478+
],
479+
logicOperator: GridLogicOperator.Or,
480+
};
481+
482+
expect(
483+
transformCrudFiltersToFilterModel(crudFilters, columnsLookup),
484+
).toEqual(filterModel);
485+
});
440486
});

packages/mui/src/definitions/dataGrid/index.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export const transformCrudFiltersToFilterModel = (
184184
columnsType?: Record<string, string>,
185185
): GridFilterModel | undefined => {
186186
const gridFilterItems: GridFilterItem[] = [];
187+
const fieldOperatorCount: Record<string, number> = {};
187188

188189
const isExistOrFilter = crudFilters.some(
189190
(filter) => filter.operator === "or",
@@ -197,23 +198,31 @@ export const transformCrudFiltersToFilterModel = (
197198

198199
orLogicalFilters.map(({ field, value, operator }) => {
199200
const columnType = columnsType[field];
201+
const id = field + operator;
202+
203+
fieldOperatorCount[id] = (fieldOperatorCount[id] || 0) + 1;
204+
const uniqueId = id + String(fieldOperatorCount[id]);
200205

201206
gridFilterItems.push({
202207
field: field,
203208
operator: transformCrudOperatorToMuiOperator(operator, columnType),
204209
value: value === "" ? undefined : value,
205-
id: field + operator,
210+
id: fieldOperatorCount[id] > 1 ? uniqueId : id,
206211
});
207212
});
208213
} else {
209214
(crudFilters as LogicalFilter[]).map(({ field, value, operator }) => {
210215
const columnType = columnsType[field];
216+
const id = field + operator;
217+
218+
fieldOperatorCount[id] = (fieldOperatorCount[id] || 0) + 1;
219+
const uniqueId = id + String(fieldOperatorCount[id]);
211220

212221
gridFilterItems.push({
213222
field: field,
214223
operator: transformCrudOperatorToMuiOperator(operator, columnType),
215224
value: value === "" ? undefined : value,
216-
id: field + operator,
225+
id: fieldOperatorCount[id] > 1 ? uniqueId : id,
217226
});
218227
});
219228
}

0 commit comments

Comments
 (0)