Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MenuItem): be able to provide a component to title #1938

Merged
merged 3 commits into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/components/Menu/MenuItem/MenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
/* eslint-disable react/jsx-props-no-spreading */
import React, { ForwardedRef, forwardRef, useCallback, useEffect, useLayoutEffect, useMemo, useRef } from "react";
import React, {
AriaAttributes,
ForwardedRef,
ReactElement,
forwardRef,
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef
} from "react";
import cx from "classnames";
import { isFunction } from "lodash-es";
import { ComponentDefaultTestId, getTestId } from "../../../tests/test-ids-utils";
Expand Down Expand Up @@ -70,9 +80,15 @@ export interface MenuItemProps extends VibeComponentProps {
* 2. click/hover on icon button will open the sub menu
*/
splitMenuItem?: boolean;
"aria-label"?: AriaAttributes["aria-label"];
}

const MenuItem: VibeComponent<MenuItemProps> & {
interface MenuItemTitleComponentProps extends Omit<MenuItemProps, "title"> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YossiSaadi changed it eventually because MenuItemProps is exported so it breaks stuff

title: ReactElement;
"aria-label": AriaAttributes["aria-label"];
}

const MenuItem: VibeComponent<MenuItemProps | MenuItemTitleComponentProps> & {
iconType?: typeof Icon.type;
tooltipPositions?: typeof DialogPosition;
isSelectable?: boolean;
Expand Down Expand Up @@ -115,6 +131,7 @@ const MenuItem: VibeComponent<MenuItemProps> & {
onMouseLeave,
shouldScrollMenu,
"data-testid": dataTestId,
"aria-label": ariaLabel,
splitMenuItem = false
},
ref: ForwardedRef<HTMLElement>
Expand Down Expand Up @@ -220,6 +237,8 @@ const MenuItem: VibeComponent<MenuItemProps> & {
[setSubMenuIsOpenByIndex, index, closeMenu]
);

// if "title" is a component ariaLabel is mandatory
const iconLabel = ariaLabel || (title as string);
const renderSubMenuIconIfNeeded = () => {
if (!hasChildren) return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say not to take title's type into account. aria || title

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it fails TS, it's not smart enough


Expand All @@ -243,7 +262,7 @@ const MenuItem: VibeComponent<MenuItemProps> & {
<Icon
clickable={false}
icon={DropdownChevronRight}
iconLabel={title}
iconLabel={iconLabel}
className={styles.subMenuIcon}
ignoreFocusStyle
iconSize={18}
Expand Down Expand Up @@ -279,7 +298,7 @@ const MenuItem: VibeComponent<MenuItemProps> & {
iconType={finalIconType}
clickable={false}
icon={icon}
iconLabel={title}
iconLabel={iconLabel}
className={styles.icon}
ignoreFocusStyle
style={iconStyle}
Expand Down
Loading