Skip to content

Commit

Permalink
refactor: rename item.title to item.label to avoid conflict with antd (
Browse files Browse the repository at this point in the history
…#439)

* chore: move title to label

* docs: update doc

* chore: reorder
  • Loading branch information
zombieJ authored Mar 17, 2022
1 parent 907f621 commit 0cee119
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
color: #777 !important;
}
}
& > &-item-divider {
&-item-divider {
height: 1px;
margin: 1px 0;
overflow: hidden;
Expand Down
3 changes: 3 additions & 0 deletions docs/demo/items.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## items

<code src="../examples/items.tsx">
3 changes: 0 additions & 3 deletions docs/demo/options.md

This file was deleted.

20 changes: 10 additions & 10 deletions docs/examples/options.tsx → docs/examples/items.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import '../../assets/index.less';

export default () => (
<Menu
options={[
items={[
{
// MenuItem
title: 'Top Menu Item',
label: 'Top Menu Item',
key: 'top',
},
{
// MenuGroup
type: 'group',
title: 'Top Menu Group without children',
label: 'Top Menu Group without children',
},
{
// MenuGroup
type: 'group',
title: 'Top Menu Group with children',
label: 'Top Menu Group with children',
children: [
{
// MenuItem
title: 'Menu Item 1',
label: 'Menu Item 1',
key: 'inner1',
},
{
Expand All @@ -33,30 +33,30 @@ export default () => (
},
{
// MenuItem
title: 'Menu Item 2',
label: 'Menu Item 2',
key: 'inner2',
},
],
},
{
// SubMenu
title: 'SubMenu',
label: 'SubMenu',
key: 'sub1',
children: [
{
// MenuItem
title: 'Menu Item 1-1',
label: 'Menu Item 1-1',
key: 'inner11',
},

{
// SubMenu
title: 'SubMenu inner',
label: 'SubMenu inner',
key: 'sub1-1',
children: [
{
// MenuItem
title: 'Menu Item 111',
label: 'Menu Item 111',
key: 'inner1-1-1',
},
],
Expand Down
2 changes: 1 addition & 1 deletion src/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useMenuId } from './context/IdContext';
import PrivateContext from './context/PrivateContext';

export interface MenuItemProps
extends Omit<MenuItemType, 'title' | 'key'>,
extends Omit<MenuItemType, 'label' | 'key'>,
Omit<
React.HTMLAttributes<HTMLLIElement>,
'onClick' | 'onMouseEnter' | 'onMouseLeave' | 'onSelect'
Expand Down
4 changes: 3 additions & 1 deletion src/MenuItemGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { useFullPath, useMeasure } from './context/PathContext';
import type { MenuItemGroupType } from './interface';

export interface MenuItemGroupProps
extends Omit<MenuItemGroupType, 'type' | 'children'> {
extends Omit<MenuItemGroupType, 'type' | 'children' | 'label'> {
title?: React.ReactNode;

children?: React.ReactNode;

/** @private Internal filled key. Do not set it directly */
Expand Down
5 changes: 4 additions & 1 deletion src/SubMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import {
import { useMenuId } from '../context/IdContext';
import PrivateContext from '../context/PrivateContext';

export interface SubMenuProps extends Omit<SubMenuType, 'key' | 'children'> {
export interface SubMenuProps
extends Omit<SubMenuType, 'key' | 'children' | 'label'> {
title?: React.ReactNode;

children?: React.ReactNode;

/** @private Used for rest popup. Do not use in your prod */
Expand Down
6 changes: 3 additions & 3 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface ItemSharedProps {
}

export interface SubMenuType extends ItemSharedProps {
title?: React.ReactNode;
label?: React.ReactNode;

children: ItemType[];

Expand Down Expand Up @@ -35,7 +35,7 @@ export interface SubMenuType extends ItemSharedProps {
}

export interface MenuItemType extends ItemSharedProps {
title?: React.ReactNode;
label?: React.ReactNode;

disabled?: boolean;

Expand All @@ -54,7 +54,7 @@ export interface MenuItemType extends ItemSharedProps {
export interface MenuItemGroupType extends ItemSharedProps {
type: 'group';

title?: React.ReactNode;
label?: React.ReactNode;

children?: ItemType[];
}
Expand Down
13 changes: 6 additions & 7 deletions src/utils/nodeUtil.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import toArray from 'rc-util/lib/Children/toArray';
import type { MenuItemType, ItemType } from '../interface';
import type { ItemType } from '../interface';
import { Divider, MenuItem, MenuItemGroup, SubMenu } from '..';

export function parseChildren(
Expand Down Expand Up @@ -38,23 +38,23 @@ function convertItemsToNodes(list: ItemType[]) {
return (list || [])
.map((opt, index) => {
if (opt && typeof opt === 'object') {
const { children, key, type, ...restProps } = opt as any;
const { label, children, key, type, ...restProps } = opt as any;
const mergedKey = key ?? `tmp-${index}`;

// MenuItemGroup & SubMenuItem
if (children || type === 'group') {
if (type === 'group') {
// Group
return (
<MenuItemGroup key={mergedKey} {...restProps}>
<MenuItemGroup key={mergedKey} {...restProps} title={label}>
{convertItemsToNodes(children)}
</MenuItemGroup>
);
}

// Sub Menu
return (
<SubMenu key={mergedKey} {...restProps}>
<SubMenu key={mergedKey} {...restProps} title={label}>
{convertItemsToNodes(children)}
</SubMenu>
);
Expand All @@ -65,10 +65,9 @@ function convertItemsToNodes(list: ItemType[]) {
return <Divider key={mergedKey} {...restProps} />;
}

const { title, ...restMenuItemProps } = restProps as MenuItemType;
return (
<MenuItem key={mergedKey} {...restMenuItemProps}>
{title}
<MenuItem key={mergedKey} {...restProps}>
{label}
</MenuItem>
);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Options.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ describe('Options', () => {
// Invalid
null,
{
title: 'SubMenu',
label: 'SubMenu',
key: 'sub1',
children: [
{
type: 'group',
title: 'Menu Group',
label: 'Menu Group',
children: [
{
title: 'Menu Item 1',
label: 'Menu Item 1',
key: '1',
},
{
title: 'Menu Item 2',
label: 'Menu Item 2',
key: '2',
},
],
Expand Down

0 comments on commit 0cee119

Please sign in to comment.