Skip to content

Commit

Permalink
fix(autocomplete): tags not generated for preselected options
Browse files Browse the repository at this point in the history
Signed-off-by: gitdallas <[email protected]>
  • Loading branch information
gitdallas committed Sep 26, 2023
1 parent 15ec166 commit d6b1957
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 85 deletions.
21 changes: 8 additions & 13 deletions client/src/app/components/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,13 @@ export const Autocomplete: React.FC<IAutocompleteProps> = ({
}) => {
const [inputValue, setInputValue] = useState(searchString);
const [menuIsOpen, setMenuIsOpen] = useState(false);
const [currentChips, setCurrentChips] = useState<Set<string>>(
new Set(selections)
);
const [hint, setHint] = useState("");
const [menuItems, setMenuItems] = useState<React.ReactElement[]>([]);

/** refs used to detect when clicks occur inside vs outside of the textInputGroup and menu popper */
const menuRef = useRef<HTMLDivElement>(null);
const searchInputRef = useRef<HTMLInputElement>(null);

React.useEffect(() => {
onChange([...currentChips]);
buildMenu();
}, [currentChips]);

React.useEffect(() => {
buildMenu();
}, [options]);
Expand All @@ -67,7 +59,7 @@ export const Autocomplete: React.FC<IAutocompleteProps> = ({
.filter(
(item: string, index: number, arr: string[]) =>
arr.indexOf(item) === index &&
!currentChips.has(item) &&
!selections.includes(item) &&

Check warning on line 62 in client/src/app/components/Autocomplete.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/Autocomplete.tsx#L62

Added line #L62 was not covered by tests
(!inputValue || item.toLowerCase().includes(inputValue.toLowerCase()))
)
.map((currentValue, index) => (
Expand Down Expand Up @@ -129,9 +121,11 @@ export const Autocomplete: React.FC<IAutocompleteProps> = ({

/** callback for removing a chip from the chip selections */
const deleteChip = (chipToDelete: string) => {
const newChips = new Set(currentChips);
const newChips = new Set(selections);

Check warning on line 124 in client/src/app/components/Autocomplete.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/Autocomplete.tsx#L124

Added line #L124 was not covered by tests
newChips.delete(chipToDelete);
setCurrentChips(newChips);
// newChips.delete(chipToDelete);
onChange(Array.from(newChips));

Check warning on line 127 in client/src/app/components/Autocomplete.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/Autocomplete.tsx#L127

Added line #L127 was not covered by tests
// setCurrentChips(newChips);
};

/** add the given string as a chip in the chip group and clear the input */
Expand All @@ -145,7 +139,8 @@ export const Autocomplete: React.FC<IAutocompleteProps> = ({
}
newChipText = matchingOption;
}
setCurrentChips(new Set([...currentChips, newChipText]));
// setCurrentChips(new Set([...currentChips, newChipText]));
onChange(Array.from(new Set([...selections, newChipText])));

Check warning on line 143 in client/src/app/components/Autocomplete.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/components/Autocomplete.tsx#L143

Added line #L143 was not covered by tests
setInputValue("");
setMenuIsOpen(false);
};
Expand Down Expand Up @@ -301,7 +296,7 @@ export const Autocomplete: React.FC<IAutocompleteProps> = ({
</FlexItem>
<FlexItem key="chips">
<Flex spaceItems={{ default: "spaceItemsXs" }}>
{Array.from(currentChips).map((currentChip) => (
{selections.map((currentChip) => (
<FlexItem key={currentChip}>
<Label color={labelColor} onClose={() => deleteChip(currentChip)}>
{currentChip}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,78 +445,36 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
name="tags"
label={t("terms.tags")}
fieldId="tags"
renderInput={({ field: { value, name, onChange } }) => (
<Autocomplete
noResultsMessage={t("message.noResultsFoundTitle")}
onChange={(selections) => {
onChange(
selections
.map((sel) => getTagRef(sel))
.filter((sel) => sel !== undefined) as TagRef[]
);
}}
options={tagOptions.map((o) => o.value)}
placeholderText={t("composed.selectMany", {
what: t("terms.tags").toLowerCase(),
})}
searchInputAriaLabel="tags-select-toggle"
selections={
value
.map(
(formTag) =>
tags?.find((tagRef) => tagRef.name === formTag.name)
)
.map((matchingTag) =>
matchingTag ? matchingTag.name : undefined
)
.filter((e) => e !== undefined) as string[]
}
/>
// <SimpleSelect
// maxHeight={DEFAULT_SELECT_MAX_HEIGHT}
// placeholderText={t("composed.selectMany", {
// what: t("terms.tags").toLowerCase(),
// })}
// id="tags-select"
// variant="typeaheadmulti"
// toggleId="tags-select-toggle"
// toggleAriaLabel="tags dropdown toggle"
// aria-label={name}
// value={value
// .map((formTag) =>
// tags?.find((tagRef) => tagRef.name === formTag.name)
// )
// .map((matchingTag) =>
// matchingTag
// ? {
// value: matchingTag.name,
// toString: () => matchingTag.name,
// }
// : undefined
// )
// .filter((e) => e !== undefined)}
// options={tagOptions}
// onChange={(selection) => {
// const selectionWithValue = selection.toString();

// const currentValue = value || [];
// const e = currentValue.find(
// (f) => f.name === selectionWithValue
// );
// if (e) {
// onChange(
// currentValue.filter((f) => f.name !== selectionWithValue)
// );
// } else {
// const tag = getTagRef(selectionWithValue);
// if (currentValue && typeof tag !== "undefined")
// onChange([...currentValue, tag]);
// }
// }}
// onClear={() => onChange([])}
// noResultsFoundText={t("message.noResultsFoundTitle")}
// />
)}
renderInput={({ field: { value, name, onChange } }) => {
const selections = value
.map(
(formTag) =>
tags?.find((tagRef) => tagRef.name === formTag.name)

Check warning on line 452 in client/src/app/pages/applications/components/application-form/application-form.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/components/application-form/application-form.tsx#L452

Added line #L452 was not covered by tests
)
.map((matchingTag) =>
matchingTag ? matchingTag.name : undefined
)
.filter((e) => e !== undefined) as string[];

Check warning on line 457 in client/src/app/pages/applications/components/application-form/application-form.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/components/application-form/application-form.tsx#L457

Added line #L457 was not covered by tests

return (
<Autocomplete
noResultsMessage={t("message.noResultsFoundTitle")}
onChange={(selections) => {
onChange(

Check warning on line 463 in client/src/app/pages/applications/components/application-form/application-form.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/components/application-form/application-form.tsx#L462-L463

Added lines #L462 - L463 were not covered by tests
selections
.map((sel) => getTagRef(sel))
.filter((sel) => sel !== undefined) as TagRef[]

Check warning on line 466 in client/src/app/pages/applications/components/application-form/application-form.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/components/application-form/application-form.tsx#L465-L466

Added lines #L465 - L466 were not covered by tests
);
}}
options={tagOptions.map((o) => o.value)}

Check warning on line 469 in client/src/app/pages/applications/components/application-form/application-form.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/pages/applications/components/application-form/application-form.tsx#L469

Added line #L469 was not covered by tests
placeholderText={t("composed.selectMany", {
what: t("terms.tags").toLowerCase(),
})}
searchInputAriaLabel="tags-select-toggle"
selections={selections}
/>
);
}}
/>
<HookFormPFGroupController
control={control}
Expand Down

0 comments on commit d6b1957

Please sign in to comment.