UNPKG

4.8 kBJavaScriptView Raw
1import _Object$assign from "@babel/runtime-corejs2/core-js/object/assign";
2import _Object$values from "@babel/runtime-corejs2/core-js/object/values";
3import _extends from "@babel/runtime-corejs2/helpers/esm/extends";
4import _inheritsLoose from "@babel/runtime-corejs2/helpers/esm/inheritsLoose";
5import classNames from 'classnames';
6import PropTypes from 'prop-types';
7import React from 'react';
8import uncontrollable from 'uncontrollable';
9import warning from 'warning';
10import { bsStyles, bsClass, getClassSet, splitBsPropsAndOmit } from './utils/bootstrapUtils';
11import { State, Style } from './utils/StyleConfig';
12import Body from './PanelBody';
13import Heading from './PanelHeading';
14import Title from './PanelTitle';
15import Footer from './PanelFooter';
16import Toggle from './PanelToggle';
17import Collapse from './PanelCollapse';
18var has = Object.prototype.hasOwnProperty;
19
20var defaultGetId = function defaultGetId(id, type) {
21 return id ? id + "--" + type : null;
22};
23
24var propTypes = {
25 /**
26 * Controls the collapsed/expanded state ofthe Panel. Requires
27 * a `Panel.Collapse` or `<Panel.Body collapsible>` child component
28 * in order to actually animate out or in.
29 *
30 * @controllable onToggle
31 */
32 expanded: PropTypes.bool,
33
34 /**
35 * A callback fired when the collapse state changes.
36 *
37 * @controllable expanded
38 */
39 onToggle: PropTypes.func,
40 eventKey: PropTypes.any,
41
42 /**
43 * An HTML `id` attribute uniquely identifying the Panel component.
44 */
45 id: PropTypes.string
46};
47var contextTypes = {
48 $bs_panelGroup: PropTypes.shape({
49 getId: PropTypes.func,
50 activeKey: PropTypes.any,
51 onToggle: PropTypes.func
52 })
53};
54var childContextTypes = {
55 $bs_panel: PropTypes.shape({
56 headingId: PropTypes.string,
57 bodyId: PropTypes.string,
58 bsClass: PropTypes.string,
59 onToggle: PropTypes.func,
60 expanded: PropTypes.bool
61 })
62};
63
64var Panel =
65/*#__PURE__*/
66function (_React$Component) {
67 _inheritsLoose(Panel, _React$Component);
68
69 function Panel() {
70 var _this;
71
72 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
73 args[_key] = arguments[_key];
74 }
75
76 _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
77
78 _this.handleToggle = function (e) {
79 var panelGroup = _this.context.$bs_panelGroup;
80 var expanded = !_this.getExpanded();
81
82 if (panelGroup && panelGroup.onToggle) {
83 panelGroup.onToggle(_this.props.eventKey, expanded, e);
84 } else {
85 _this.props.onToggle(expanded, e);
86 }
87 };
88
89 return _this;
90 }
91
92 var _proto = Panel.prototype;
93
94 _proto.getChildContext = function getChildContext() {
95 var _this$props = this.props,
96 eventKey = _this$props.eventKey,
97 id = _this$props.id;
98 var idKey = eventKey == null ? id : eventKey;
99 var ids;
100
101 if (idKey !== null) {
102 var panelGroup = this.context.$bs_panelGroup;
103 var getId = panelGroup && panelGroup.getId || defaultGetId;
104 ids = {
105 headingId: getId(idKey, 'heading'),
106 bodyId: getId(idKey, 'body')
107 };
108 }
109
110 return {
111 $bs_panel: _extends({}, ids, {
112 bsClass: this.props.bsClass,
113 expanded: this.getExpanded(),
114 onToggle: this.handleToggle
115 })
116 };
117 };
118
119 _proto.getExpanded = function getExpanded() {
120 var panelGroup = this.context.$bs_panelGroup;
121
122 if (panelGroup && has.call(panelGroup, 'activeKey')) {
123 process.env.NODE_ENV !== "production" ? warning(this.props.expanded == null, 'Specifying `<Panel>` `expanded` in the context of an accordion ' + '`<PanelGroup>` is not supported. Set `activeKey` on the ' + '`<PanelGroup>` instead.') : void 0;
124 return panelGroup.activeKey === this.props.eventKey;
125 }
126
127 return !!this.props.expanded;
128 };
129
130 _proto.render = function render() {
131 var _this$props2 = this.props,
132 className = _this$props2.className,
133 children = _this$props2.children;
134
135 var _splitBsPropsAndOmit = splitBsPropsAndOmit(this.props, ['onToggle', 'eventKey', 'expanded']),
136 bsProps = _splitBsPropsAndOmit[0],
137 props = _splitBsPropsAndOmit[1];
138
139 return React.createElement("div", _extends({}, props, {
140 className: classNames(className, getClassSet(bsProps))
141 }), children);
142 };
143
144 return Panel;
145}(React.Component);
146
147Panel.propTypes = propTypes;
148Panel.contextTypes = contextTypes;
149Panel.childContextTypes = childContextTypes;
150var UncontrolledPanel = uncontrollable(bsClass('panel', bsStyles(_Object$values(State).concat([Style.DEFAULT, Style.PRIMARY]), Style.DEFAULT, Panel)), {
151 expanded: 'onToggle'
152});
153
154_Object$assign(UncontrolledPanel, {
155 Heading: Heading,
156 Title: Title,
157 Body: Body,
158 Footer: Footer,
159 Toggle: Toggle,
160 Collapse: Collapse
161});
162
163export default UncontrolledPanel;
\No newline at end of file