-
-
Notifications
You must be signed in to change notification settings - Fork 450
Allow other components inside TabList #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
800e3a9
af03fd9
70c5b35
d76292a
f559020
4c6dbd6
b1e4545
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,21 @@ import React, { PropTypes } from 'react'; | |
| import cx from 'classnames'; | ||
|
|
||
| function renderChildren(props) { | ||
| return React.Children.map(props.children, (child) => | ||
| React.cloneElement(child, { | ||
| return React.Children.map(props.children, (child) => { | ||
| // if child is not a tab we don't need to clone it | ||
| // since we don't need to add custom props | ||
|
|
||
| if (child.type.displayName !== 'Tab') { | ||
| return child; | ||
| } | ||
|
|
||
| const clonedProps = { | ||
| activeTabClassName: props.activeTabClassName, | ||
| disabledTabClassName: props.disabledTabClassName, | ||
| }) | ||
| ); | ||
| }; | ||
|
|
||
| return React.cloneElement(child, clonedProps); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would still clone everytime. if (child.type.displayName === 'Tab') {
return React.cloneElement(child, {
activeTabClassName: props.activeTabClassName,
disabledTabClassName: props.disabledTabClassName,
});
}
return child; |
||
| }); | ||
| } | ||
|
|
||
| module.exports = React.createClass({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -209,13 +209,17 @@ module.exports = React.createClass({ | |
|
|
||
| index++; | ||
|
|
||
| return cloneElement(tab, { | ||
| ref, | ||
| id, | ||
| panelId, | ||
| selected, | ||
| focus, | ||
| }); | ||
| if (tab.type.displayName === 'Tab') { | ||
|
||
| return cloneElement(tab, { | ||
| ref, | ||
| id, | ||
| panelId, | ||
| selected, | ||
| focus, | ||
| }); | ||
| } | ||
|
|
||
| return tab; | ||
| }), | ||
| }); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -185,6 +185,27 @@ describe('react-tabs', () => { | |
| expect(wrapper.childAt(2).text()).toBe('Hello Bar'); | ||
| expect(wrapper.childAt(3).text()).toBe('Hello Baz'); | ||
| }); | ||
|
|
||
| it('should not clone non tabs element', () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice to figure out how to test this 👍 |
||
| class Demo extends React.Component { | ||
| render() { | ||
| const plus = <div ref="yolo">+</div>; | ||
|
|
||
| return (<Tabs> | ||
| <TabList> | ||
| <Tab>Foo</Tab> | ||
| {plus} | ||
| </TabList> | ||
|
|
||
| <TabPanel>Hello Baz</TabPanel> | ||
| </Tabs>); | ||
| } | ||
| } | ||
|
|
||
| const wrapper = mount(<Demo />); | ||
|
|
||
| expect(wrapper.ref('yolo').text()).toBe('+'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('validation', () => { | ||
|
|
@@ -201,7 +222,25 @@ describe('react-tabs', () => { | |
| expect(result instanceof Error).toBe(true); | ||
| }); | ||
|
|
||
| it('should result with a warning when wrong element is found', () => { | ||
| it(`should result with warning when tabs/panels are imbalanced and | ||
| it should ignore non tab children`, () => { | ||
| const wrapper = shallow( | ||
| <Tabs> | ||
| <TabList> | ||
| <Tab>Foo</Tab> | ||
| <div>+</div> | ||
| </TabList> | ||
|
|
||
| <TabPanel>Hello Foo</TabPanel> | ||
| <TabPanel>Hello Bar</TabPanel> | ||
| </Tabs> | ||
| ); | ||
|
|
||
| const result = Tabs.propTypes.children(wrapper.props(), 'children', 'Tabs'); | ||
| expect(result instanceof Error).toBe(true); | ||
| }); | ||
|
|
||
| it('should not throw a warning when wrong element is found', () => { | ||
| const wrapper = shallow( | ||
| <Tabs> | ||
| <TabList> | ||
|
|
@@ -213,7 +252,7 @@ describe('react-tabs', () => { | |
| ); | ||
|
|
||
| const result = Tabs.propTypes.children(wrapper.props(), 'children', 'Tabs'); | ||
| expect(result instanceof Error).toBe(true); | ||
| expect(result instanceof Error).toBe(false); | ||
| }); | ||
|
|
||
| it('should be okay with rendering without any children', () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there a reason to use the displayName. If not I think it would be better to do
child.type !== Typeandimport Tab from './Tab'at the top.