Allow using whole row for power search

Reviewed By: lblasa

Differential Revision: D49827828

fbshipit-source-id: 93219146ba59074be305eb9a963550223a5abd18
This commit is contained in:
Andrey Goncharov
2023-10-02 08:27:37 -07:00
committed by Facebook GitHub Bot
parent c27f9c7916
commit 010de365fa
4 changed files with 30 additions and 14 deletions

View File

@@ -84,6 +84,7 @@ export type FieldConfig = {
key: string; key: string;
label: string; label: string;
operators: {[key: string]: OperatorConfig}; operators: {[key: string]: OperatorConfig};
useWholeRow?: boolean;
}; };
export type PowerSearchConfig = { export type PowerSearchConfig = {

View File

@@ -20,7 +20,7 @@ import {PowerSearchStringSetTerm} from './PowerSearchStringSetTerm';
import {PowerSearchStringTerm} from './PowerSearchStringTerm'; import {PowerSearchStringTerm} from './PowerSearchStringTerm';
export type IncompleteSearchExpressionTerm = { export type IncompleteSearchExpressionTerm = {
field: {key: string; label: string}; field: {key: string; label: string; useWholeRow?: boolean};
operator: OperatorConfig; operator: OperatorConfig;
searchValue?: any; searchValue?: any;
}; };

View File

@@ -132,7 +132,10 @@ export type DataTableColumn<T = any> = {
visible?: boolean; visible?: boolean;
inversed?: boolean; inversed?: boolean;
sortable?: boolean; sortable?: boolean;
powerSearchConfig?: OperatorConfig[] | false; powerSearchConfig?:
| OperatorConfig[]
| false
| {operators: OperatorConfig[]; useWholeRow?: boolean};
}; };
export interface TableRowRenderContext<T = any> { export interface TableRowRenderContext<T = any> {
@@ -248,22 +251,32 @@ export function DataTable<T extends object>(
if (column.powerSearchConfig === false) { if (column.powerSearchConfig === false) {
continue; continue;
} }
let useWholeRow = false;
let columnPowerSearchOperators: OperatorConfig[];
// If no power search config provided we treat every input as a string
if (!column.powerSearchConfig) {
columnPowerSearchOperators = [
dataTablePowerSearchOperators.string_contains(true),
dataTablePowerSearchOperators.string_not_contains(true),
dataTablePowerSearchOperators.string_matches_exactly(true),
dataTablePowerSearchOperators.string_not_matches_exactly(true),
];
} else if (Array.isArray(column.powerSearchConfig)) {
columnPowerSearchOperators = column.powerSearchConfig;
} else {
columnPowerSearchOperators = column.powerSearchConfig.operators;
useWholeRow = true;
}
const columnFieldConfig: FieldConfig = { const columnFieldConfig: FieldConfig = {
label: column.title || column.key, label: column.title || column.key,
key: column.key, key: column.key,
// If no power search config provided we treat every input as a string operators: columnPowerSearchOperators.reduce((res, operatorConfig) => {
operators: column.powerSearchConfig?.reduce((res, operatorConfig) => {
res[operatorConfig.key] = operatorConfig; res[operatorConfig.key] = operatorConfig;
return res; return res;
}, {} as Record<string, OperatorConfig>) ?? { }, {} as Record<string, OperatorConfig>),
string_contains: dataTablePowerSearchOperators.string_contains(true), useWholeRow,
string_not_contains:
dataTablePowerSearchOperators.string_not_contains(true),
string_matches_exactly:
dataTablePowerSearchOperators.string_matches_exactly(true),
string_not_matches_exactly:
dataTablePowerSearchOperators.string_not_matches_exactly(true),
},
}; };
res.fields[column.key] = columnFieldConfig; res.fields[column.key] = columnFieldConfig;
} }

View File

@@ -480,7 +480,9 @@ export function computeDataTableFilter(
return true; return true;
} }
return searchExpression.every((searchTerm) => { return searchExpression.every((searchTerm) => {
const value = getValueAtPath(item, searchTerm.field.key); const value = searchTerm.field.useWholeRow
? item
: getValueAtPath(item, searchTerm.field.key);
if (!value) { if (!value) {
console.warn( console.warn(
'computeDataTableFilter -> value at searchTerm.field.key is not recognized', 'computeDataTableFilter -> value at searchTerm.field.key is not recognized',