material-ui
Version:
React Components that Implement Google's Material Design.
326 lines (264 loc) • 10.8 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _simpleAssign = require('simple-assign');
var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _warning = require('warning');
var _warning2 = _interopRequireDefault(_warning);
var _TabTemplate = require('./TabTemplate');
var _TabTemplate2 = _interopRequireDefault(_TabTemplate);
var _InkBar = require('./InkBar');
var _InkBar2 = _interopRequireDefault(_InkBar);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function getStyles(props, context) {
var tabs = context.muiTheme.tabs;
return {
tabItemContainer: {
width: '100%',
backgroundColor: tabs.backgroundColor,
whiteSpace: 'nowrap',
display: 'flex'
}
};
}
var Tabs = function (_Component) {
(0, _inherits3.default)(Tabs, _Component);
function Tabs() {
var _ref;
var _temp, _this, _ret;
(0, _classCallCheck3.default)(this, Tabs);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Tabs.__proto__ || (0, _getPrototypeOf2.default)(Tabs)).call.apply(_ref, [this].concat(args))), _this), _this.state = { selectedIndex: 0 }, _this.handleTabClick = function (value, event, tab) {
var valueLink = _this.getValueLink(_this.props);
var index = tab.props.index;
if (valueLink.value && valueLink.value !== value || _this.state.selectedIndex !== index) {
valueLink.requestChange(value, event, tab);
}
_this.setState({ selectedIndex: index });
if (tab.props.onActive) {
tab.props.onActive(tab);
}
}, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
}
(0, _createClass3.default)(Tabs, [{
key: 'componentWillMount',
value: function componentWillMount() {
var valueLink = this.getValueLink(this.props);
var initialIndex = this.props.initialSelectedIndex;
this.setState({
selectedIndex: valueLink.value !== undefined ? this.getSelectedIndex(this.props) : initialIndex < this.getTabCount() ? initialIndex : 0
});
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(newProps, nextContext) {
var valueLink = this.getValueLink(newProps);
var newState = {
muiTheme: nextContext.muiTheme || this.context.muiTheme
};
if (valueLink.value !== undefined) {
newState.selectedIndex = this.getSelectedIndex(newProps);
}
this.setState(newState);
}
}, {
key: 'getTabs',
value: function getTabs() {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props;
var tabs = [];
_react.Children.forEach(props.children, function (tab) {
if ((0, _react.isValidElement)(tab)) {
tabs.push(tab);
}
});
return tabs;
}
}, {
key: 'getTabCount',
value: function getTabCount() {
return this.getTabs().length;
}
// Do not use outside of this component, it will be removed once valueLink is deprecated
}, {
key: 'getValueLink',
value: function getValueLink(props) {
return props.valueLink || {
value: props.value,
requestChange: props.onChange
};
}
}, {
key: 'getSelectedIndex',
value: function getSelectedIndex(props) {
var valueLink = this.getValueLink(props);
var selectedIndex = -1;
this.getTabs(props).forEach(function (tab, index) {
if (valueLink.value === tab.props.value) {
selectedIndex = index;
}
});
return selectedIndex;
}
}, {
key: 'getSelected',
value: function getSelected(tab, index) {
var valueLink = this.getValueLink(this.props);
return valueLink.value ? valueLink.value === tab.props.value : this.state.selectedIndex === index;
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var _props = this.props,
contentContainerClassName = _props.contentContainerClassName,
contentContainerStyle = _props.contentContainerStyle,
initialSelectedIndex = _props.initialSelectedIndex,
inkBarStyle = _props.inkBarStyle,
inkBarContainerStyle = _props.inkBarContainerStyle,
onChange = _props.onChange,
style = _props.style,
tabItemContainerStyle = _props.tabItemContainerStyle,
tabTemplate = _props.tabTemplate,
tabTemplateStyle = _props.tabTemplateStyle,
other = (0, _objectWithoutProperties3.default)(_props, ['contentContainerClassName', 'contentContainerStyle', 'initialSelectedIndex', 'inkBarStyle', 'inkBarContainerStyle', 'onChange', 'style', 'tabItemContainerStyle', 'tabTemplate', 'tabTemplateStyle']);
var prepareStyles = this.context.muiTheme.prepareStyles;
var styles = getStyles(this.props, this.context);
var valueLink = this.getValueLink(this.props);
var tabValue = valueLink.value;
var tabContent = [];
var width = 100 / this.getTabCount();
var tabs = this.getTabs().map(function (tab, index) {
process.env.NODE_ENV !== "production" ? (0, _warning2.default)(tab.type && tab.type.muiName === 'Tab', 'Material-UI: Tabs only accepts Tab Components as children.\n Found ' + (tab.type.muiName || tab.type) + ' as child number ' + (index + 1) + ' of Tabs') : void 0;
process.env.NODE_ENV !== "production" ? (0, _warning2.default)(!tabValue || tab.props.value !== undefined, 'Material-UI: Tabs value prop has been passed, but Tab ' + index + '\n does not have a value prop. Needs value if Tabs is going\n to be a controlled component.') : void 0;
tabContent.push(tab.props.children ? (0, _react.createElement)(tabTemplate || _TabTemplate2.default, {
key: index,
selected: _this2.getSelected(tab, index),
style: tabTemplateStyle
}, tab.props.children) : undefined);
return (0, _react.cloneElement)(tab, {
key: index,
index: index,
selected: _this2.getSelected(tab, index),
width: width + '%',
onClick: _this2.handleTabClick
});
});
var realSelectedIndex = valueLink.value ? this.getSelectedIndex(this.props) : this.state.selectedIndex;
var inkBar = realSelectedIndex !== -1 ? _react2.default.createElement(_InkBar2.default, {
left: width * realSelectedIndex + '%',
width: width + '%',
style: inkBarStyle
}) : null;
var inkBarContainerWidth = tabItemContainerStyle ? tabItemContainerStyle.width : '100%';
return _react2.default.createElement(
'div',
(0, _extends3.default)({ style: prepareStyles((0, _simpleAssign2.default)({}, style)) }, other),
_react2.default.createElement(
'div',
{ style: prepareStyles((0, _simpleAssign2.default)(styles.tabItemContainer, tabItemContainerStyle)) },
tabs
),
_react2.default.createElement(
'div',
{ style: prepareStyles((0, _simpleAssign2.default)({ width: inkBarContainerWidth }, inkBarContainerStyle)) },
inkBar
),
_react2.default.createElement(
'div',
{
style: prepareStyles((0, _simpleAssign2.default)({}, contentContainerStyle)),
className: contentContainerClassName
},
tabContent
)
);
}
}]);
return Tabs;
}(_react.Component);
Tabs.defaultProps = {
initialSelectedIndex: 0,
onChange: function onChange() {}
};
Tabs.contextTypes = {
muiTheme: _propTypes2.default.object.isRequired
};
Tabs.propTypes = process.env.NODE_ENV !== "production" ? {
/**
* Should be used to pass `Tab` components.
*/
children: _propTypes2.default.node,
/**
* The css class name of the root element.
*/
className: _propTypes2.default.string,
/**
* The css class name of the content's container.
*/
contentContainerClassName: _propTypes2.default.string,
/**
* Override the inline-styles of the content's container.
*/
contentContainerStyle: _propTypes2.default.object,
/**
* Specify initial visible tab index.
* If `initialSelectedIndex` is set but larger than the total amount of specified tabs,
* `initialSelectedIndex` will revert back to default.
* If `initialSelectedIndex` is set to any negative value, no tab will be selected intially.
*/
initialSelectedIndex: _propTypes2.default.number,
/**
* Override the inline-styles of the InkBar.
*/
inkBarStyle: _propTypes2.default.object,
/**
* Override the inline-styles of the InkBar container.
*/
inkBarContainerStyle: _propTypes2.default.object,
/**
* Called when the selected value change.
*/
onChange: _propTypes2.default.func,
/**
* Override the inline-styles of the root element.
*/
style: _propTypes2.default.object,
/**
* Override the inline-styles of the tab-labels container.
*/
tabItemContainerStyle: _propTypes2.default.object,
/**
* Override the default tab template used to wrap the content of each tab element.
*/
tabTemplate: _propTypes2.default.func,
/**
* Override the inline-styles of the tab template.
*/
tabTemplateStyle: _propTypes2.default.object,
/**
* Makes Tabs controllable and selects the tab whose value prop matches this prop.
*/
value: _propTypes2.default.any
} : {};
exports.default = Tabs;