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
*/
import {Input} from 'antd';
import {Button, Input} from 'antd';
import React from 'react';
type PowerSearchStringTermProps = {
onCancel: () => void;
onChange: (value: string) => void;
defaultValue?: string;
};
export const PowerSearchStringTerm: React.FC<PowerSearchStringTermProps> = ({
onCancel,
onChange,
defaultValue,
}) => {
return (
<Input
autoFocus
style={{width: 100}}
placeholder="..."
onBlur={(event) => {
const newValue = event.target.value;
const [editing, setEditing] = React.useState(!defaultValue);
if (!newValue) {
onCancel();
return;
}
if (editing) {
return (
<Input
autoFocus
style={{width: 100}}
placeholder="..."
onBlur={(event) => {
const newValue = event.target.value;
onChange(newValue);
}}
onKeyDown={(event) => {
if (event.key === 'Enter' || event.key === 'Escape') {
event.currentTarget.blur();
}
}}
/>
);
setEditing(false);
if (!newValue) {
onCancel();
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,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
@@ -164,6 +165,21 @@ export const PowerSearchTerm: React.FC<PowerSearchTermProps> = ({
}
} else {
switch (searchTerm.operator.valueType) {
case 'STRING': {
searchValueComponent = (
<PowerSearchStringTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'ENUM': {
searchValueComponent = (
<Button>