Make string term editable

Summary:
At the end of the stack we are going to unify two branches of the switch-case in PowerSearchTerm.tsx.
Each search term is going to handle the finalized and "editing" mode internally

Reviewed By: lblasa

Differential Revision: D49451975

fbshipit-source-id: 587ba332d1fbfbaefb368e37965543575e64062f
This commit is contained in:
Andrey Goncharov
2023-09-20 04:36:57 -07:00
committed by Facebook GitHub Bot
parent 0540d240c0
commit 09cb48a3e3
2 changed files with 48 additions and 21 deletions

View File

@@ -7,38 +7,49 @@
* @format * @format
*/ */
import {Input} from 'antd'; import {Button, Input} from 'antd';
import React from 'react'; import React from 'react';
type PowerSearchStringTermProps = { type PowerSearchStringTermProps = {
onCancel: () => void; onCancel: () => void;
onChange: (value: string) => void; onChange: (value: string) => void;
defaultValue?: string;
}; };
export const PowerSearchStringTerm: React.FC<PowerSearchStringTermProps> = ({ export const PowerSearchStringTerm: React.FC<PowerSearchStringTermProps> = ({
onCancel, onCancel,
onChange, onChange,
defaultValue,
}) => { }) => {
return ( const [editing, setEditing] = React.useState(!defaultValue);
<Input
autoFocus
style={{width: 100}}
placeholder="..."
onBlur={(event) => {
const newValue = event.target.value;
if (!newValue) { if (editing) {
onCancel(); return (
return; <Input
} autoFocus
style={{width: 100}}
placeholder="..."
onBlur={(event) => {
const newValue = event.target.value;
onChange(newValue); setEditing(false);
}}
onKeyDown={(event) => { if (!newValue) {
if (event.key === 'Enter' || event.key === 'Escape') { onCancel();
event.currentTarget.blur(); return;
} }
}}
/> onChange(newValue);
); }}
onKeyDown={(event) => {
if (event.key === 'Enter' || event.key === 'Escape') {
event.currentTarget.blur();
}
}}
defaultValue={defaultValue}
/>
);
}
return <Button onClick={() => setEditing(true)}>{defaultValue}</Button>;
}; };

View File

@@ -57,6 +57,7 @@ export const PowerSearchTerm: React.FC<PowerSearchTermProps> = ({
searchValue: newValue, searchValue: newValue,
}); });
}} }}
defaultValue={searchTerm.searchValue}
/> />
); );
break; break;
@@ -164,6 +165,21 @@ export const PowerSearchTerm: React.FC<PowerSearchTermProps> = ({
} }
} else { } else {
switch (searchTerm.operator.valueType) { switch (searchTerm.operator.valueType) {
case 'STRING': {
searchValueComponent = (
<PowerSearchStringTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'ENUM': { case 'ENUM': {
searchValueComponent = ( searchValueComponent = (
<Button> <Button>