Skip to content

Commit

Permalink
feat: support extra option (#734)
Browse files Browse the repository at this point in the history
Co-authored-by: afc163 <afc163@gmail.com>
Co-authored-by: iceweb1999 <1999iceweb@polyhedra.network>
  • Loading branch information
3 people authored Aug 14, 2024
1 parent d386b34 commit a05da80
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
11 changes: 11 additions & 0 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@
color: #777 !important;
}
}

&-item {
display: flex;
align-items: center;

.@{menuPrefixCls}-extra {
margin-left: auto;
font-size: 14px;
}
}

&-item-divider {
height: 1px;
margin: 1px 0;
Expand Down
1 change: 1 addition & 0 deletions docs/examples/items.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default () => (
// MenuItem
label: 'Top Menu Item',
key: 'top',
extra: '⌘B',
},
{
// MenuGroup
Expand Down
4 changes: 2 additions & 2 deletions src/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ const Menu = React.forwardRef<MenuRef, MenuProps>((props, ref) => {
measureChildList: React.ReactElement[],
] = React.useMemo(
() => [
parseItems(children, items, EMPTY_LIST, _internalComponents),
parseItems(children, items, EMPTY_LIST, {}),
parseItems(children, items, EMPTY_LIST, _internalComponents, prefixCls),
parseItems(children, items, EMPTY_LIST, {}, prefixCls),
],
[children, items, _internalComponents],
);
Expand Down
2 changes: 2 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export interface MenuItemType extends ItemSharedProps {

itemIcon?: RenderIconType;

extra?: React.ReactNode;

key: React.Key;

// >>>>> Active
Expand Down
9 changes: 7 additions & 2 deletions src/utils/nodeUtil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { parseChildren } from './commonUtil';
function convertItemsToNodes(
list: ItemType[],
components: Required<Components>,
prefixCls?: string,
) {
const {
item: MergedMenuItem,
Expand All @@ -20,7 +21,7 @@ function convertItemsToNodes(
return (list || [])
.map((opt, index) => {
if (opt && typeof opt === 'object') {
const { label, children, key, type, ...restProps } = opt as any;
const { label, children, key, type, extra, ...restProps } = opt as any;
const mergedKey = key ?? `tmp-${index}`;

// MenuItemGroup & SubMenuItem
Expand Down Expand Up @@ -50,6 +51,9 @@ function convertItemsToNodes(
return (
<MergedMenuItem key={mergedKey} {...restProps}>
{label}
{(!!extra || extra === 0) && (
<span className={`${prefixCls}-extra`}>{extra}</span>
)}
</MergedMenuItem>
);
}
Expand All @@ -64,6 +68,7 @@ export function parseItems(
items: ItemType[] | undefined,
keyPath: string[],
components: Components,
prefixCls?: string,
) {
let childNodes = children;

Expand All @@ -76,7 +81,7 @@ export function parseItems(
};

if (items) {
childNodes = convertItemsToNodes(items, mergedComponents);
childNodes = convertItemsToNodes(items, mergedComponents, prefixCls);
}

return parseChildren(childNodes, keyPath);
Expand Down
16 changes: 16 additions & 0 deletions tests/MenuItem.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,21 @@ describe('MenuItem', () => {

expect(container.querySelector('li')).toMatchSnapshot();
});

it('should set extra to option', () => {
const { container } = render(
<Menu
items={[
{
label: 'Top Menu Item',
key: 'top',
extra: '⌘B',
},
]}
/>,
);

expect(container.querySelector('li')).toMatchSnapshot();
});
});
});
16 changes: 16 additions & 0 deletions tests/__snapshots__/MenuItem.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`MenuItem overwrite default role should set extra to option 1`] = `
<li
class="rc-menu-item"
data-menu-id="rc-menu-uuid-test-top"
role="menuitem"
tabindex="-1"
>
Top Menu Item
<span
class="rc-menu-extra"
>
⌘B
</span>
</li>
`;

exports[`MenuItem overwrite default role should set role to listitem 1`] = `
<li
class="rc-menu-item"
Expand Down

0 comments on commit a05da80

Please sign in to comment.