react-toolbox/components/tabs/Tabs.js

167 lines
4.6 KiB
JavaScript
Raw Normal View History

2016-05-31 00:23:55 +03:00
import React, { Component, PropTypes } from 'react';
2016-05-26 22:01:54 +03:00
import classnames from 'classnames';
import { themr } from 'react-css-themr';
2016-05-31 00:23:55 +03:00
import { TABS } from '../identifiers.js';
import InjectTab from './Tab.js';
import InjectTabContent from './TabContent.js';
const factory = (Tab, TabContent) => {
class Tabs extends Component {
static propTypes = {
children: PropTypes.node,
className: PropTypes.string,
disableAnimatedBottomBorder: PropTypes.bool,
fixed: PropTypes.bool,
hideMode: PropTypes.oneOf(['display', 'unmounted']),
2016-05-31 00:23:55 +03:00
index: PropTypes.number,
inverse: PropTypes.bool,
2016-05-31 00:23:55 +03:00
onChange: PropTypes.func,
theme: PropTypes.shape({
2016-08-12 06:31:08 +03:00
fixed: PropTypes.string,
inverse: PropTypes.string,
2016-05-31 00:23:55 +03:00
navigation: PropTypes.string,
pointer: PropTypes.string,
tabs: PropTypes.string
})
};
static defaultProps = {
index: 0,
fixed: false,
inverse: false,
hideMode: 'unmounted'
2016-05-31 00:23:55 +03:00
};
state = {
pointer: {}
};
componentDidMount () {
!this.props.disableAnimatedBottomBorder && this.updatePointer(this.props.index);
window.addEventListener('resize', this.handleResize);
this.handleResize();
2016-05-31 00:23:55 +03:00
}
2016-05-31 00:23:55 +03:00
componentWillReceiveProps (nextProps) {
!this.props.disableAnimatedBottomBorder && this.updatePointer(nextProps.index);
}
2015-09-19 19:48:09 +03:00
2016-05-31 00:23:55 +03:00
componentWillUnmount () {
window.removeEventListener('resize', this.handleResize);
clearTimeout(this.resizeTimeout);
2016-05-31 00:23:55 +03:00
clearTimeout(this.pointerTimeout);
}
2015-11-13 03:01:27 +03:00
handleHeaderClick = (event) => {
const idx = parseInt(event.currentTarget.id);
2016-05-31 00:23:55 +03:00
if (this.props.onChange) this.props.onChange(idx);
};
2016-08-12 06:31:08 +03:00
handleResize = () => {
if (this.resizeTimeout) {
clearTimeout(this.resizeTimeout);
}
this.resizeTimeout = setTimeout(this.handleResizeEnd, 50);
};
handleResizeEnd = () => {
this.updatePointer(this.props.index);
};
2016-05-31 00:23:55 +03:00
parseChildren () {
const headers = [];
const contents = [];
React.Children.forEach(this.props.children, (item) => {
if (item.type === Tab) {
headers.push(item);
if (item.props.children) {
contents.push(<TabContent children={item.props.children} theme={this.props.theme} />);
2016-05-31 00:23:55 +03:00
}
} else if (item.type === TabContent) {
contents.push(item);
2015-11-13 03:01:27 +03:00
}
2016-05-31 00:23:55 +03:00
});
2015-10-11 23:27:59 +03:00
2016-05-31 00:23:55 +03:00
return {headers, contents};
}
2015-10-11 23:27:59 +03:00
2016-05-31 00:23:55 +03:00
updatePointer (idx) {
clearTimeout(this.pointerTimeout);
this.pointerTimeout = setTimeout(() => {
const startPoint = this.refs.tabs.getBoundingClientRect().left;
const label = this.refs.navigation.children[idx].getBoundingClientRect();
this.setState({
pointer: {
top: `${this.refs.navigation.getBoundingClientRect().height}px`,
left: `${label.left - startPoint}px`,
width: `${label.width}px`
}
});
}, 20);
}
renderHeaders (headers) {
return headers.map((item, idx) => {
return React.cloneElement(item, {
id: idx,
2016-05-31 00:23:55 +03:00
key: idx,
theme: this.props.theme,
2016-05-31 00:23:55 +03:00
active: this.props.index === idx,
2016-08-22 23:32:15 +03:00
onClick: event => {
this.handleHeaderClick(event);
item.props.onClick && item.props.onClick(event);
}
2016-05-31 00:23:55 +03:00
});
2016-03-05 23:53:52 +03:00
});
2016-05-31 00:23:55 +03:00
}
2015-09-19 19:48:09 +03:00
2016-05-31 00:23:55 +03:00
renderContents (contents) {
const contentElements = contents.map((item, idx) => {
return React.cloneElement(item, {
key: idx,
theme: this.props.theme,
active: this.props.index === idx,
hidden: this.props.index !== idx && this.props.hideMode === 'display',
tabIndex: idx
2016-05-31 00:23:55 +03:00
});
});
if (this.props.hideMode === 'display') {
return contentElements;
2016-05-31 00:23:55 +03:00
}
return contentElements.filter((item, idx) => (idx === this.props.index));
2016-05-31 00:23:55 +03:00
}
2016-05-31 00:23:55 +03:00
render () {
const { className, theme, fixed, inverse } = this.props;
2016-05-31 00:23:55 +03:00
const { headers, contents } = this.parseChildren();
const classes = classnames(
theme.tabs,
className,
{
[theme.fixed]: fixed,
[theme.inverse]: inverse
}
);
2016-05-31 00:23:55 +03:00
return (
<div ref='tabs' data-react-toolbox='tabs' className={classes}>
2016-05-31 00:23:55 +03:00
<nav className={theme.navigation} ref='navigation'>
{this.renderHeaders(headers)}
</nav>
<span className={theme.pointer} style={this.state.pointer} />
{this.renderContents(contents)}
</div>
);
2016-04-10 20:08:21 +03:00
}
2015-11-13 03:01:27 +03:00
}
2015-09-19 19:48:09 +03:00
2016-05-31 00:23:55 +03:00
return Tabs;
};
2015-10-22 02:31:17 +03:00
2016-05-31 00:23:55 +03:00
const Tabs = factory(InjectTab, InjectTabContent);
export default themr(TABS)(Tabs);
export { factory as tabsFactory };
export { Tabs };