Add string_contains operator

Summary: Doc: https://docs.google.com/document/d/1miofxds9DJgWScj0zFyBbdpRH5Rj0T9FqiCapof5-vU/edit#heading=h.pg8svtdjlx7

Reviewed By: lblasa

Differential Revision: D49226934

fbshipit-source-id: 12d43334e87b0fb502173143d5a19a48cd2fc9ee
This commit is contained in:
Andrey Goncharov
2023-09-14 04:48:12 -07:00
committed by Facebook GitHub Bot
parent 589937bc8f
commit ceac388eed
2 changed files with 23 additions and 9 deletions

View File

@@ -11,16 +11,27 @@ import {OperatorConfig} from '../PowerSearch';
export type PowerSearchOperatorProcessor = (
powerSearchOperatorConfig: OperatorConfig,
searchValue: any,
value: any,
) => boolean;
export type PowerSearchOperatorProcessorConfig = {
[key: string]: PowerSearchOperatorProcessor;
};
export const dataTablePowerSearchOperators = {} satisfies {
export const dataTablePowerSearchOperators = {
string_contains: () => ({
label: 'contains',
key: 'string_contains',
valueType: 'STRING',
}),
} satisfies {
[key: string]: (...args: any[]) => OperatorConfig;
};
export const dataTablePowerSearchOperatorProcessorConfig =
{} satisfies PowerSearchOperatorProcessorConfig;
export type PowerSearchOperatorProcessorConfig = {
[K in keyof typeof dataTablePowerSearchOperators]: PowerSearchOperatorProcessor;
};
export const dataTablePowerSearchOperatorProcessorConfig = {
string_contains: (operator, searchValue, value) =>
(value as string)
.toLowerCase()
.includes((searchValue as string).toLowerCase()),
} satisfies PowerSearchOperatorProcessorConfig;

View File

@@ -494,7 +494,10 @@ export function computeDataTableFilter(
return true;
}
const processor = powerSearchProcessors[searchTerm.operator.key];
const processor =
powerSearchProcessors[
searchTerm.operator.key as keyof typeof powerSearchProcessors
];
if (!processor) {
console.warn(
'computeDataTableFilter -> processor at searchTerm.operator.key is not recognized',
@@ -504,7 +507,7 @@ export function computeDataTableFilter(
return true;
}
return processor(searchTerm.operator, value);
return processor(searchTerm.operator, searchTerm.searchValue, value);
});
};
}