-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (78 loc) · 2.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
export function customFilterAndSearch(
/** value of the column filter input / table search input, should come from the table's `customFilterAndSearch` */
value = "",
/** data of a table row, should come from table's `customFilterAndSearch` */
rowData = {},
/** name of the table column field */
columnField = "",
/** all the filters saved from table in a react state -> `useState([])` */
filters = [],
/** value of the table search input saved in a react state -> `useState("")` */
searchTerm = "",
/** array of strings; all the (necessary) fields that should be included for searching based on `searchTerm`, example: ["name", "surname", "birth"] */
lookupRowData = [],
/** the condition for Filter to return `true` as in accepted row for display */
CONDITION,
/** wheater to enable console log for debug */
debug = false
) {
// CLEAN UP:
if (!value)
throw "material-table-custom-filter-and-search says: \nNo value provided as a first function argument!";
if (rowData === null && typeof rowData !== "object")
throw "material-table-custom-filter-and-search says: \nrowData needs to be an object!";
if (lookupRowData.length === 0) {
lookupRowData = [...Object.keys(rowData)];
searchTerm = searchTerm?.toLowerCase();
}
debug & console.table(arguments);
// EXISTANCE OF THE TERM IN ROWDATA:
const valueExistsInRowData = (field) => {
const fieldValue = rowData[field];
// console.log("field", field, rowData[field]);
if (!fieldValue) return false;
if (typeof fieldValue === "string") {
const isTrue = fieldValue.toLowerCase().includes(value.toLowerCase());
return Boolean(isTrue);
}
if (typeof fieldValue === "number") {
const isTrue = fieldValue.toString().includes(value.toLowerCase());
return Boolean(isTrue);
}
if (Array.isArray(fieldValue)) {
fieldValue.forEach(valueExistsInRowData);
}
if (typeof fieldValue === "object") {
Object.keys(fieldValue).forEach(valueExistsInRowData);
}
};
// RULE FOR USING COLUMN INPUT:
const columFilterValue = filters?.find(
(filter) => filter.column.field === columnField
)?.value;
function termExistsInRow() {
return lookupRowData.some(valueExistsInRowData);
}
// ACTION TO EXECUTE
if (columFilterValue && searchTerm) {
// console.log(
// "%cFILTER SEARCHING...",
// "color: white; font-weight: 700; background: green",
// value
// );
const conditionForFilteringisTrue = termExistsInRow() && Boolean(CONDITION);
return conditionForFilteringisTrue ? true : false;
} else if (columFilterValue) {
const conditionForFilteringisTrue = Boolean(CONDITION);
return conditionForFilteringisTrue ? true : false;
} else if (searchTerm) {
// console.log(
// "%cJUST SEARCHING...",
// "color: white; font-weight: 700; background-color: red",
// value
// );
return termExistsInRow() ? true : false;
} else {
return true;
}
}