Skip to content

Commit

Permalink
PR suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Bolton <[email protected]>
  • Loading branch information
ibolton336 committed Apr 2, 2024
1 parent 67e8ce8 commit 4228e53
Showing 1 changed file with 15 additions and 53 deletions.
68 changes: 15 additions & 53 deletions client/src/app/components/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,73 +324,35 @@ export const Autocomplete: React.FC<IAutocompleteProps> = ({
);

const renderMenuItems = () => {
if (isGrouped && groupedOptions) {
return Object.entries(groupedOptions).map(([groupName, groupOptions]) => (
<React.Fragment key={groupName}>
const effectiveGroupedOptions = isGrouped
? groupedOptions
: { undefined: filteredOptions };

return Object.entries(effectiveGroupedOptions).map(
([groupName, groupOptions], index) => (
<React.Fragment key={groupName || `ungrouped-${index}`}>
<MenuGroup label={groupName}>
<MenuList>
{groupOptions.map((option) => (
<MenuItem
key={option.uniqueId}
key={option.uniqueId || option.id}
itemId={option.id.toString()}
onClick={(e) => handleMenuItemOnSelect(e, option)}
>
{toString(option.name)}
</MenuItem>
))}
{/* if supplied, add the menu heading */}
{menuHeader ? (
<>
<MenuItem isDisabled key="heading" itemId="-2">
{menuHeader}
</MenuItem>
<Divider key="divider" />
</>
) : undefined}

{/* show a disabled "no result" when all menu items are filtered out */}
{groupOptions.length === 0 ? (
<MenuItem isDisabled key="no result" itemId="-1">
{noResultsMessage}
</MenuItem>
) : undefined}
</MenuList>
</MenuGroup>
<Divider />
{/* Add a divider except after the last group */}
{index < Object.keys(effectiveGroupedOptions).length - 1 && (
<Divider />
)}
</React.Fragment>
));
} else {
return (
<MenuList>
{/* if supplied, add the menu heading */}
{menuHeader ? (
<>
<MenuItem isDisabled key="heading" itemId="-2">
{menuHeader}
</MenuItem>
<Divider key="divider" />
</>
) : undefined}

{/* show a disabled "no result" when all menu items are filtered out */}
{filteredOptions.length === 0 ? (
<MenuItem isDisabled key="no result" itemId="-1">
{noResultsMessage}
</MenuItem>
) : undefined}
{filteredOptions.map((option) => (
<MenuItem
key={option.id}
itemId={option.id.toString()}
onClick={(e) => handleMenuItemOnSelect(e, option)}
>
{toString(option.name)}
</MenuItem>
))}
</MenuList>
);
}
)
);
};

const menu = (
<Menu ref={menuRef} onKeyDown={handleMenuOnKeyDown} isScrollable>
<MenuContent>{renderMenuItems()}</MenuContent>
Expand Down

0 comments on commit 4228e53

Please sign in to comment.