UNPKG

sc-react-ions

Version:

An open source set of React components that implement Ambassador's Design and UX patterns.

167 lines (128 loc) 5.87 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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 _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _OptClass = require('../internal/OptClass'); var _OptClass2 = _interopRequireDefault(_OptClass); var _style = require('./style.scss'); var _style2 = _interopRequireDefault(_style); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } 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; } var PanelSlider = function (_React$Component) { _inherits(PanelSlider, _React$Component); function PanelSlider(props) { _classCallCheck(this, PanelSlider); var _this = _possibleConstructorReturn(this, (PanelSlider.__proto__ || Object.getPrototypeOf(PanelSlider)).call(this, props)); _this.state = { panels: [], isTransitioning: false }; _this.componentWillMount = function () { _this.activatePanel(_this.props.activePanel); }; _this.componentWillReceiveProps = function (nextProps) { if (nextProps.activePanel !== _this.props.activePanel) { _this.setState({ isTransitioning: true }); _this.activatePanel(nextProps.activePanel); setTimeout(function () { _this.setState({ isTransitioning: false }); }, 500); } }; _this.getStyle = function (index) { var translateValue = void 0; if (index === 0) { translateValue = 0; } else { translateValue = '-' + index + '00'; } return { 'transform': 'translateX(' + translateValue + '%)' }; }; _this.getPanels = function () { var panels = []; _react2.default.Children.forEach(_this.props.children, function (panel) { if (_react2.default.isValidElement(panel)) { panels.push(panel); } }); return panels; }; _this.activatePanel = function (activePanel) { var panels = _this.getPanels(); var initialPanels = []; panels.forEach(function (panel, index) { var active = activePanel === index; initialPanels = [].concat(_toConsumableArray(initialPanels), [{ active: active }]); }); _this.setState({ panels: initialPanels }); }; return _this; } // No sCU on this component because // nested child components need to be // able to update as needed _createClass(PanelSlider, [{ key: 'render', value: function render() { var _this2 = this; var isTransitioningClass = this.state.isTransitioning ? _style2.default['is-transitioning'] : null; var panelSliderClasses = (0, _OptClass2.default)(_style2.default, 'panel-slider', [this.props.optClass, isTransitioningClass]); var panelWrapClasses = (0, _OptClass2.default)(_style2.default, 'panel-wrap'); var panels = this.getPanels().map(function (panel, index) { return _react2.default.cloneElement(panel, { key: index, panelIndex: index, active: _this2.state.panels[index].active, name: _this2.props.name }); }); return _react2.default.createElement( 'div', { className: panelSliderClasses }, _react2.default.createElement( 'div', { className: _style2.default['wrapper'] }, _react2.default.createElement( 'div', { className: _style2.default['inner'] }, _react2.default.createElement( 'div', { className: panelWrapClasses, style: this.getStyle(this.props.activePanel) }, panels ) ) ) ); } }]); return PanelSlider; }(_react2.default.Component); PanelSlider.propTypes = { /** * The currently active panel */ activePanel: _propTypes2.default.number, /** * Optional CSS class(es) to be used for local styles (string or array of strings) */ optClass: _propTypes2.default.oneOfType([_propTypes2.default.array, _propTypes2.default.string]) }; PanelSlider.defaultProps = { activePanel: 0, name: 'PanelSlider' }; exports.default = PanelSlider;