UNPKG

zrmc

Version:

ZRMC is an ES7 React wrapper for Material Components Web.

218 lines (183 loc) 6.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = undefined; var _objectWithoutProperties2 = require("babel-runtime/helpers/objectWithoutProperties"); var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2); 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 _react = require("react"); var _react2 = _interopRequireDefault(_react); var _propTypes = require("prop-types"); var _propTypes2 = _interopRequireDefault(_propTypes); var _ = require("../"); var _2 = _interopRequireDefault(_); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * mdc-tab-bar * See: * https://material.io/components/web/catalog/tabs/ * http://material-components-web.appspot.com/tabs.html * */ var MDC_TABBAR = "mdc-tab-bar"; /** * Copyright (c) 2015-present, CWB SAS * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var Tabbar = function (_Component) { (0, _inherits3.default)(Tabbar, _Component); function Tabbar(props) { (0, _classCallCheck3.default)(this, Tabbar); var _this = (0, _possibleConstructorReturn3.default)(this, (Tabbar.__proto__ || Object.getPrototypeOf(Tabbar)).call(this, props)); _this.handleTabSelect = function (text, tabId) { if (_this.state.activeTab !== tabId) { if (_this.props.onChange) { _this.props.onChange(text, tabId); } _this.setState({ activeTab: tabId }); } }; var activeTab = props.activeTab; _this.state = { activeTab: activeTab }; _this.ref = null; _this.activeRef = null; _this.indicatorRef = null; return _this; } (0, _createClass3.default)(Tabbar, [{ key: "componentDidMount", value: function componentDidMount() { this.updateIndicator(); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { this.updateIndicator(); } }, { key: "updateIndicator", value: function updateIndicator() { if (this.activeRef && this.activeRef.ref && this.ref && this.indicatorRef) { var r = this.ref.getBoundingClientRect(); var rect = this.activeRef.ref.getBoundingClientRect(); var width = this.ref.clientWidth; var tabWidth = rect.width; var tabLeft = rect.left - r.left; var scaledWidth = tabWidth / width; var style = "transform: translateX(" + tabLeft + "px) scale(" + scaledWidth + ", 1); visibility: visible;"; if (this.props.activeColor) { style += " background-color:" + this.props.activeColor; } this.indicatorRef.style = style; } } }, { key: "render", value: function render() { var _this2 = this; var _props = this.props, children = _props.children, onChange = _props.onChange, color = _props.color, activeColor = _props.activeColor, ripple = _props.ripple, props = (0, _objectWithoutProperties3.default)(_props, ["children", "onChange", "color", "activeColor", "ripple"]); var classes = MDC_TABBAR; var text = false; var icon = false; var activeTab = this.state.activeTab; // Check icon / text from children Tabs _react.Children.forEach(children, function (child) { if (child.props && child.props.text) { text = true; } if (child.props && child.props.icon) { icon = true; } }); if (text && icon) { classes += " mdc-tab--with-icon-and-text"; } else if (icon) { classes += " mdc-tab-bar--icon-tab-bar"; } // let style; /* if (activeColor) { style = { backgroundColor: activeColor }; } */ var element = _react2.default.createElement( "div", { className: classes, ref: function ref(c) { _this2.ref = c; } }, _react2.default.createElement( "div", { className: "mdc-tab-scroller" }, _react2.default.createElement( "div", { className: "mdc-tab-scroller__scroll-area mdc-tab-scroller__scroll-area--scroll" }, _react2.default.createElement( "div", { className: "mdc-tab-scroller__scroll-content" }, _react.Children.map(children, function (child, tabId) { return _react2.default.cloneElement(child, { tabId: tabId, active: tabId === activeTab, ripple: ripple, color: tabId === activeTab ? activeColor : color, ref: function ref(c) { if (tabId === activeTab) { _this2.activeRef = c; } }, onTabSelect: _this2.handleTabSelect }); }) ) ) ) ); return _2.default.render(element, props); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(nextProps, prevState) { var activeTab = nextProps.activeTab; if (activeTab !== prevState.activeTab && !nextProps.derivedState) { return { activeTab: activeTab }; } return null; } }]); return Tabbar; }(_react.Component); exports.default = Tabbar; Tabbar.defaultProps = { mdcElement: MDC_TABBAR, children: null, activeTab: 0, onChange: null, color: null, activeColor: null, ripple: false }; Tabbar.propTypes = { mdcElement: _propTypes2.default.string, children: _propTypes2.default.node, activeTab: _propTypes2.default.number, onChange: _propTypes2.default.func, color: _propTypes2.default.string, activeColor: _propTypes2.default.string, ripple: _propTypes2.default.bool };