Client: Added filtering capabilities to filaments
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { CrudSort } from "@refinedev/core";
|
||||
import { SortOrder } from "antd/es/table/interface";
|
||||
import { CrudFilter, CrudSort } from "@refinedev/core";
|
||||
import { ColumnFilterItem, SortOrder } from "antd/es/table/interface";
|
||||
|
||||
interface IObj {
|
||||
[key: string]: unknown;
|
||||
@@ -74,3 +74,77 @@ export function getSortOrderForField(sorters: CrudSort[], field: string): SortOr
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a filtering function that can be used to filter an array of objects based on the provided filters.
|
||||
* @param filters An array of `CrudFilter` objects that define the filtering criteria.
|
||||
* @returns A function that returns a boolean value based on whether the provided object matches the filtering criteria.
|
||||
*/
|
||||
export function genericFilterer(filters: CrudFilter[]) {
|
||||
return (record: IObj) => {
|
||||
for (const filter of filters) {
|
||||
if (!("field" in filter)) {
|
||||
console.error("Filter must be of type LogicalFilter");
|
||||
return false;
|
||||
}
|
||||
if (!Array.isArray(filter.value)) {
|
||||
console.error("Filter value must be an array of strings.");
|
||||
return false;
|
||||
}
|
||||
let value = record[filter.field];
|
||||
if (value === undefined || value === null) {
|
||||
value = "";
|
||||
}
|
||||
if (typeof value !== "string") {
|
||||
console.error("Only string fields can be filtered, field is of type " + typeof value);
|
||||
return false;
|
||||
}
|
||||
switch (filter.operator) {
|
||||
case "in":
|
||||
if (!filter.value.includes(value)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
console.error(`Not implemented operator: ${filter.operator}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates an array of `ColumnFilterItem` objects based on the unique values of a given field in an array of objects.
|
||||
* @param dataSource An array of objects to filter.
|
||||
* @param field The name of the field to filter on.
|
||||
* @returns An array of `ColumnFilterItem` objects representing the unique values of the specified field in the input array.
|
||||
*/
|
||||
export function filterPopulator(dataSource: IObj[], field: string): ColumnFilterItem[] {
|
||||
const filters: ColumnFilterItem[] = [];
|
||||
dataSource.forEach((element) => {
|
||||
const value = element[field];
|
||||
if (typeof value === "string" && value !== "") {
|
||||
// Make sure it's not already in the filters
|
||||
if (filters.find((f) => f.value === value)) {
|
||||
return;
|
||||
}
|
||||
filters.push({
|
||||
text: value,
|
||||
value: value,
|
||||
});
|
||||
}
|
||||
});
|
||||
// Sort the filters
|
||||
filters.sort((a, b) => {
|
||||
if (typeof a.text !== "string" || typeof b.text !== "string") {
|
||||
return 0;
|
||||
}
|
||||
return a.text.localeCompare(b.text);
|
||||
});
|
||||
filters.push({
|
||||
text: "<empty>",
|
||||
value: "",
|
||||
});
|
||||
return filters;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user