UNPKG

2.68 kBJavaScriptView Raw
1import _objectWithoutPropertiesLoose from "@babel/runtime-corejs2/helpers/esm/objectWithoutPropertiesLoose";
2import _inheritsLoose from "@babel/runtime-corejs2/helpers/esm/inheritsLoose";
3import _assertThisInitialized from "@babel/runtime-corejs2/helpers/esm/assertThisInitialized";
4import PropTypes from 'prop-types';
5import React from 'react';
6import classNames from 'classnames';
7import elementType from 'react-prop-types/lib/elementType';
8import SafeAnchor from './SafeAnchor';
9import createChainedFunction from './utils/createChainedFunction';
10var propTypes = {
11 /**
12 * only here to satisfy linting, just the html onClick handler.
13 *
14 * @private
15 */
16 onClick: PropTypes.func,
17
18 /**
19 * You can use a custom element for this component
20 */
21 componentClass: elementType
22};
23var defaultProps = {
24 componentClass: SafeAnchor
25};
26var contextTypes = {
27 $bs_panel: PropTypes.shape({
28 bodyId: PropTypes.string,
29 onToggle: PropTypes.func,
30 expanded: PropTypes.bool
31 })
32};
33
34var PanelToggle =
35/*#__PURE__*/
36function (_React$Component) {
37 _inheritsLoose(PanelToggle, _React$Component);
38
39 function PanelToggle() {
40 var _this;
41
42 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
43 args[_key] = arguments[_key];
44 }
45
46 _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
47 _this.handleToggle = _this.handleToggle.bind(_assertThisInitialized(_assertThisInitialized(_this)));
48 return _this;
49 }
50
51 var _proto = PanelToggle.prototype;
52
53 _proto.handleToggle = function handleToggle(event) {
54 var _ref = this.context.$bs_panel || {},
55 onToggle = _ref.onToggle;
56
57 if (onToggle) {
58 onToggle(event);
59 }
60 };
61
62 _proto.render = function render() {
63 var _this$props = this.props,
64 onClick = _this$props.onClick,
65 className = _this$props.className,
66 componentClass = _this$props.componentClass,
67 props = _objectWithoutPropertiesLoose(_this$props, ["onClick", "className", "componentClass"]);
68
69 var _ref2 = this.context.$bs_panel || {},
70 expanded = _ref2.expanded,
71 bodyId = _ref2.bodyId;
72
73 var Component = componentClass;
74 props.onClick = createChainedFunction(onClick, this.handleToggle);
75 props['aria-expanded'] = expanded;
76 props.className = classNames(className, !expanded && 'collapsed');
77
78 if (bodyId) {
79 props['aria-controls'] = bodyId;
80 }
81
82 return React.createElement(Component, props);
83 };
84
85 return PanelToggle;
86}(React.Component);
87
88PanelToggle.propTypes = propTypes;
89PanelToggle.defaultProps = defaultProps;
90PanelToggle.contextTypes = contextTypes;
91export default PanelToggle;
\No newline at end of file