Skip to content

Commit

Permalink
cleanup, stick with selection naming rather than sometimes chip
Browse files Browse the repository at this point in the history
Signed-off-by: gitdallas <dallas.nicol@gmail.com>
  • Loading branch information
gitdallas committed Sep 26, 2023
1 parent be944ff commit 7c2fa1b
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions client/src/app/components/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,33 +119,32 @@ export const Autocomplete: React.FC<IAutocompleteProps> = ({
buildMenu();
};

/** callback for removing a chip from the chip selections */
const deleteChip = (chipToDelete: string) => {
const newChips = new Set(selections);
newChips.delete(chipToDelete);
onChange(Array.from(newChips));
/** callback for removing a selection */
const deleteSelection = (selectionToDelete: string) => {
onChange(selections.filter((s) => s !== selectionToDelete));

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
};

/** add the given string as a chip in the chip group and clear the input */
const addChip = (newChipText: string) => {
/** add the given string as a selection */
const addSelection = (newSelectionText: string) => {
if (!allowUserOptions) {
const matchingOption = options.find(
(o) => o.toLowerCase() === (hint || newChipText).toLowerCase()
(o) => o.toLowerCase() === (hint || newSelectionText).toLowerCase()
);
if (!matchingOption) {
console.log({ matchingOption, newSelectionText, options });

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L133 was not covered by tests
if (!matchingOption || selections.includes(matchingOption)) {
return;
}
newChipText = matchingOption;
newSelectionText = matchingOption;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L137 was not covered by tests
}
onChange(Array.from(new Set([...selections, newChipText])));
onChange([...selections, newSelectionText]);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L139 was not covered by tests
setInputValue("");
setMenuIsOpen(false);
};

/** add the current input value as a chip */
/** add the current input value as a selection */
const handleEnter = () => {
if (inputValue.length) {
addChip(inputValue);
addSelection(inputValue);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L147 was not covered by tests
}
};

Expand Down Expand Up @@ -208,13 +207,13 @@ export const Autocomplete: React.FC<IAutocompleteProps> = ({
closeMenu && setMenuIsOpen(false);
};

/** add the text of the selected item as a new chip */
/** add the text of the selected menu item to the selected items */
const onSelect = (event?: React.MouseEvent<Element, MouseEvent>) => {
if (!event) {
return;
}
const selectedText = (event.target as HTMLElement).innerText;
addChip(selectedText);
addSelection(selectedText);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L216 was not covered by tests
event.stopPropagation();
focusTextInput(true);
};
Expand Down Expand Up @@ -293,10 +292,13 @@ export const Autocomplete: React.FC<IAutocompleteProps> = ({
</FlexItem>
<FlexItem key="chips">
<Flex spaceItems={{ default: "spaceItemsXs" }}>
{selections.map((currentChip) => (
<FlexItem key={currentChip}>
<Label color={labelColor} onClose={() => deleteChip(currentChip)}>
{currentChip}
{selections.map((currentSelection) => (
<FlexItem key={currentSelection}>

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L296 was not covered by tests
<Label
color={labelColor}
onClose={() => deleteSelection(currentSelection)}

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L299 was not covered by tests
>
{currentSelection}
</Label>
</FlexItem>
))}
Expand Down

0 comments on commit 7c2fa1b

Please sign in to comment.