diff --git a/components/tabs/__tests__/index.test.js b/components/tabs/__tests__/index.test.js index 6da4728897..77cad3c172 100644 --- a/components/tabs/__tests__/index.test.js +++ b/components/tabs/__tests__/index.test.js @@ -16,6 +16,9 @@ describe('Tabs', () => { foo + {undefined} + {null} + {false} , ); }); @@ -32,6 +35,10 @@ describe('Tabs', () => { wrapper.find('.anticon-close').simulate('click'); expect(handleEdit).toHaveBeenCalledWith('1', 'remove'); }); + + it('validateElement', () => { + expect(wrapper.find('.ant-tabs-tab').length).toBe(1); + }); }); describe('tabPosition', () => { diff --git a/components/tabs/index.tsx b/components/tabs/index.tsx index 13af137a83..14db911fa9 100755 --- a/components/tabs/index.tsx +++ b/components/tabs/index.tsx @@ -129,31 +129,29 @@ export default class Tabs extends React.Component { let childrenWithClose: React.ReactElement[] = []; if (type === 'editable-card') { childrenWithClose = []; - React.Children.forEach( - children as React.ReactNode, - (child: React.ReactElement, index) => { - let closable = child.props.closable; - closable = typeof closable === 'undefined' ? true : closable; - const closeIcon = closable ? ( - this.removeTab(child.key as string, e)} - /> - ) : null; - childrenWithClose.push( - React.cloneElement(child, { - tab: ( -
- {child.props.tab} - {closeIcon} -
- ), - key: child.key || index, - }), - ); - }, - ); + React.Children.forEach(children as React.ReactNode, (child, index) => { + if (!React.isValidElement(child)) return child; + let closable = child.props.closable; + closable = typeof closable === 'undefined' ? true : closable; + const closeIcon = closable ? ( + this.removeTab(child.key as string, e)} + /> + ) : null; + childrenWithClose.push( + React.cloneElement(child, { + tab: ( +
+ {child.props.tab} + {closeIcon} +
+ ), + key: child.key || index, + }), + ); + }); // Add new tab handler if (!hideAdd) { tabBarExtraContent = (