UNPKG

3 kBJavaScriptView Raw
1var _excluded = ["className", "cssModule", "tag", "innerRef", "children", "targetId"];
2function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
3function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
5import React, { useContext } from 'react';
6import PropTypes from 'prop-types';
7import classNames from 'classnames';
8import { mapToCssModules, tagPropType } from './utils';
9import { AccordionContext } from './AccordionContext';
10var propTypes = {
11 children: PropTypes.node,
12 /** Add custom class */
13 className: PropTypes.string,
14 /** Change existing base class name with a new class name */
15 cssModule: PropTypes.object,
16 innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.string, PropTypes.func]),
17 /** Set a custom element for this component */
18 tag: tagPropType,
19 /** Unique key used to control item's collapse/expand */
20 targetId: PropTypes.string.isRequired
21};
22function AccordionHeader(props) {
23 var className = props.className,
24 cssModule = props.cssModule,
25 _props$tag = props.tag,
26 Tag = _props$tag === void 0 ? 'h2' : _props$tag,
27 innerRef = props.innerRef,
28 children = props.children,
29 targetId = props.targetId,
30 attributes = _objectWithoutProperties(props, _excluded);
31 var _useContext = useContext(AccordionContext),
32 open = _useContext.open,
33 toggle = _useContext.toggle;
34 var classes = mapToCssModules(classNames(className, 'accordion-header'), cssModule);
35 var buttonClasses = mapToCssModules(classNames('accordion-button', {
36 collapsed: !(Array.isArray(open) ? open.includes(targetId) : open === targetId)
37 }), cssModule);
38 return /*#__PURE__*/React.createElement(Tag, _extends({}, attributes, {
39 className: classes,
40 ref: innerRef
41 }), /*#__PURE__*/React.createElement("button", {
42 type: "button",
43 className: buttonClasses,
44 onClick: function onClick() {
45 return toggle(targetId);
46 }
47 }, children));
48}
49AccordionHeader.propTypes = propTypes;
50export default AccordionHeader;
\No newline at end of file