-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #804 from Adslot/re-apply-tab-implementation
Re-apply the implementation of tab&tabs
- Loading branch information
Showing
7 changed files
with
263 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* eslint-disable react/no-unused-prop-types */ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classnames from 'classnames'; | ||
|
||
const Tab = ({ children, show }) => ( | ||
<div role="tabpanel" aria-hidden={show} className={classnames(['tab-pane', 'fade', { active: show, in: show }])}> | ||
{children} | ||
</div> | ||
); | ||
|
||
Tab.innerName = 'au_tab'; | ||
|
||
Tab.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
eventKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired, | ||
title: PropTypes.node.isRequired, | ||
tabClassName: PropTypes.string, | ||
disabled: PropTypes.bool, | ||
show: PropTypes.bool, | ||
}; | ||
|
||
export default Tab; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import Tab from '.'; | ||
|
||
describe('<Tab />', () => { | ||
it('should render with props', () => { | ||
expect( | ||
shallow( | ||
<Tab eventKey="first" show={false} title="First"> | ||
hi | ||
</Tab> | ||
).props().className | ||
).to.equal('tab-pane fade'); | ||
expect( | ||
shallow( | ||
<Tab eventKey="first" show title="First"> | ||
hi | ||
</Tab> | ||
).props().className | ||
).to.equal('tab-pane fade active in'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* eslint-disable jsx-a11y/anchor-is-valid */ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import _ from 'lodash'; | ||
import classnames from 'classnames'; | ||
import Tab from '../Tab'; | ||
|
||
class Tabs extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
activeKey: this.props.defaultActiveKey, | ||
}; | ||
|
||
this.switchTab = this.switchTab.bind(this); | ||
} | ||
|
||
get isControlled() { | ||
const { activeKey, onSelect } = this.props; | ||
|
||
return !_.isNil(activeKey) && _.isFunction(onSelect); | ||
} | ||
|
||
get activeKey() { | ||
return this.isControlled ? this.props.activeKey : this.state.activeKey; | ||
} | ||
|
||
switchTab(key) { | ||
const { onSelect, activeKey } = this.props; | ||
return event => { | ||
event.preventDefault(); | ||
|
||
if (this.isControlled && key !== activeKey) { | ||
onSelect(key); | ||
} else if (key !== this.state.activeKey) { | ||
this.setState({ activeKey: key }); | ||
} | ||
}; | ||
} | ||
|
||
render() { | ||
const { id, children } = this.props; | ||
|
||
const tabs = []; | ||
const content = React.Children.map(children, child => { | ||
if (_.isFunction(child.type) && child.type.innerName === Tab.innerName) { | ||
const tab = React.cloneElement(child, { | ||
show: this.activeKey === child.props.eventKey, | ||
}); | ||
tabs.push(tab); | ||
|
||
return tab; | ||
} | ||
return child; | ||
}); | ||
|
||
return ( | ||
<div id={id}> | ||
<ul role="tablist" className="nav nav-tabs"> | ||
{tabs.map(tab => ( | ||
<li | ||
role="presentation" | ||
className={classnames(tab.props.tabClassName, { active: tab.props.show, disabled: tab.props.disabled })} | ||
key={tab.props.eventKey} | ||
> | ||
<a | ||
id={`${id}-tab-${tab.props.eventKey}`} | ||
role="tab" | ||
tabIndex={-1} | ||
aria-selected={tab.props.show} | ||
onClick={this.switchTab(tab.props.eventKey)} | ||
style={tab.props.disabled ? { pointerEvents: 'none' } : {}} | ||
> | ||
{tab.props.title} | ||
</a> | ||
</li> | ||
))} | ||
</ul> | ||
<div className="tab-content">{content}</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
Tabs.propTypes = { | ||
id: PropTypes.string.isRequired, | ||
children: PropTypes.node.isRequired, | ||
defaultActiveKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
onSelect: PropTypes.func, | ||
}; | ||
|
||
export default Tabs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import React from 'react'; | ||
import _ from 'lodash'; | ||
import { shallow } from 'enzyme'; | ||
import sinon from 'sinon'; | ||
import Tabs from '.'; | ||
import Tab from '../Tab'; | ||
|
||
describe('<Tabs />', () => { | ||
it('should render with props', () => { | ||
const wrapper = shallow( | ||
<Tabs defaultActiveKey="first" id="test"> | ||
<Tab eventKey="first" title="Fist"> | ||
Tab1 | ||
</Tab> | ||
<Tab eventKey="last" title="Second" disabled> | ||
Tab2 | ||
</Tab> | ||
</Tabs> | ||
); | ||
const links = wrapper.find('a'); | ||
let content = wrapper.find(Tab); | ||
expect(links.length).to.equal(2); | ||
expect(content.length).to.equal(2); | ||
|
||
expect(links.last().props().style).to.eql({ pointerEvents: 'none' }); | ||
|
||
expect(content.first().props().show).to.equal(true); | ||
expect(content.last().props().show).to.equal(false); | ||
|
||
links.last().simulate('click', { preventDefault: _.noop }); | ||
content = wrapper.find(Tab); | ||
expect(content.first().props().show).to.equal(false); | ||
expect(content.last().props().show).to.equal(true); | ||
}); | ||
|
||
it('should work as controlled', () => { | ||
const selectSpy = sinon.spy(); | ||
const wrapper = shallow( | ||
<Tabs activeKey="first" onSelect={selectSpy} id="test"> | ||
<Tab eventKey="first" title="Fist"> | ||
Tab1 | ||
</Tab> | ||
<Tab eventKey="last" title="Second"> | ||
Tab2 | ||
</Tab> | ||
<div>other</div> | ||
</Tabs> | ||
); | ||
const links = wrapper.find('a'); | ||
expect(links.length).to.equal(2); | ||
|
||
links.last().simulate('click', { preventDefault: _.noop }); | ||
expect(selectSpy.callCount).to.equal(1); | ||
expect(selectSpy.calledWith('last')).to.equal(true); | ||
}); | ||
|
||
it('should not re render when key is the same', () => { | ||
const wrapper = shallow( | ||
<Tabs defaultActiveKey="first" id="test"> | ||
<Tab eventKey="first" title="Fist"> | ||
Tab1 | ||
</Tab> | ||
<Tab eventKey="last" title="Second"> | ||
Tab2 | ||
</Tab> | ||
</Tabs> | ||
); | ||
const spy = sinon.spy(wrapper.instance(), 'setState'); | ||
const links = wrapper.find('a'); | ||
links.last().simulate('click', { preventDefault: _.noop }); | ||
links.last().simulate('click', { preventDefault: _.noop }); | ||
expect(spy.callCount).to.equal(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters