Skip to content

Commit b933ba9

Browse files
committed
🐛 Allow none-menu-item in Menu children
close ant-design/ant-design#11517 (comment)
1 parent 7e19257 commit b933ba9

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/SubPopupMenu.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ export function getActiveKey(props, originalActiveKey) {
3535
if (activeKey) {
3636
let found;
3737
loopMenuItem(children, (c, i) => {
38-
if (c && !c.props.disabled && activeKey === getKeyFromChildrenIndex(c, eventKey, i)) {
38+
if (
39+
c &&
40+
c.props &&
41+
!c.props.disabled &&
42+
activeKey === getKeyFromChildrenIndex(c, eventKey, i)
43+
) {
3944
found = true;
4045
}
4146
});
@@ -266,6 +271,10 @@ export class SubPopupMenu extends React.Component {
266271
const props = this.props;
267272
const key = getKeyFromChildrenIndex(child, props.eventKey, i);
268273
const childProps = child.props;
274+
// https://github.com/ant-design/ant-design/issues/11517#issuecomment-477403055
275+
if (!childProps || typeof child.type === 'string') {
276+
return child;
277+
}
269278
const isActive = key === state.activeKey;
270279
const newChildProps = {
271280
mode: childProps.mode || props.mode,
@@ -348,8 +357,6 @@ export class SubPopupMenu extends React.Component {
348357
delete props.onClick;
349358

350359
return (
351-
// ESLint is not smart enough to know that the type of `children` was checked.
352-
/* eslint-disable */
353360
<DOMWrap
354361
{...props}
355362
prefixCls={prefixCls}
@@ -367,7 +374,6 @@ export class SubPopupMenu extends React.Component {
367374
(c, i) => this.renderMenuItem(c, i, eventKey || '0-menu-'),
368375
)}
369376
</DOMWrap>
370-
/*eslint-enable */
371377
);
372378
}
373379
}

tests/Menu.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ describe('Menu', () => {
9797
expect(wrapper.find('MenuItem').first().props().active).toBe(true);
9898
});
9999

100+
it('should render none menu item children', () => {
101+
expect(() => {
102+
mount(
103+
<Menu activeKey="1">
104+
<MenuItem key="1">1</MenuItem>
105+
<MenuItem key="2">2</MenuItem>
106+
string
107+
{'string'}
108+
{null}
109+
{undefined}
110+
{12345}
111+
<div />
112+
<input />
113+
</Menu>
114+
);
115+
}).not.toThrow();
116+
});
117+
100118
it('select multiple items', () => {
101119
const wrapper = mount(
102120
<Menu multiple>

0 commit comments

Comments
 (0)