Skip to content

Commit fb20367

Browse files
Guan Putao1991123
Guan Pu
authored andcommitted
fix(Tab): can not render when child have null item
1 parent cd4a577 commit fb20367

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/tab/tab.jsx

+12-8
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ export default class Tab extends Component {
133133
let activeKey = props.activeKey || props.defaultActiveKey;
134134
if (!activeKey) {
135135
React.Children.forEach(props.children, (child, index) => {
136-
/* eslint-disable eqeqeq */
137-
if (activeKey == undefined && !child.props.disabled) {
138-
activeKey = child.key || index;
136+
if (React.isValidElement(child)) {
137+
/* eslint-disable eqeqeq */
138+
if (activeKey == undefined && !child.props.disabled) {
139+
activeKey = child.key || index;
140+
}
139141
}
140142
});
141143
}
@@ -145,11 +147,13 @@ export default class Tab extends Component {
145147
getNextActiveKey(isNext) {
146148
const children = [];
147149
React.Children.forEach(this.props.children, child => {
148-
if (!child.props.disabled) {
149-
if (isNext) {
150-
children.push(child);
151-
} else {
152-
children.unshift(child);
150+
if (React.isValidElement(child)) {
151+
if (!child.props.disabled) {
152+
if (isNext) {
153+
children.push(child);
154+
} else {
155+
children.unshift(child);
156+
}
153157
}
154158
}
155159
});

test/tab/index-spec.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ describe('Tab', () => {
1313

1414
describe('simple', () => {
1515
it('should render only one tab', () => {
16-
const wrapper = mount(<Tab><Tab.Item title="foo" /></Tab>)
16+
const wrapper = mount(<Tab><Tab.Item title="foo" /></Tab>);
1717
assert(wrapper.find('.next-tabs-tab').length === 1);
18-
})
18+
});
1919
});
2020

2121
describe('with props', () => {
2222
let wrapper;
23-
const panes = [1, 2, 3, 4].map((item, index) => <Tab.Item title={`item ${item}`} key={index}></Tab.Item>);
23+
const panes = [1, 2, 3, 4, 5].map((item, index) => item < 5 ? <Tab.Item title={`item ${item}`} key={index}></Tab.Item> : null);
2424

2525
afterEach(() => {
2626
wrapper.unmount();
@@ -68,7 +68,7 @@ describe('Tab', () => {
6868
const wrapper2 = mount(<Tab tabPosition="right" shape="wrapped">{panes}</Tab>);
6969
assert(wrapper2.find('.next-tabs').hasClass('next-tabs-vertical'));
7070

71-
const wrapper3 = mount(<Tab tabPosition="bottom" shape="wrapped">{panes}</Tab>)
71+
const wrapper3 = mount(<Tab tabPosition="bottom" shape="wrapped">{panes}</Tab>);
7272
assert(wrapper3.find('.next-tabs').hasClass('next-tabs-bottom'));
7373
});
7474

@@ -83,7 +83,7 @@ describe('Tab', () => {
8383
});
8484

8585
it('should render extra in left side', () => {
86-
wrapper = mount(<Tab shape="wrapped" tabPosition="left" extra={<button className="test-extra">hello world</button>}>{panes}</Tab>)
86+
wrapper = mount(<Tab shape="wrapped" tabPosition="left" extra={<button className="test-extra">hello world</button>}>{panes}</Tab>);
8787
assert(wrapper.find('.test-extra').length === 1);
8888
});
8989

@@ -96,15 +96,15 @@ describe('Tab', () => {
9696
});
9797

9898
it('should render with contentStyle & contentClassName', () => {
99-
const contentStyle={ background: 'red' };
99+
const contentStyle = { background: 'red' };
100100
wrapper = mount(<Tab contentStyle={contentStyle} contentClassName="custom-content">{panes}</Tab>);
101101
assert(wrapper.find('.next-tabs-content').hasClass('custom-content'));
102102
const tabContent = wrapper.find('.next-tabs-content').instance();
103103
assert(tabContent.style.background === 'red' || tabContent.style.background.startsWith('red'));
104104
});
105105

106106
it('should render with tabRender', () => {
107-
wrapper = mount(<Tab tabRender={(key, props) => <div className="custom-tab-item">{props.title}</div>}>{panes}</Tab>)
107+
wrapper = mount(<Tab tabRender={(key, props) => <div className="custom-tab-item">{props.title}</div>}>{panes}</Tab>);
108108
assert(wrapper.find('.custom-tab-item').length === 4);
109109
});
110110

@@ -162,7 +162,7 @@ describe('Tab', () => {
162162
wrapper = mount(<Tab onClose={key => tabKey = key}>
163163
<Tab.Item title="foo" />
164164
<Tab.Item title="bar" closeable />
165-
</Tab>)
165+
</Tab>);
166166
const closeIcon = wrapper.find('.next-icon-close');
167167
assert(closeIcon.length === 1);
168168
closeIcon.simulate('click');
@@ -222,7 +222,7 @@ describe('Tab', () => {
222222
it('should scrollToActiveTab', () => {
223223
wrapper = mount(<div style={boxStyle}><Tab activeKey="9">{panes}</Tab></div>, { attachTo: target });
224224
// console.log(wrapper.find('.next-tabs').instance());
225-
wrapper.setProps({ activeKey: "3" });
225+
wrapper.setProps({ activeKey: '3' });
226226
// wrapper.find('.next-tabs-tab').at(1).simulate('click');
227227

228228
});

0 commit comments

Comments
 (0)