UNPKG

@zilverenkruis/panel-group

Version:

The ZK Components for the panel-group component

194 lines (149 loc) 8.02 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); 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 _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _uncontrollable = require('uncontrollable'); var _uncontrollable2 = _interopRequireDefault(_uncontrollable); var _bootstrapUtils = require('@zilverenkruis/utils/dist/bootstrapUtils'); var _ValidComponentChildren = require('@zilverenkruis/utils/dist/ValidComponentChildren'); var _ValidComponentChildren2 = _interopRequireDefault(_ValidComponentChildren); var _PropTypes = require('@zilverenkruis/utils/dist/PropTypes'); 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; } var propTypes = { accordion: _propTypes2.default.bool, /** * When `accordion` is enabled, `activeKey` controls the which child `Panel` is expanded. `activeKey` should * match a child Panel `eventKey` prop exactly. * * @controllable onSelect */ activeKey: _propTypes2.default.any, /** * A callback fired when a child Panel collapse state changes. It's called with the next expanded `activeKey` * * @controllable activeKey */ onSelect: _propTypes2.default.func, /** * An HTML role attribute */ role: _propTypes2.default.string, /** * A function that takes an eventKey and type and returns a * unique id for each Panel heading and Panel Collapse. The function _must_ be a pure function, * meaning it should always return the _same_ id for the same set of inputs. The default * value requires that an `id` to be set for the PanelGroup. * * The `type` argument will either be `"body"` or `"heading"`. * * @defaultValue (eventKey, type) => `${this.props.id}-${type}-${key}` */ generateChildId: _propTypes2.default.func, /** * HTML id attribute, required if no `generateChildId` prop * is specified. */ id: (0, _PropTypes.generatedId)('PanelGroup') }; var defaultProps = { accordion: false }; var childContextTypes = { $bs_panelGroup: _propTypes2.default.shape({ getId: _propTypes2.default.func, headerRole: _propTypes2.default.string, panelRole: _propTypes2.default.string, activeKey: _propTypes2.default.any, onToggle: _propTypes2.default.func }) }; var PanelGroup = function (_React$Component) { _inherits(PanelGroup, _React$Component); function PanelGroup() { var _ref; var _temp, _this, _ret; _classCallCheck(this, PanelGroup); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = PanelGroup.__proto__ || Object.getPrototypeOf(PanelGroup)).call.apply(_ref, [this].concat(args))), _this), _this.handleSelect = function (key, expanded, e) { if (expanded) { _this.props.onSelect(key, e); } else if (_this.props.activeKey === key) { _this.props.onSelect(null, e); } }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(PanelGroup, [{ key: 'getChildContext', value: function getChildContext() { var _props = this.props, activeKey = _props.activeKey, accordion = _props.accordion, generateChildId = _props.generateChildId, id = _props.id; var getId = null; if (accordion) { getId = generateChildId || function (key, type) { return id ? id + '-' + type + '-' + key : null; }; } return { $bs_panelGroup: _extends({ getId: getId, headerRole: 'tab', panelRole: 'tabpanel' }, accordion && { activeKey: activeKey, onToggle: this.handleSelect }) }; } }, { key: 'render', value: function render() { var _props2 = this.props, accordion = _props2.accordion, className = _props2.className, children = _props2.children, props = _objectWithoutProperties(_props2, ['accordion', 'className', 'children']); var _splitBsPropsAndOmit = (0, _bootstrapUtils.splitBsPropsAndOmit)(props, ['onSelect', 'activeKey']), _splitBsPropsAndOmit2 = _slicedToArray(_splitBsPropsAndOmit, 2), bsProps = _splitBsPropsAndOmit2[0], elementProps = _splitBsPropsAndOmit2[1]; if (accordion) { elementProps.role = elementProps.role || 'tablist'; } var classes = (0, _bootstrapUtils.getClassSet)(bsProps); return _react2.default.createElement( 'div', _extends({}, elementProps, { className: (0, _classnames2.default)(className, classes) }), _ValidComponentChildren2.default.map(children, function (child) { return (0, _react.cloneElement)(child, { bsStyle: child.props.bsStyle || bsProps.bsStyle }); }) ); } }]); return PanelGroup; }(_react2.default.Component); PanelGroup.propTypes = propTypes; PanelGroup.defaultProps = defaultProps; PanelGroup.childContextTypes = childContextTypes; exports.default = (0, _uncontrollable2.default)((0, _bootstrapUtils.bsClass)('panel-group', PanelGroup), { activeKey: 'onSelect' });