Skip to content

Commit

Permalink
fix(Tab): can not render when child have null item
Browse files Browse the repository at this point in the history
  • Loading branch information
Guan Pu authored and tao1991123 committed Dec 21, 2018
1 parent cd4a577 commit fb20367
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
20 changes: 12 additions & 8 deletions src/tab/tab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ export default class Tab extends Component {
let activeKey = props.activeKey || props.defaultActiveKey;
if (!activeKey) {
React.Children.forEach(props.children, (child, index) => {
/* eslint-disable eqeqeq */
if (activeKey == undefined && !child.props.disabled) {
activeKey = child.key || index;
if (React.isValidElement(child)) {
/* eslint-disable eqeqeq */
if (activeKey == undefined && !child.props.disabled) {
activeKey = child.key || index;
}
}
});
}
Expand All @@ -145,11 +147,13 @@ export default class Tab extends Component {
getNextActiveKey(isNext) {
const children = [];
React.Children.forEach(this.props.children, child => {
if (!child.props.disabled) {
if (isNext) {
children.push(child);
} else {
children.unshift(child);
if (React.isValidElement(child)) {
if (!child.props.disabled) {
if (isNext) {
children.push(child);
} else {
children.unshift(child);
}
}
}
});
Expand Down
18 changes: 9 additions & 9 deletions test/tab/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ describe('Tab', () => {

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

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

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

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

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

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

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

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

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

Expand Down Expand Up @@ -162,7 +162,7 @@ describe('Tab', () => {
wrapper = mount(<Tab onClose={key => tabKey = key}>
<Tab.Item title="foo" />
<Tab.Item title="bar" closeable />
</Tab>)
</Tab>);
const closeIcon = wrapper.find('.next-icon-close');
assert(closeIcon.length === 1);
closeIcon.simulate('click');
Expand Down Expand Up @@ -222,7 +222,7 @@ describe('Tab', () => {
it('should scrollToActiveTab', () => {
wrapper = mount(<div style={boxStyle}><Tab activeKey="9">{panes}</Tab></div>, { attachTo: target });
// console.log(wrapper.find('.next-tabs').instance());
wrapper.setProps({ activeKey: "3" });
wrapper.setProps({ activeKey: '3' });
// wrapper.find('.next-tabs-tab').at(1).simulate('click');

});
Expand Down

0 comments on commit fb20367

Please sign in to comment.