Support filtering by exact match

Summary: Project: https://docs.google.com/document/d/1SbFrpvfIShX5npANNa1AkgHliR1M-VMLW5Q9RhREt94/edit

Reviewed By: antonk52

Differential Revision: D46642517

fbshipit-source-id: e23eead18189adb9aaacb7eab3244cfce289203e
This commit is contained in:
Andrey Goncharov
2023-06-12 07:29:55 -07:00
committed by Facebook GitHub Bot
parent b34d3e0a74
commit 16b4d58ac3
2 changed files with 13 additions and 2 deletions

View File

@@ -120,6 +120,7 @@ export type DataTableColumn<T = any> = {
enabled: boolean; enabled: boolean;
predefined?: boolean; predefined?: boolean;
strict?: boolean; strict?: boolean;
exact?: boolean;
}[]; }[];
inversed?: boolean; inversed?: boolean;
sortable?: boolean; sortable?: boolean;

View File

@@ -63,6 +63,11 @@ type AddColumnFilterOptions = {
* By disabling this option you make filter include rows that don't have this value ie undefined * By disabling this option you make filter include rows that don't have this value ie undefined
*/ */
strict?: boolean; strict?: boolean;
/**
* By default, the filter is matched as a substring: rowValue.toLowerCase().includes(filter).
* With this flag the filter is going to be matched by exact equality: rowValue === filter
*/
exact?: boolean;
disableOthers?: boolean; disableOthers?: boolean;
}; };
@@ -559,7 +564,9 @@ function addColumnFilter<T>(
): void { ): void {
options = Object.assign({disableOthers: false, strict: true}, options); options = Object.assign({disableOthers: false, strict: true}, options);
const column = columns.find((c) => c.key === columnId)!; const column = columns.find((c) => c.key === columnId)!;
const filterValue = String(value).toLowerCase(); const filterValue = options.exact
? String(value)
: String(value).toLowerCase();
const existing = column.filters!.find((c) => c.value === filterValue); const existing = column.filters!.find((c) => c.value === filterValue);
if (existing) { if (existing) {
existing.enabled = true; existing.enabled = true;
@@ -569,6 +576,7 @@ function addColumnFilter<T>(
value: filterValue, value: filterValue,
enabled: true, enabled: true,
strict: options.strict, strict: options.strict,
exact: options.exact,
}); });
} }
if (options.disableOthers) { if (options.disableOthers) {
@@ -719,7 +727,9 @@ export function computeDataTableFilter(
} }
const value = getValueAtPath(item, column.key); const value = getValueAtPath(item, column.key);
const isMatching = String(value).toLowerCase().includes(f.value); const isMatching = f.exact
? String(value) === f.value
: String(value).toLowerCase().includes(f.value);
return f.strict ? isMatching : isMatching || value === undefined; return f.strict ? isMatching : isMatching || value === undefined;
}); });