Make enum term editable

Reviewed By: lblasa

Differential Revision: D49452483

fbshipit-source-id: e9bef3c5499a91231f6025e403c132b924491def
This commit is contained in:
Andrey Goncharov
2023-09-20 04:36:57 -07:00
committed by Facebook GitHub Bot
parent 09cb48a3e3
commit b3fe4c9650
2 changed files with 52 additions and 25 deletions

View File

@@ -7,20 +7,24 @@
* @format * @format
*/ */
import {Select} from 'antd'; import {Button, Select} from 'antd';
import React from 'react'; import React from 'react';
type PowerSearchEnumTermProps = { type PowerSearchEnumTermProps = {
onCancel: () => void; onCancel: () => void;
onChange: (value: string) => void; onChange: (value: string) => void;
enumLabels: {[key: string]: string}; enumLabels: {[key: string]: string};
defaultValue?: string;
}; };
export const PowerSearchEnumTerm: React.FC<PowerSearchEnumTermProps> = ({ export const PowerSearchEnumTerm: React.FC<PowerSearchEnumTermProps> = ({
onCancel, onCancel,
onChange, onChange,
enumLabels, enumLabels,
defaultValue,
}) => { }) => {
const [editing, setEditing] = React.useState(!defaultValue);
const options = React.useMemo(() => { const options = React.useMemo(() => {
return Object.entries(enumLabels).map(([key, label]) => ({ return Object.entries(enumLabels).map(([key, label]) => ({
label, label,
@@ -29,28 +33,42 @@ export const PowerSearchEnumTerm: React.FC<PowerSearchEnumTermProps> = ({
}, [enumLabels]); }, [enumLabels]);
const selectValueRef = React.useRef<string>(); const selectValueRef = React.useRef<string>();
if (defaultValue && !selectValueRef.current) {
selectValueRef.current = defaultValue;
}
if (editing) {
return (
<Select
autoFocus
style={{width: 100}}
placeholder="..."
options={options}
defaultOpen
onBlur={() => {
if (!selectValueRef.current) {
onCancel();
}
setEditing(false);
}}
onSelect={(value) => {
selectValueRef.current = value;
onChange(value);
}}
onKeyDown={(event) => {
if (event.key === 'Enter' || event.key === 'Escape') {
event.currentTarget.blur();
}
}}
defaultValue={defaultValue}
/>
);
}
return ( return (
<Select <Button onClick={() => setEditing(true)}>
autoFocus {/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */}
style={{width: 100}} {enumLabels[defaultValue!]}
placeholder="..." </Button>
options={options}
defaultOpen
onBlur={() => {
if (!selectValueRef.current) {
onCancel();
}
}}
onSelect={(value) => {
selectValueRef.current = value;
onChange(value);
}}
onKeyDown={(event) => {
if (event.key === 'Enter' || event.key === 'Escape') {
event.currentTarget.blur();
}
}}
/>
); );
}; };

View File

@@ -120,6 +120,7 @@ export const PowerSearchTerm: React.FC<PowerSearchTermProps> = ({
}); });
}} }}
enumLabels={searchTerm.operator.enumLabels} enumLabels={searchTerm.operator.enumLabels}
defaultValue={searchTerm.searchValue}
/> />
); );
break; break;
@@ -182,9 +183,17 @@ export const PowerSearchTerm: React.FC<PowerSearchTermProps> = ({
} }
case 'ENUM': { case 'ENUM': {
searchValueComponent = ( searchValueComponent = (
<Button> <PowerSearchEnumTerm
{searchTerm.operator.enumLabels[searchTerm.searchValue]} onCancel={onCancel}
</Button> onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
enumLabels={searchTerm.operator.enumLabels}
defaultValue={searchTerm.searchValue}
/>
); );
break; break;
} }