Skip to content

Commit

Permalink
fix: check args even filter is same when field changed (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs authored Aug 23, 2024
1 parent 25746f6 commit 6e565cd
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/filter/src/hooks/use-filter-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,31 @@ export const useFilterSelect = (rule: SingleFilter) => {
throw new Error("Field has no filter");
}
// If new field has the same filter, it can be retained
const canRetainFilter = newField.filterFnList.some(
const theSameFilterInNewField = newField.filterFnList.find(
(filter) => filter.name === rule.name,
);
const canRetainFilter = !!theSameFilterInNewField;
const needRetainFilter = tryRetainFilter && canRetainFilter;
const fallbackFilter = autoSelectFirstFilter
? newField.filterFnList[0].name
? newField.filterFnList[0]
: undefined;

const newFilterSchema = needRetainFilter ? rule.name : fallbackFilter;
const newFilterSchema = needRetainFilter
? theSameFilterInNewField
: fallbackFilter;

const needRetainArgs =
tryRetainArgs &&
(needRetainFilter || canRetainArgs(newField.filterFnList[0]));
newFilterSchema &&
// For generic filter, the new filter schema is not the same as the current filter schema
// even if the filter name is the same, eg. Equels string -> Equels number
// needRetainFilter &&
canRetainArgs(newFilterSchema);

setRule({
...rule,
path: newField.path,
name: newFilterSchema,
name: newFilterSchema?.name,
// If the filter is retained, keep the arguments
args: needRetainArgs
? rule.args
Expand Down

0 comments on commit 6e565cd

Please sign in to comment.