Make enum term editable
Reviewed By: lblasa Differential Revision: D49452483 fbshipit-source-id: e9bef3c5499a91231f6025e403c132b924491def
This commit is contained in:
committed by
Facebook GitHub Bot
parent
09cb48a3e3
commit
b3fe4c9650
@@ -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,7 +33,11 @@ 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 (
|
return (
|
||||||
<Select
|
<Select
|
||||||
autoFocus
|
autoFocus
|
||||||
@@ -41,6 +49,7 @@ export const PowerSearchEnumTerm: React.FC<PowerSearchEnumTermProps> = ({
|
|||||||
if (!selectValueRef.current) {
|
if (!selectValueRef.current) {
|
||||||
onCancel();
|
onCancel();
|
||||||
}
|
}
|
||||||
|
setEditing(false);
|
||||||
}}
|
}}
|
||||||
onSelect={(value) => {
|
onSelect={(value) => {
|
||||||
selectValueRef.current = value;
|
selectValueRef.current = value;
|
||||||
@@ -51,6 +60,15 @@ export const PowerSearchEnumTerm: React.FC<PowerSearchEnumTermProps> = ({
|
|||||||
event.currentTarget.blur();
|
event.currentTarget.blur();
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
defaultValue={defaultValue}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Button onClick={() => setEditing(true)}>
|
||||||
|
{/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */}
|
||||||
|
{enumLabels[defaultValue!]}
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user