UNPKG

d2-ui

Version:
302 lines (249 loc) 11.1 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _simpleAssign = require('simple-assign'); var _simpleAssign2 = _interopRequireDefault(_simpleAssign); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactDom = require('react-dom'); var _reactDom2 = _interopRequireDefault(_reactDom); var _TabTemplate = require('./TabTemplate'); var _TabTemplate2 = _interopRequireDefault(_TabTemplate); var _InkBar = require('./InkBar'); var _InkBar2 = _interopRequireDefault(_InkBar); var _warning = require('warning'); var _warning2 = _interopRequireDefault(_warning); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function getStyles(props, context) { var tabs = context.muiTheme.tabs; return { tabItemContainer: { width: '100%', backgroundColor: tabs.backgroundColor, whiteSpace: 'nowrap' } }; } var Tabs = function (_Component) { _inherits(Tabs, _Component); function Tabs() { var _Object$getPrototypeO; var _temp, _this, _ret; _classCallCheck(this, Tabs); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(Tabs)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.state = { selectedIndex: 0 }, _this.handleTabTouchTap = function (value, event, tab) { var valueLink = _this.getValueLink(_this.props); var tabIndex = tab.props.tabIndex; if (valueLink.value && valueLink.value !== value || _this.state.selectedIndex !== tabIndex) { valueLink.requestChange(value, event, tab); } _this.setState({ selectedIndex: tabIndex }); if (tab.props.onActive) { tab.props.onActive(tab); } }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(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: 'getEvenWidth', value: function getEvenWidth() { return parseInt(window.getComputedStyle(_reactDom2.default.findDOMNode(this)).getPropertyValue('width'), 10); } }, { key: 'getTabs', value: function getTabs() { var tabs = []; _react2.default.Children.forEach(this.props.children, function (tab) { if (_react2.default.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().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; var contentContainerClassName = _props.contentContainerClassName; var contentContainerStyle = _props.contentContainerStyle; var initialSelectedIndex = _props.initialSelectedIndex; var // eslint-disable-line no-unused-vars inkBarStyle = _props.inkBarStyle; var style = _props.style; var tabItemContainerStyle = _props.tabItemContainerStyle; var tabTemplate = _props.tabTemplate; var other = _objectWithoutProperties(_props, ['contentContainerClassName', 'contentContainerStyle', 'initialSelectedIndex', 'inkBarStyle', 'style', 'tabItemContainerStyle', 'tabTemplate']); 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', '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, '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 ? _react2.default.createElement(tabTemplate || _TabTemplate2.default, { key: index, selected: _this2.getSelected(tab, index) }, tab.props.children) : undefined); return _react2.default.cloneElement(tab, { key: index, selected: _this2.getSelected(tab, index), tabIndex: index, width: width + '%', onTouchTap: _this2.handleTabTouchTap }); }); var inkBar = this.state.selectedIndex !== -1 ? _react2.default.createElement(_InkBar2.default, { left: width * this.state.selectedIndex + '%', width: width + '%', style: inkBarStyle }) : null; var inkBarContainerWidth = tabItemContainerStyle ? tabItemContainerStyle.width : '100%'; return _react2.default.createElement( 'div', _extends({}, other, { style: prepareStyles((0, _simpleAssign2.default)({}, style)) }), _react2.default.createElement( 'div', { style: prepareStyles((0, _simpleAssign2.default)(styles.tabItemContainer, tabItemContainerStyle)) }, tabs ), _react2.default.createElement( 'div', { style: { width: inkBarContainerWidth } }, inkBar ), _react2.default.createElement( 'div', { style: prepareStyles((0, _simpleAssign2.default)({}, contentContainerStyle)), className: contentContainerClassName }, tabContent ) ); } }]); return Tabs; }(_react.Component); Tabs.propTypes = { /** * Should be used to pass `Tab` components. */ children: _react.PropTypes.node, /** * The css class name of the root element. */ className: _react.PropTypes.string, /** * The css class name of the content's container. */ contentContainerClassName: _react.PropTypes.string, /** * Override the inline-styles of the content's container. */ contentContainerStyle: _react.PropTypes.object, /** * Specify initial visible tab index. * Initial selected index is set by default to 0. * If initialSelectedIndex is set but larger than the total amount of specified tabs, * initialSelectedIndex will revert back to default. */ initialSelectedIndex: _react.PropTypes.number, /** * Override the inline-styles of the InkBar. */ inkBarStyle: _react.PropTypes.object, /** * Called when the selected value change. */ onChange: _react.PropTypes.func, /** * Override the inline-styles of the root element. */ style: _react.PropTypes.object, /** * Override the inline-styles of the tab-labels container. */ tabItemContainerStyle: _react.PropTypes.object, /** * Override the default tab template used to wrap the content of each tab element. */ tabTemplate: _react.PropTypes.func, /** * Makes Tabs controllable and selects the tab whose value prop matches this prop. */ value: _react.PropTypes.any }; Tabs.defaultProps = { initialSelectedIndex: 0, onChange: function onChange() {} }; Tabs.contextTypes = { muiTheme: _react.PropTypes.object.isRequired }; exports.default = Tabs;