Compute power search config from columns
Summary: Doc: https://docs.google.com/document/d/1miofxds9DJgWScj0zFyBbdpRH5Rj0T9FqiCapof5-vU/edit#heading=h.pg8svtdjlx7 Reviewed By: antonk52 Differential Revision: D49349521 fbshipit-source-id: 49f8059bdbfbcc316b79eb633ec54d957f16548d
This commit is contained in:
committed by
Facebook GitHub Bot
parent
8cea5be59d
commit
54a93c03aa
@@ -63,6 +63,5 @@ export type FieldConfig = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type PowerSearchConfig = {
|
export type PowerSearchConfig = {
|
||||||
name: string;
|
|
||||||
fields: {[key: string]: FieldConfig};
|
fields: {[key: string]: FieldConfig};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -104,7 +104,6 @@ const operators = {
|
|||||||
} satisfies {[key: string]: OperatorConfig};
|
} satisfies {[key: string]: OperatorConfig};
|
||||||
|
|
||||||
export const powerSearchExampleConfig: PowerSearchConfig = {
|
export const powerSearchExampleConfig: PowerSearchConfig = {
|
||||||
name: 'FlipperPowerSearchExampleConfig',
|
|
||||||
fields: {
|
fields: {
|
||||||
title: {
|
title: {
|
||||||
key: 'title',
|
key: 'title',
|
||||||
|
|||||||
@@ -61,8 +61,7 @@ import {
|
|||||||
_DataSourceView,
|
_DataSourceView,
|
||||||
} from 'flipper-plugin-core';
|
} from 'flipper-plugin-core';
|
||||||
import {useLatestRef} from '../../utils/useLatestRef';
|
import {useLatestRef} from '../../utils/useLatestRef';
|
||||||
import {PowerSearch, OperatorConfig} from '../PowerSearch';
|
import {PowerSearch, PowerSearchConfig, FieldConfig} from '../PowerSearch';
|
||||||
import {powerSearchExampleConfig} from '../PowerSearch/PowerSearchExampleConfig';
|
|
||||||
import {
|
import {
|
||||||
dataTablePowerSearchOperatorProcessorConfig,
|
dataTablePowerSearchOperatorProcessorConfig,
|
||||||
dataTablePowerSearchOperators,
|
dataTablePowerSearchOperators,
|
||||||
@@ -237,6 +236,30 @@ export function DataTable<T extends object>(
|
|||||||
[columns],
|
[columns],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const powerSearchConfig: PowerSearchConfig = useMemo(() => {
|
||||||
|
const res: PowerSearchConfig = {fields: {}};
|
||||||
|
|
||||||
|
for (const column of columns) {
|
||||||
|
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 ?? {
|
||||||
|
string_contains: dataTablePowerSearchOperators.string_contains(),
|
||||||
|
string_not_contains:
|
||||||
|
dataTablePowerSearchOperators.string_not_contains(),
|
||||||
|
string_matches_exactly:
|
||||||
|
dataTablePowerSearchOperators.string_matches_exactly(),
|
||||||
|
string_not_matches_exactly:
|
||||||
|
dataTablePowerSearchOperators.string_not_matches_exactly(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
res.fields[column.key] = columnFieldConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}, [columns]);
|
||||||
|
|
||||||
const renderingConfig = useMemo<TableRowRenderContext<T>>(() => {
|
const renderingConfig = useMemo<TableRowRenderContext<T>>(() => {
|
||||||
let startIndex = 0;
|
let startIndex = 0;
|
||||||
|
|
||||||
@@ -439,8 +462,6 @@ export function DataTable<T extends object>(
|
|||||||
[
|
[
|
||||||
tableState.searchExpression,
|
tableState.searchExpression,
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
...tableState.columns.map((c) => c.filters),
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
...tableState.columns.map((c) => c.inversed),
|
...tableState.columns.map((c) => c.inversed),
|
||||||
tableState.filterExceptions,
|
tableState.filterExceptions,
|
||||||
],
|
],
|
||||||
@@ -603,7 +624,7 @@ export function DataTable<T extends object>(
|
|||||||
{props.enableSearchbar && (
|
{props.enableSearchbar && (
|
||||||
<Searchbar gap>
|
<Searchbar gap>
|
||||||
<PowerSearch
|
<PowerSearch
|
||||||
config={powerSearchExampleConfig}
|
config={powerSearchConfig}
|
||||||
initialSearchExpression={searchExpression}
|
initialSearchExpression={searchExpression}
|
||||||
onSearchExpressionChange={(newSearchExpression) => {
|
onSearchExpressionChange={(newSearchExpression) => {
|
||||||
tableManager.setSearchExpression(newSearchExpression);
|
tableManager.setSearchExpression(newSearchExpression);
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type {DataTableColumn} from './DataTable';
|
import type {DataTableColumn} from './DataTableWithPowerSearch';
|
||||||
import {Percentage} from '../../utils/widthUtils';
|
import {Percentage} from '../../utils/widthUtils';
|
||||||
import {MutableRefObject, Reducer, RefObject} from 'react';
|
import {MutableRefObject, Reducer, RefObject} from 'react';
|
||||||
import {DataSourceVirtualizer} from '../../data-source/index';
|
import {DataSourceVirtualizer} from '../../data-source/index';
|
||||||
@@ -43,10 +43,7 @@ type PersistedState = {
|
|||||||
/** The currently applicable sorting, if any */
|
/** The currently applicable sorting, if any */
|
||||||
sorting: Sorting | undefined;
|
sorting: Sorting | undefined;
|
||||||
/** The default columns, but normalized */
|
/** The default columns, but normalized */
|
||||||
columns: Pick<
|
columns: Pick<DataTableColumn, 'key' | 'width' | 'visible' | 'inversed'>[];
|
||||||
DataTableColumn,
|
|
||||||
'key' | 'width' | 'filters' | 'visible' | 'inversed'
|
|
||||||
>[];
|
|
||||||
scrollOffset: number;
|
scrollOffset: number;
|
||||||
autoScroll: boolean;
|
autoScroll: boolean;
|
||||||
};
|
};
|
||||||
@@ -143,9 +140,6 @@ export const dataTableManagerReducer = produce<
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'resetFilters': {
|
case 'resetFilters': {
|
||||||
draft.columns.forEach((c) =>
|
|
||||||
c.filters?.forEach((f) => (f.enabled = false)),
|
|
||||||
);
|
|
||||||
draft.searchExpression = undefined;
|
draft.searchExpression = undefined;
|
||||||
draft.filterExceptions = undefined;
|
draft.filterExceptions = undefined;
|
||||||
break;
|
break;
|
||||||
@@ -446,11 +440,6 @@ function computeInitialColumns(
|
|||||||
(columnsWithoutWidth > 1
|
(columnsWithoutWidth > 1
|
||||||
? `${Math.floor(100 / visibleColumnCount)}%`
|
? `${Math.floor(100 / visibleColumnCount)}%`
|
||||||
: undefined),
|
: undefined),
|
||||||
filters:
|
|
||||||
c.filters?.map((f) => ({
|
|
||||||
...f,
|
|
||||||
predefined: true,
|
|
||||||
})) ?? [],
|
|
||||||
visible: c.visible !== false,
|
visible: c.visible !== false,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import {
|
|||||||
_tryGetFlipperLibImplementation,
|
_tryGetFlipperLibImplementation,
|
||||||
_DataSourceView,
|
_DataSourceView,
|
||||||
} from 'flipper-plugin-core';
|
} from 'flipper-plugin-core';
|
||||||
import {DataTableColumn} from './DataTable';
|
import {DataTableColumn} from './DataTableWithPowerSearch';
|
||||||
import {toFirstUpper} from '../../utils/toFirstUpper';
|
import {toFirstUpper} from '../../utils/toFirstUpper';
|
||||||
import {renderColumnValue} from './TableRow';
|
import {renderColumnValue} from './TableRow';
|
||||||
import {textContent} from '../../utils/textContent';
|
import {textContent} from '../../utils/textContent';
|
||||||
|
|||||||
Reference in New Issue
Block a user