UNPKG

5.36 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime-corejs2/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
6
7exports.__esModule = true;
8exports.default = void 0;
9
10var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/objectWithoutPropertiesLoose"));
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/extends"));
13
14var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/inheritsLoose"));
15
16var _classnames = _interopRequireDefault(require("classnames"));
17
18var _propTypes = _interopRequireDefault(require("prop-types"));
19
20var _react = _interopRequireWildcard(require("react"));
21
22var _uncontrollable = _interopRequireDefault(require("uncontrollable"));
23
24var _bootstrapUtils = require("./utils/bootstrapUtils");
25
26var _ValidComponentChildren = _interopRequireDefault(require("./utils/ValidComponentChildren"));
27
28var _PropTypes = require("./utils/PropTypes");
29
30var propTypes = {
31 accordion: _propTypes.default.bool,
32
33 /**
34 * When `accordion` is enabled, `activeKey` controls the which child `Panel` is expanded. `activeKey` should
35 * match a child Panel `eventKey` prop exactly.
36 *
37 * @controllable onSelect
38 */
39 activeKey: _propTypes.default.any,
40
41 /**
42 * A callback fired when a child Panel collapse state changes. It's called with the next expanded `activeKey`
43 *
44 * @controllable activeKey
45 */
46 onSelect: _propTypes.default.func,
47
48 /**
49 * An HTML role attribute
50 */
51 role: _propTypes.default.string,
52
53 /**
54 * A function that takes an eventKey and type and returns a
55 * unique id for each Panel heading and Panel Collapse. The function _must_ be a pure function,
56 * meaning it should always return the _same_ id for the same set of inputs. The default
57 * value requires that an `id` to be set for the PanelGroup.
58 *
59 * The `type` argument will either be `"body"` or `"heading"`.
60 *
61 * @defaultValue (eventKey, type) => `${this.props.id}-${type}-${key}`
62 */
63 generateChildId: _propTypes.default.func,
64
65 /**
66 * HTML id attribute, required if no `generateChildId` prop
67 * is specified.
68 */
69 id: (0, _PropTypes.generatedId)('PanelGroup')
70};
71var defaultProps = {
72 accordion: false
73};
74var childContextTypes = {
75 $bs_panelGroup: _propTypes.default.shape({
76 getId: _propTypes.default.func,
77 headerRole: _propTypes.default.string,
78 panelRole: _propTypes.default.string,
79 activeKey: _propTypes.default.any,
80 onToggle: _propTypes.default.func
81 })
82};
83
84var PanelGroup =
85/*#__PURE__*/
86function (_React$Component) {
87 (0, _inheritsLoose2.default)(PanelGroup, _React$Component);
88
89 function PanelGroup() {
90 var _this;
91
92 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
93 args[_key] = arguments[_key];
94 }
95
96 _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
97
98 _this.handleSelect = function (key, expanded, e) {
99 if (expanded) {
100 _this.props.onSelect(key, e);
101 } else if (_this.props.activeKey === key) {
102 _this.props.onSelect(null, e);
103 }
104 };
105
106 return _this;
107 }
108
109 var _proto = PanelGroup.prototype;
110
111 _proto.getChildContext = function getChildContext() {
112 var _this$props = this.props,
113 activeKey = _this$props.activeKey,
114 accordion = _this$props.accordion,
115 generateChildId = _this$props.generateChildId,
116 id = _this$props.id;
117 var getId = null;
118
119 if (accordion) {
120 getId = generateChildId || function (key, type) {
121 return id ? id + "-" + type + "-" + key : null;
122 };
123 }
124
125 return {
126 $bs_panelGroup: (0, _extends2.default)({
127 getId: getId,
128 headerRole: 'tab',
129 panelRole: 'tabpanel'
130 }, accordion && {
131 activeKey: activeKey,
132 onToggle: this.handleSelect
133 })
134 };
135 };
136
137 _proto.render = function render() {
138 var _this$props2 = this.props,
139 accordion = _this$props2.accordion,
140 className = _this$props2.className,
141 children = _this$props2.children,
142 props = (0, _objectWithoutPropertiesLoose2.default)(_this$props2, ["accordion", "className", "children"]);
143
144 var _splitBsPropsAndOmit = (0, _bootstrapUtils.splitBsPropsAndOmit)(props, ['onSelect', 'activeKey']),
145 bsProps = _splitBsPropsAndOmit[0],
146 elementProps = _splitBsPropsAndOmit[1];
147
148 if (accordion) {
149 elementProps.role = elementProps.role || 'tablist';
150 }
151
152 var classes = (0, _bootstrapUtils.getClassSet)(bsProps);
153 return _react.default.createElement("div", (0, _extends2.default)({}, elementProps, {
154 className: (0, _classnames.default)(className, classes)
155 }), _ValidComponentChildren.default.map(children, function (child) {
156 return (0, _react.cloneElement)(child, {
157 bsStyle: child.props.bsStyle || bsProps.bsStyle
158 });
159 }));
160 };
161
162 return PanelGroup;
163}(_react.default.Component);
164
165PanelGroup.propTypes = propTypes;
166PanelGroup.defaultProps = defaultProps;
167PanelGroup.childContextTypes = childContextTypes;
168
169var _default = (0, _uncontrollable.default)((0, _bootstrapUtils.bsClass)('panel-group', PanelGroup), {
170 activeKey: 'onSelect'
171});
172
173exports.default = _default;
174module.exports = exports["default"];
\No newline at end of file