From b3fe4c9650c752452af1255c7d39823d6d93ee25 Mon Sep 17 00:00:00 2001 From: Andrey Goncharov Date: Wed, 20 Sep 2023 04:36:57 -0700 Subject: [PATCH] Make enum term editable Reviewed By: lblasa Differential Revision: D49452483 fbshipit-source-id: e9bef3c5499a91231f6025e403c132b924491def --- .../ui/PowerSearch/PowerSearchEnumTerm.tsx | 62 ++++++++++++------- .../src/ui/PowerSearch/PowerSearchTerm.tsx | 15 ++++- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchEnumTerm.tsx b/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchEnumTerm.tsx index 91e60fcc8..76c621be5 100644 --- a/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchEnumTerm.tsx +++ b/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchEnumTerm.tsx @@ -7,20 +7,24 @@ * @format */ -import {Select} from 'antd'; +import {Button, Select} from 'antd'; import React from 'react'; type PowerSearchEnumTermProps = { onCancel: () => void; onChange: (value: string) => void; enumLabels: {[key: string]: string}; + defaultValue?: string; }; export const PowerSearchEnumTerm: React.FC = ({ onCancel, onChange, enumLabels, + defaultValue, }) => { + const [editing, setEditing] = React.useState(!defaultValue); + const options = React.useMemo(() => { return Object.entries(enumLabels).map(([key, label]) => ({ label, @@ -29,28 +33,42 @@ export const PowerSearchEnumTerm: React.FC = ({ }, [enumLabels]); const selectValueRef = React.useRef(); + if (defaultValue && !selectValueRef.current) { + selectValueRef.current = defaultValue; + } + + if (editing) { + return ( + { - if (!selectValueRef.current) { - onCancel(); - } - }} - onSelect={(value) => { - selectValueRef.current = value; - onChange(value); - }} - onKeyDown={(event) => { - if (event.key === 'Enter' || event.key === 'Escape') { - event.currentTarget.blur(); - } - }} - /> + ); }; diff --git a/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx b/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx index 3df20f1ff..e8a9b10ed 100644 --- a/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx +++ b/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx @@ -120,6 +120,7 @@ export const PowerSearchTerm: React.FC = ({ }); }} enumLabels={searchTerm.operator.enumLabels} + defaultValue={searchTerm.searchValue} /> ); break; @@ -182,9 +183,17 @@ export const PowerSearchTerm: React.FC = ({ } case 'ENUM': { searchValueComponent = ( - + { + onFinalize({ + ...searchTerm, + searchValue: newValue, + }); + }} + enumLabels={searchTerm.operator.enumLabels} + defaultValue={searchTerm.searchValue} + /> ); break; }