UNPKG

rmwc

Version:

A thin React wrapper for Material Design (Web) Components

325 lines (271 loc) 11.9 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.TabBarScroller = exports.TabBar = exports.TabIconText = exports.TabIcon = exports.Tab = exports.TabBarScrollerScrollFrame = exports.TabBarScrollerIndicatorInner = exports.TabBarScrollerIndicator = exports.TabBarScrollerRoot = exports.TabBarIndicator = exports.TabBarRoot = undefined; 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 _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; var _react = require('react'); var React = _interopRequireWildcard(_react); var _mdc = require('@material/tabs/dist/mdc.tabs'); var _Icon = require('../Icon'); var _Base = require('../Base'); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } 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 recursiveMap(children, fn) { return React.Children.map(children, //$FlowFixMe function (child) { if (!React.isValidElement(child)) { return child; } if ('children' in child.props) { //$FlowFixMe child = React.cloneElement(child, { children: recursiveMap(child.props.children, fn) }); } return fn(child); }); } var TabBarRoot = exports.TabBarRoot = (0, _Base.simpleTag)({ displayName: 'TabBarRoot', tag: 'nav', classNames: function classNames(props) { return ['mdc-tab-bar', { 'mdc-tab-bar-scroller__scroll-frame__tabs': props.isTabScroller }]; }, consumeProps: ['isTabScroller'] }); var TabBarIndicator = exports.TabBarIndicator = (0, _Base.simpleTag)({ displayName: 'TabBarIndicator', tag: 'span', classNames: 'mdc-tab-bar__indicator' }); var TabBarScrollerRoot = exports.TabBarScrollerRoot = (0, _Base.simpleTag)({ displayName: 'TabBarScrollerRoot', tag: 'div', classNames: 'mdc-tab-bar-scroller' }); var TabBarScrollerIndicator = exports.TabBarScrollerIndicator = (0, _Base.simpleTag)({ displayName: 'TabBarScrollerIndicatorBack', tag: 'div', classNames: function classNames(props) { return ['mdc-tab-bar-scroller__indicator', { 'mdc-tab-bar-scroller__indicator--back': props.back, 'mdc-tab-bar-scroller__indicator--forward': props.forward }]; }, consumeProps: ['forward', 'back'] }); var TabBarScrollerIndicatorInner = exports.TabBarScrollerIndicatorInner = (0, _Base.simpleTag)({ displayName: 'TabBarScrollerIndicatorInner', tag: _Icon.Icon, classNames: 'mdc-tab-bar-scroller__indicator__inner' }); var TabBarScrollerScrollFrame = exports.TabBarScrollerScrollFrame = (0, _Base.simpleTag)({ displayName: 'TabBarScrollerScrollFrameEl', tag: 'div', classNames: 'mdc-tab-bar-scroller__scroll-frame' }); /****************************************************** * Public *******************************************************/ /** A Tab component */ var Tab = exports.Tab = (0, _Base.simpleTag)({ displayName: 'Tab', classNames: 'mdc-tab' }); /** A Tab icon. This is an instance of the Icon component. */ var TabIcon = exports.TabIcon = (0, _Base.simpleTag)({ displayName: 'TabIcon', tag: _Icon.Icon, classNames: 'mdc-tab__icon' }); /** Text that goes under a Tab icon */ var TabIconText = exports.TabIconText = (0, _Base.simpleTag)({ displayName: 'TabIconText', tag: 'span', classNames: 'mdc-tab__icon-text' }); /** The TabBar component */ var TabBar = exports.TabBar = function (_withFoundation) { _inherits(TabBar, _withFoundation); function TabBar() { _classCallCheck(this, TabBar); return _possibleConstructorReturn(this, (TabBar.__proto__ || Object.getPrototypeOf(TabBar)).apply(this, arguments)); } _createClass(TabBar, [{ key: 'syncWithProps', value: function syncWithProps(nextProps) { var _this2 = this; (0, _Base.syncFoundationProp)(nextProps.activeTabIndex, this.activeTabIndex, function () { return _this2.activeTabIndex = nextProps.activeTabIndex !== undefined ? nextProps.activeTabIndex : _this2.activeTabIndex; }); } }, { key: 'componentDidMount', value: function componentDidMount() { var _this3 = this; _get(TabBar.prototype.__proto__ || Object.getPrototypeOf(TabBar.prototype), 'componentDidMount', this).call(this); // This corrects an issue where passing in 0 or no activeTabIndex // causes the first tab of the set to not be active if (this.props.activeTabIndex === 0 || this.props.activeTabIndex === undefined) { window.requestAnimationFrame(function () { _this3.foundation_ && _this3.foundation_.adapter_.setTabActiveAtIndex(0, true); }); } } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps) { // Children changing is a pain... // We have to perform a lot of cleanup and sometimes we have to reinit var childrenDidChange = prevProps && prevProps.children && this.props && this.props.children && JSON.stringify(React.Children.map(prevProps.children, function (child) { return 'key' in child && child.key; })) !== JSON.stringify(React.Children.map(this.props.children, function (child) { return 'key' in child && child.key; })); var tabsLengthMismatch = React.Children.toArray(this.props.children).length !== this.tabs.length; if (childrenDidChange || tabsLengthMismatch) { this.tabs.forEach(function (mdcTab) { mdcTab.foundation_ && mdcTab.foundation_.destroy(); }); this.tabs_ = this.gatherTabs_(function (el) { return new _mdc.MDCTab(el); }); this.layout(); this.syncWithProps(this.props); } } }, { key: 'render', value: function render() { var _props = this.props, children = _props.children, activeTabIndex = _props.activeTabIndex, apiRef = _props.apiRef, rest = _objectWithoutProperties(_props, ['children', 'activeTabIndex', 'apiRef']); var root_ = this.foundationRefs.root_; return React.createElement( TabBarRoot, Object.assign({}, rest, { elementRef: root_ }), children, React.createElement(TabBarIndicator, null) ); } }]); return TabBar; }((0, _Base.withFoundation)({ constructor: _mdc.MDCTabBar, adapter: {} })); Object.defineProperty(TabBar, 'displayName', { enumerable: true, writable: true, value: 'TabBar' }); /** The TabBar Scroll container */ var TabBarScroller = exports.TabBarScroller = function (_withFoundation2) { _inherits(TabBarScroller, _withFoundation2); function TabBarScroller() { _classCallCheck(this, TabBarScroller); return _possibleConstructorReturn(this, (TabBarScroller.__proto__ || Object.getPrototypeOf(TabBarScroller)).apply(this, arguments)); } _createClass(TabBarScroller, [{ key: 'initialize', value: function initialize() { var _this5 = this; _get(TabBarScroller.prototype.__proto__ || Object.getPrototypeOf(TabBarScroller.prototype), 'initialize', this).call(this, function () { return _this5.tabBarApi; }); } }, { key: 'componentDidUpdate', value: function componentDidUpdate() { this.layout(); } }, { key: 'render', value: function render() { var _this6 = this; var _props2 = this.props, children = _props2.children, indicatorForward = _props2.indicatorForward, indicatorBack = _props2.indicatorBack, rest = _objectWithoutProperties(_props2, ['children', 'indicatorForward', 'indicatorBack']); var root_ = this.foundationRefs.root_; var newChildren = recursiveMap(children, function (child) { if (child.type.displayName && child.type.displayName === 'TabBar') { return React.cloneElement(child, Object.assign({}, child.props, { isTabScroller: true, ref: function ref(tabBarApi) { return _this6.tabBarApi = tabBarApi; } })); } return child; }); return React.createElement( TabBarScrollerRoot, Object.assign({}, rest, { elementRef: root_ }), React.createElement( TabBarScrollerIndicator, { back: true }, React.createElement(TabBarScrollerIndicatorInner, { use: indicatorBack }) ), React.createElement( TabBarScrollerScrollFrame, null, newChildren ), React.createElement( TabBarScrollerIndicator, { forward: true }, React.createElement(TabBarScrollerIndicatorInner, { use: indicatorForward }) ) ); } }]); return TabBarScroller; }((0, _Base.withFoundation)({ constructor: _mdc.MDCTabBarScroller, adapter: {} })); Object.defineProperty(TabBarScroller, 'displayName', { enumerable: true, writable: true, value: 'TabBarScroller' }); Object.defineProperty(TabBarScroller, 'defaultProps', { enumerable: true, writable: true, value: { indicatorForward: React.createElement( 'svg', { style: { fill: 'currentColor' }, height: '24', viewBox: '0 0 24 24', width: '24' }, React.createElement('path', { d: 'M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z' }), React.createElement('path', { d: 'M0 0h24v24H0z', fill: 'none' }) ), indicatorBack: React.createElement( 'svg', { style: { fill: 'currentColor' }, height: '24', viewBox: '0 0 24 24', width: '24' }, React.createElement('path', { d: 'M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z' }), React.createElement('path', { d: 'M0 0h24v24H0z', fill: 'none' }) ) } });