Skip to content

Commit

Permalink
fix(@clayui/drop-down): fix contextual menu error not rendering icons…
Browse files Browse the repository at this point in the history
… without spaces
  • Loading branch information
matuzalemsteles committed Mar 29, 2022
1 parent 6fa494d commit a115add
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
53 changes: 34 additions & 19 deletions packages/clay-drop-down/src/DropDownWithItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ export interface IProps extends IDropDownContentProps {
searchValue?: string;
}

const findNested = <
T extends {items?: Array<T>; [key: string]: any},
K extends keyof T
>(
items: Array<T>,
key: K
): T | undefined =>
items.find((item) => {
if (item[key]) {
return true;
}

// Ignore the search if the nested items are part of a contextual submenu
// because it will be in another menu and the current menu does not need
// to know the information of what exists inside the contextual one, like
// knowing if there is an icon.
if (item.items && item.type !== 'contextual') {
return findNested(item.items, key);
}

return false;
});

interface IInternalItem {
spritemap?: string;
}
Expand Down Expand Up @@ -230,6 +253,15 @@ const Contextual: React.FunctionComponent<
const menuElementRef = useRef<HTMLDivElement>(null);
const timeoutHandleRef = useRef<any>(null);

const hasRightSymbols = React.useMemo(
() => items && !!findNested(items, 'symbolRight'),
[items]
);
const hasLeftSymbols = React.useMemo(
() => items && !!findNested(items, 'symbolLeft'),
[items]
);

return (
<ClayDropDown.Item
{...otherProps}
Expand Down Expand Up @@ -266,6 +298,8 @@ const Contextual: React.FunctionComponent<
active={visible}
alignElementRef={triggerElementRef}
alignmentPosition={8}
hasLeftSymbols={hasLeftSymbols}
hasRightSymbols={hasRightSymbols}
onSetActive={setVisible}
ref={menuElementRef}
>
Expand Down Expand Up @@ -376,25 +410,6 @@ const DropDownContent: React.FunctionComponent<IDropDownContentProps> = ({
</ClayDropDown.ItemList>
);

const findNested = <
T extends {items?: Array<T>; [key: string]: any},
K extends keyof T
>(
items: Array<T>,
key: K
): T | undefined =>
items.find((item) => {
if (item[key]) {
return true;
}

if (item.items) {
return findNested(item.items, key);
}

return false;
});

export const ClayDropDownWithItems: React.FunctionComponent<IProps> = ({
active,
alignmentByViewport,
Expand Down
6 changes: 5 additions & 1 deletion packages/clay-drop-down/stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,11 @@ storiesOf('Components|ClayDropDown', module)
{type: 'divider'},
{
items: [
{label: 'Basic Document'},
{
label: 'Basic Document',
symbolLeft: 'document',
symbolRight: 'check',
},
{label: 'Contract'},
{label: 'Marketing Banner'},
{label: 'Spreadsheet'},
Expand Down

0 comments on commit a115add

Please sign in to comment.