Skip to content

Commit 3e50db7

Browse files
Merge pull request #60 from cgarciagarcia/bugfix/improve-filter-action
fix: remove empty filter value
2 parents 527242c + dc09675 commit 3e50db7

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/actions/filter.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ export const filterAction = <Al>(
2323
state: GlobalState<Al>,
2424
override: boolean | FilterValue = false,
2525
): GlobalState<Al> => {
26+
value = Array.isArray(value)
27+
? value.filter((val) => `${val}`.length !== 0)
28+
: value;
29+
30+
const isEmpty = Array.isArray(value)
31+
? value.length === 0
32+
: `${value}`.length === 0;
33+
34+
if (isEmpty) {
35+
return removeFilterAction([attribute], state);
36+
}
37+
2638
const conflicts = state.pruneConflictingFilters[attribute];
2739
const allFilters = state.filters.filter(
2840
(filter) =>

tests/Units/filterAction.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,42 @@ describe("Filter Action test: ", () => {
353353
},
354354
]);
355355
});
356+
357+
it("should remove filter in case filter value is empty string", () => {
358+
const state: GlobalState = {
359+
...initialState,
360+
filters: [
361+
{
362+
attribute: "name",
363+
value: ["Carlos"],
364+
},
365+
],
366+
} as const;
367+
368+
const result = filterAction("name", ["", "", ""], state);
369+
370+
expect(result.filters).toEqual([]);
371+
});
372+
373+
it("should remove only empty filters", () => {
374+
const state: GlobalState = {
375+
...initialState,
376+
filters: [
377+
{
378+
attribute: "name",
379+
value: ["Carlos"],
380+
},
381+
],
382+
} as const;
383+
384+
const result = filterAction("name", ["", "eduardo"], state);
385+
386+
expect(result.filters).toEqual([
387+
{
388+
attribute: "name",
389+
value: ["Carlos", "eduardo"],
390+
operator: undefined,
391+
},
392+
]);
393+
});
356394
});

0 commit comments

Comments
 (0)