From d6b1957cbd7d248928da014b05b4a8a6e88044ed Mon Sep 17 00:00:00 2001 From: gitdallas Date: Tue, 26 Sep 2023 15:30:29 -0500 Subject: [PATCH] fix(autocomplete): tags not generated for preselected options Signed-off-by: gitdallas --- client/src/app/components/Autocomplete.tsx | 21 ++-- .../application-form/application-form.tsx | 102 ++++++------------ 2 files changed, 38 insertions(+), 85 deletions(-) diff --git a/client/src/app/components/Autocomplete.tsx b/client/src/app/components/Autocomplete.tsx index 71b4672e91..ad9236e754 100644 --- a/client/src/app/components/Autocomplete.tsx +++ b/client/src/app/components/Autocomplete.tsx @@ -42,9 +42,6 @@ export const Autocomplete: React.FC = ({ }) => { const [inputValue, setInputValue] = useState(searchString); const [menuIsOpen, setMenuIsOpen] = useState(false); - const [currentChips, setCurrentChips] = useState>( - new Set(selections) - ); const [hint, setHint] = useState(""); const [menuItems, setMenuItems] = useState([]); @@ -52,11 +49,6 @@ export const Autocomplete: React.FC = ({ const menuRef = useRef(null); const searchInputRef = useRef(null); - React.useEffect(() => { - onChange([...currentChips]); - buildMenu(); - }, [currentChips]); - React.useEffect(() => { buildMenu(); }, [options]); @@ -67,7 +59,7 @@ export const Autocomplete: React.FC = ({ .filter( (item: string, index: number, arr: string[]) => arr.indexOf(item) === index && - !currentChips.has(item) && + !selections.includes(item) && (!inputValue || item.toLowerCase().includes(inputValue.toLowerCase())) ) .map((currentValue, index) => ( @@ -129,9 +121,11 @@ export const Autocomplete: React.FC = ({ /** callback for removing a chip from the chip selections */ const deleteChip = (chipToDelete: string) => { - const newChips = new Set(currentChips); + const newChips = new Set(selections); newChips.delete(chipToDelete); - setCurrentChips(newChips); + // newChips.delete(chipToDelete); + onChange(Array.from(newChips)); + // setCurrentChips(newChips); }; /** add the given string as a chip in the chip group and clear the input */ @@ -145,7 +139,8 @@ export const Autocomplete: React.FC = ({ } newChipText = matchingOption; } - setCurrentChips(new Set([...currentChips, newChipText])); + // setCurrentChips(new Set([...currentChips, newChipText])); + onChange(Array.from(new Set([...selections, newChipText]))); setInputValue(""); setMenuIsOpen(false); }; @@ -301,7 +296,7 @@ export const Autocomplete: React.FC = ({ - {Array.from(currentChips).map((currentChip) => ( + {selections.map((currentChip) => (