Skip to content

Commit a0acfe6

Browse files
committed
feat: add support for pasting terms into the search bar
1 parent d07c465 commit a0acfe6

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

client/src/components/Shared/SearchBar/SearchBar.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ const SearchBar: React.FC<SearchBarProps> = ({ handleSubmit }) => {
3636
}
3737

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

128+
const handlePaste = (event: any) => {
129+
const pastedOptions = event.clipboardData.getData("text").split(",").map((item: string) => { return { suggestion: item.trim() } });
130+
setSelectedOptions(pastedOptions);
131+
// we don't want the code to also run what's in onInputChange for the Autocomplete since everything is handled here
132+
event.preventDefault()
133+
}
134+
128135
return (
129136
<>
130137
<Box id="search-bar-container" width={isMobile ? '95%' : '75%'}>
@@ -155,6 +162,7 @@ const SearchBar: React.FC<SearchBarProps> = ({ handleSubmit }) => {
155162
<Box>
156163
<TextField
157164
{...params}
165+
onPaste={handlePaste}
158166
variant="standard"
159167
label="Search Terms"
160168
/>

0 commit comments

Comments
 (0)