Support entering freeform search values

Summary: Project doc: https://docs.google.com/document/d/1miofxds9DJgWScj0zFyBbdpRH5Rj0T9FqiCapof5-vU

Reviewed By: lblasa

Differential Revision: D48556764

fbshipit-source-id: aa2d77ce111e33ebe5d936c30d23ed78ca0e2c2e
This commit is contained in:
Andrey Goncharov
2023-08-30 07:26:35 -07:00
committed by Facebook GitHub Bot
parent c091067902
commit 7061de44fe

View File

@@ -8,7 +8,7 @@
*/ */
import * as React from 'react'; import * as React from 'react';
import {AutoComplete, Button, Space} from 'antd'; import {AutoComplete, Button, Input, Space} from 'antd';
import { import {
PowerSearchConfig, PowerSearchConfig,
FieldConfig, FieldConfig,
@@ -65,18 +65,63 @@ export const PowerSearch: React.FC<PowerSearchProps> = ({config}) => {
return groupedOptions; return groupedOptions;
}, [config.fields]); }, [config.fields]);
const lastSearchTermHasSearchValue =
searchExpression.length > 0
? searchExpression[searchExpression.length - 1].searchValue !== undefined
: false;
return ( return (
<div style={{display: 'flex', flexDirection: 'row'}}> <div style={{display: 'flex', flexDirection: 'row'}}>
<Space size={[0, 8]}> <Space size={[0, 8]}>
{searchExpression.map((searchTerm, i) => ( {searchExpression.map((searchTerm, i) => {
<Space key={i.toString()}> const isLastTerm = i === searchExpression.length - 1;
return (
<Space.Compact block size="small" key={i.toString()}>
<Button>{searchTerm.field.label}</Button> <Button>{searchTerm.field.label}</Button>
<Button>{searchTerm.operator.label}</Button> <Button>{searchTerm.operator.label}</Button>
<Button>{searchTerm.searchValue}</Button> {lastSearchTermHasSearchValue || !isLastTerm ? (
<Button>{searchTerm.searchValue ?? '...'}</Button>
) : (
// TODO: Fix width
<Input
autoFocus
style={{width: 100}}
placeholder="..."
onBlur={(event) => {
const newValue = event.target.value;
if (!newValue) {
setSearchExpression((prevSearchExpression) => {
return prevSearchExpression.slice(0, -1);
});
return;
}
setSearchExpression((prevSearchExpression) => {
return [
...prevSearchExpression.slice(0, -1),
{
...prevSearchExpression[
prevSearchExpression.length - 1
],
searchValue: newValue,
},
];
});
}}
onKeyUp={(event) => {
if (event.key === 'Enter') {
event.currentTarget.blur();
}
}}
/>
)}
</Space.Compact>
);
})}
</Space> </Space>
))} <AutoComplete<string, AutocompleteOption>
</Space>
<AutoComplete
style={{flex: '1'}} style={{flex: '1'}}
options={options} options={options}
bordered={false} bordered={false}