UNPKG

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