Skip to content

Commit

Permalink
feat: add support for pasting terms into the search bar
Browse files Browse the repository at this point in the history
  • Loading branch information
katiestahl committed Mar 5, 2024
1 parent d07c465 commit a0acfe6
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions client/src/components/Shared/SearchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ const SearchBar: React.FC<SearchBarProps> = ({ handleSubmit }) => {
}

// support searching for terms that the API may not return (add user's typed term to options if it's not already there)
if (
if ( typedSearchTerm &&
autocompleteOptions.filter(
(option: { suggestion: string }) => option.suggestion === typedSearchTerm
).length === 0
).length === 0 && typedSearchTerm.trim() !== ""
) {
autocompleteOptions = [
{ suggestion: typedSearchTerm },
Expand Down Expand Up @@ -125,6 +125,13 @@ const SearchBar: React.FC<SearchBarProps> = ({ handleSubmit }) => {
}
}, [selectedOptions]);

const handlePaste = (event: any) => {
const pastedOptions = event.clipboardData.getData("text").split(",").map((item: string) => { return { suggestion: item.trim() } });
setSelectedOptions(pastedOptions);
// we don't want the code to also run what's in onInputChange for the Autocomplete since everything is handled here
event.preventDefault()
}

return (
<>
<Box id="search-bar-container" width={isMobile ? '95%' : '75%'}>
Expand Down Expand Up @@ -155,6 +162,7 @@ const SearchBar: React.FC<SearchBarProps> = ({ handleSubmit }) => {
<Box>
<TextField
{...params}
onPaste={handlePaste}
variant="standard"
label="Search Terms"
/>
Expand Down

0 comments on commit a0acfe6

Please sign in to comment.