Remove last search term on backspace press when term finder is empty

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

Reviewed By: lblasa

Differential Revision: D48637890

fbshipit-source-id: fb952ffe1ab1a94d2bc465b89be6c62a71be9b9f
This commit is contained in:
Andrey Goncharov
2023-08-30 07:26:35 -07:00
committed by Facebook GitHub Bot
parent 52a4051b15
commit 7b9ddb617f
2 changed files with 13 additions and 1 deletions

View File

@@ -24,12 +24,13 @@ export type PowerSearchTermFinderRef = {
type PowerSearchTermFinderProps = { type PowerSearchTermFinderProps = {
options: PowerSearchTermFinderOptionGroup[]; options: PowerSearchTermFinderOptionGroup[];
onSelect: (selectedOption: PowerSearchTermFinderOption) => void; onSelect: (selectedOption: PowerSearchTermFinderOption) => void;
onBackspacePressWhileEmpty: () => void;
}; };
export const PowerSearchTermFinder = React.forwardRef< export const PowerSearchTermFinder = React.forwardRef<
PowerSearchTermFinderRef, PowerSearchTermFinderRef,
PowerSearchTermFinderProps PowerSearchTermFinderProps
>(({options, onSelect}, ref) => { >(({options, onSelect, onBackspacePressWhileEmpty}, ref) => {
const [searchTermFinderValue, setSearchTermFinderValue] = React.useState< const [searchTermFinderValue, setSearchTermFinderValue] = React.useState<
string | null string | null
>(null); >(null);
@@ -62,6 +63,9 @@ export const PowerSearchTermFinder = React.forwardRef<
if (event.key === 'Enter') { if (event.key === 'Enter') {
setSearchTermFinderValue(null); setSearchTermFinderValue(null);
} }
if (event.key === 'Backspace' && !searchTermFinderValue) {
onBackspacePressWhileEmpty();
}
}} }}
/> />
); );

View File

@@ -146,6 +146,14 @@ export const PowerSearch: React.FC<PowerSearchProps> = ({
}, },
]); ]);
}} }}
onBackspacePressWhileEmpty={() => {
setSearchExpression((prevSearchExpression) => {
return prevSearchExpression.slice(
0,
prevSearchExpression.length - 1,
);
});
}}
/> />
</PowerSearchContainer> </PowerSearchContainer>
); );