UNPKG

react-conventions

Version:

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

175 lines (137 loc) 6.3 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 _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 PanelGroup = function (_React$Component) { _inherits(PanelGroup, _React$Component); function PanelGroup(props) { _classCallCheck(this, PanelGroup); var _this = _possibleConstructorReturn(this, (PanelGroup.__proto__ || Object.getPrototypeOf(PanelGroup)).call(this, props)); _this.state = { panels: [] }; _this.componentWillMount = function () { _this.setInitialState(); }; _this.activatePanels = function (activePanels) { var panels = _this.getPanels(); var initialPanels = []; panels.forEach(function (panel, index) { if (activePanels) { initialPanels = [].concat(_toConsumableArray(initialPanels), [{ active: !!(activePanels.indexOf(index) !== -1) }]); } else { initialPanels = [].concat(_toConsumableArray(initialPanels), [{ active: false }]); } }); _this.setState({ panels: initialPanels }); }; _this.setInitialState = function () { _this.activatePanels(_this.props.activePanels); }; _this.componentWillReceiveProps = function (nextProps) { _this.activatePanels(nextProps.activePanels); }; _this.collapsePanels = function () { var panels = _this.getPanels(); var collapsedPanels = []; panels.forEach(function (panel, index) { collapsedPanels = [].concat(_toConsumableArray(collapsedPanels), [{ active: false }]); }); return collapsedPanels; }; _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.handlePanelClick = function (panel) { var panelIndex = panel.props.panelIndex; var state = _this.state.panels; if (!_this.props.accordion) { state[panelIndex] = { active: !_this.state.panels[panelIndex].active }; _this.setState({ panels: state }, function () { this.onPanelToggle(); }); } else { var resetState = _this.collapsePanels(); resetState[panelIndex] = { active: !_this.state.panels[panelIndex].active }; _this.setState({ panels: resetState }, function () { this.onPanelToggle(); }); } }; _this.onPanelToggle = function () { var activePanels = []; _this.state.panels.map(function (panel, index) { if (panel.active) { activePanels.push(index); } }); if (_this.props.onPanelToggle) { _this.props.onPanelToggle(activePanels); } }; return _this; } _createClass(PanelGroup, [{ key: 'render', value: function render() { var _this2 = this; var panelGroupClasses = (0, _OptClass2.default)(_style2.default, 'panel-group', this.props.optClass); var panels = this.getPanels().map(function (panel, index) { return _react2.default.cloneElement(panel, { key: index, panelIndex: index, active: _this2.state.panels[index].active, onPanelClick: _this2.handlePanelClick }); }); return _react2.default.createElement( 'div', { className: panelGroupClasses }, panels ); } }]); return PanelGroup; }(_react2.default.Component); PanelGroup.propTypes = { /** * The panel(s) to be open by default */ activePanels: _react2.default.PropTypes.array, /** * Whether the panelGroup should allow only one panel to be open at a time * Note: if accordion is set to true, the activePanels array will respect * only the first item. */ accordion: _react2.default.PropTypes.bool, /** * Optional CSS class(es) to be used for local styles (string or array of strings) */ optClass: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.array, _react2.default.PropTypes.string]), /** * A callback that gets triggered when a panel is toggled (when a panel header gets clicked) */ onPanelToggle: _react2.default.PropTypes.func }; PanelGroup.defaultProps = { activePanels: [] }; exports.default = PanelGroup;