UNPKG

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