Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add custom matchers to client side filters #1922

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/src/app/components/FilterToolbar/FilterToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export interface IBasicFilterCategory<
* Defaults to using the UI state's value if omitted.
*/
getServerFilterValue?: (filterValue: FilterValue) => string[] | undefined;
/** For client side filtering, provide custom algorithm for testing if the value of `TItem` matches the filter value. */
matcher?: (filter: string, item: TItem) => boolean;
}

export interface IMultiselectFilterCategory<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
FilterCategory,
FilterValue,
getFilterLogicOperator,
} from "@app/components/FilterToolbar";
import { objectKeys } from "@app/utils/utils";
import { IFilterState } from "./useFilterState";

/**
Expand Down Expand Up @@ -44,26 +44,33 @@ export const getLocalFilterDerivedState = <
filterState: { filterValues },
}: ILocalFilterDerivedStateArgs<TItem, TFilterCategoryKey>) => {
const filteredItems = items.filter((item) =>
objectKeys(filterValues).every((categoryKey) => {
const values = filterValues[categoryKey];
Object.entries<FilterValue>(filterValues).every(([filterKey, values]) => {
if (!values || values.length === 0) return true;
const filterCategory = filterCategories.find(
(category) => category.categoryKey === categoryKey
);
let itemValue = (item as any)[categoryKey];
if (filterCategory?.getItemValue) {
itemValue = filterCategory.getItemValue(item);
}
const logicOperator = getFilterLogicOperator(filterCategory);
return values[logicOperator === "AND" ? "every" : "some"](
(filterValue) => {
if (!itemValue) return false;
const lowerCaseItemValue = String(itemValue).toLowerCase();
const lowerCaseFilterValue = String(filterValue).toLowerCase();
return lowerCaseItemValue.indexOf(lowerCaseFilterValue) !== -1;
}
(category) => category.categoryKey === filterKey
);
const defaultMatcher = (filterValue: string, item: TItem) =>
legacyMatcher(
filterValue,
filterCategory?.getItemValue?.(item) ?? (item as any)[filterKey]
);
const matcher = filterCategory?.matcher ?? defaultMatcher;
const logicOperator =
getFilterLogicOperator(filterCategory) === "AND" ? "every" : "some";
return values[logicOperator]((filterValue) => matcher(filterValue, item));
})
);

return { filteredItems };
};

/**
*
* @returns false for any falsy value (regardless of the filter value), true if (coerced to string) lowercased value contains lowercased filter value.
*/
const legacyMatcher = (filterValue: string, value: any) => {
if (!value) return false;
const lowerCaseItemValue = String(value).toLowerCase();
const lowerCaseFilterValue = String(filterValue).toLowerCase();
return lowerCaseItemValue.indexOf(lowerCaseFilterValue) !== -1;
};
Loading