Skip to content

Commit

Permalink
Allow power search to be controlled externally
Browse files Browse the repository at this point in the history
Reviewed By: LukeDefeo

Differential Revision: D50732883

fbshipit-source-id: a19932b5a3f319fd1dc45572fef251369283cc61
  • Loading branch information
aigoncharov authored and facebook-github-bot committed Oct 27, 2023
1 parent f0023ea commit 2c1e814
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions desktop/flipper-plugin/src/ui/PowerSearch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export {PowerSearchConfig, OperatorConfig, FieldConfig, SearchExpressionTerm};

type PowerSearchProps = {
config: PowerSearchConfig;
// Overrides current state of the component with every update.
// It is the way to continuously force update the state of the power search externally.
// Takes prefernce over `initialSearchExpression`.
searchExpression?: SearchExpressionTerm[];
// Component stays uncontrolled and maintains its own state.
// It is respected only on initialization and any future updates are ignored.
initialSearchExpression?: SearchExpressionTerm[];
onSearchExpressionChange: (searchExpression: SearchExpressionTerm[]) => void;
onConfirmUnknownOption?: (
Expand All @@ -46,13 +52,22 @@ const OPTION_KEY_DELIMITER = '::';

export const PowerSearch: React.FC<PowerSearchProps> = ({
config,
searchExpression: searchExpressionExternal,
initialSearchExpression,
onSearchExpressionChange,
onConfirmUnknownOption,
}) => {
const [searchExpression, setSearchExpression] = React.useState<
IncompleteSearchExpressionTerm[]
>(initialSearchExpression ?? []);
>(() => {
if (searchExpressionExternal) {
return searchExpressionExternal;
}
if (initialSearchExpression) {
return initialSearchExpression;
}
return [];
});

const onSearchExpressionChangeLatestRef = useLatestRef(
onSearchExpressionChange,
Expand All @@ -69,6 +84,12 @@ export const PowerSearch: React.FC<PowerSearchProps> = ({
}
}, [searchExpression, onSearchExpressionChangeLatestRef]);

React.useEffect(() => {
if (searchExpressionExternal) {
setSearchExpression(searchExpressionExternal);
}
}, [searchExpressionExternal]);

const options: PowerSearchTermFinderOptionGroup[] = React.useMemo(() => {
const groupedOptions: PowerSearchTermFinderOptionGroup[] = [];

Expand Down Expand Up @@ -112,7 +133,7 @@ export const PowerSearch: React.FC<PowerSearchProps> = ({
{searchExpression.map((searchTerm, i) => {
return (
<PowerSearchTerm
key={i.toString()}
key={JSON.stringify(searchTerm)}
searchTerm={searchTerm}
onCancel={() => {
setSearchExpression((prevSearchExpression) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ export function DataTable<T extends object>(
<Searchbar gap>
<PowerSearch
config={powerSearchConfig}
initialSearchExpression={searchExpression}
searchExpression={searchExpression}
onSearchExpressionChange={(newSearchExpression) => {
tableManager.setSearchExpression(newSearchExpression);
}}
Expand Down

0 comments on commit 2c1e814

Please sign in to comment.