Skip to content

Commit

Permalink
Allow custom toggle title to be passed to MenuItems
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Jan 10, 2025
1 parent 9b0a025 commit b70984b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/sidebar/components/GroupList/GroupListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ function GroupListItem({
leftChannelContent={leftChannelContent}
onClick={isSelectable ? focusGroup : toggleSubmenu}
onToggleSubmenu={toggleSubmenu}
submenuToggleTitle={`Show actions for ${group.name}`}
submenu={
<>
<ul>
Expand Down
15 changes: 14 additions & 1 deletion src/sidebar/components/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,20 @@ export type MenuItemProps = {

onClick?: (e: Event) => void;
onToggleSubmenu?: (e: Event) => void;

/**
* Contents of the submenu for this item. This is typically a list of
* `MenuItem` components with the `isSubmenuItem` prop set to `true`, but can
* include other content as well. The submenu is only rendered if
* `isSubmenuVisible` is `true`.
*/
submenu?: ComponentChildren;

/**
* Custom title for the submenu toggle button.
* Omit this prop if there is no submenu.
*/
submenuToggleTitle?: string;
};

/**
Expand Down Expand Up @@ -150,6 +157,7 @@ export default function MenuItem({
onClick,
onToggleSubmenu,
submenu,
submenuToggleTitle,
}: MenuItemProps) {
const menuItemRef = useRef<HTMLAnchorElement | HTMLDivElement | null>(null);

Expand Down Expand Up @@ -226,7 +234,12 @@ export default function MenuItem({
)}
{hasSubmenuVisible && (
<SubmenuToggle
title={`Show actions for ${label}`}
title={
submenuToggleTitle ??
(typeof label === 'string'
? `Show actions for ${label}`
: `Toggle submenu`)
}
isExpanded={isSubmenuVisible}
onToggleSubmenu={onToggleSubmenu}
/>
Expand Down
29 changes: 29 additions & 0 deletions src/sidebar/components/test/MenuItem-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,35 @@ describe('MenuItem', () => {
}
// no assert needed
});

[
{
label: 'The label',
submenuToggleTitle: 'Explicit title',
expectedTitle: 'Explicit title',
},
{
label: <span>Rich label</span>,
submenuToggleTitle: 'Explicit title',
expectedTitle: 'Explicit title',
},
{ label: 'The label', expectedTitle: 'Show actions for The label' },
{ label: <span>Rich label</span>, expectedTitle: 'Toggle submenu' },
].forEach(({ label, submenuToggleTitle, expectedTitle }) => {
it('shows right title in SubmenuToggle', () => {
const wrapper = createMenuItem({
label,
submenuToggleTitle,
isSubmenuVisible: true,
submenu: <div role="menuitem">Submenu content</div>,
});

assert.equal(
wrapper.find('SubmenuToggle').prop('title'),
expectedTitle,
);
});
});
});

it(
Expand Down

0 comments on commit b70984b

Please sign in to comment.