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;
label: string;
operators: {[key: string]: OperatorConfig};
useWholeRow?: boolean;
};
export type PowerSearchConfig = {

View File

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

View File

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

View File

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