react-toolbox/components/tabs/Tab.js

84 lines
2.4 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
2016-05-26 22:01:54 +03:00
import classnames from 'classnames';
import { themr } from 'react-css-themr';
2017-01-26 20:05:32 +03:00
import { TABS } from '../identifiers';
import rippleFactory from '../ripple/Ripple';
import InjectFontIcon from '../font_icon/FontIcon';
2015-09-19 19:48:09 +03:00
const factory = (ripple, FontIcon) => {
2017-01-21 15:03:38 +03:00
class Tab extends Component {
static propTypes = {
active: PropTypes.bool,
activeClassName: PropTypes.string,
children: PropTypes.node,
className: PropTypes.string,
disabled: PropTypes.bool,
hidden: PropTypes.bool,
icon: PropTypes.node,
index: PropTypes.number,
label: PropTypes.node,
onActive: PropTypes.func,
onClick: PropTypes.func,
theme: PropTypes.shape({
active: PropTypes.string,
disabled: PropTypes.string,
hidden: PropTypes.string,
label: PropTypes.string,
rippleWrapper: PropTypes.string,
withIcon: PropTypes.string,
2017-01-26 20:05:32 +03:00
withText: PropTypes.string,
}),
2017-01-21 15:03:38 +03:00
};
2015-09-19 19:48:09 +03:00
2017-01-21 15:03:38 +03:00
static defaultProps = {
active: false,
className: '',
disabled: false,
2017-01-26 20:05:32 +03:00
hidden: false,
2017-01-21 15:03:38 +03:00
};
2015-09-19 19:48:09 +03:00
2017-01-26 20:05:32 +03:00
componentDidUpdate(prevProps) {
2017-01-21 15:03:38 +03:00
if (!prevProps.active && this.props.active && this.props.onActive) {
this.props.onActive();
}
2015-11-13 17:01:30 +03:00
}
2017-01-21 15:03:38 +03:00
handleClick = (event) => {
if (!this.props.disabled && this.props.onClick) {
this.props.onClick(event, this.props.index);
}
};
2015-09-19 19:48:09 +03:00
2017-01-26 20:05:32 +03:00
render() {
2017-01-21 15:03:38 +03:00
const {
index, onActive, // eslint-disable-line
active, activeClassName, children, className, disabled, hidden, label, icon, theme, ...other
} = this.props;
const _className = classnames(theme.label, {
[theme.active]: active,
[theme.hidden]: hidden,
[theme.withText]: label,
[theme.withIcon]: icon,
[theme.disabled]: disabled,
2017-01-26 20:05:32 +03:00
[activeClassName]: active,
2017-01-21 15:03:38 +03:00
}, className);
2015-09-19 19:48:09 +03:00
2017-01-21 15:03:38 +03:00
return (
<div {...other} data-react-toolbox="tab" role="tab" tabIndex="0" className={_className} onClick={this.handleClick}>
2017-01-26 20:05:32 +03:00
{icon && <FontIcon className={theme.icon} value={icon} />}
2017-01-21 15:03:38 +03:00
{label}
{children}
2017-01-26 20:05:32 +03:00
</div>
2017-01-21 15:03:38 +03:00
);
}
}
2015-10-22 02:31:17 +03:00
2017-01-21 15:03:38 +03:00
return ripple(Tab);
};
const Tab = factory(rippleFactory({ centered: false }), InjectFontIcon);
2016-05-31 00:23:55 +03:00
export default themr(TABS)(Tab);
2017-01-21 15:03:38 +03:00
export { factory as tabFactory };
2016-05-31 00:23:55 +03:00
export { Tab };