33 lines
1.3 KiB
JavaScript
33 lines
1.3 KiB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
import React, { useContext } from 'react';
|
|
import SelectableContext from './SelectableContext';
|
|
import AccordionContext from './AccordionContext';
|
|
export function useAccordionToggle(eventKey, onClick) {
|
|
var contextEventKey = useContext(AccordionContext);
|
|
var onSelect = useContext(SelectableContext);
|
|
return function (e) {
|
|
/*
|
|
Compare the event key in context with the given event key.
|
|
If they are the same, then collapse the component.
|
|
*/
|
|
var eventKeyPassed = eventKey === contextEventKey ? null : eventKey;
|
|
onSelect(eventKeyPassed, e);
|
|
if (onClick) onClick(e);
|
|
};
|
|
}
|
|
var AccordionToggle = React.forwardRef(function (_ref, ref) {
|
|
var _ref$as = _ref.as,
|
|
Component = _ref$as === void 0 ? 'button' : _ref$as,
|
|
children = _ref.children,
|
|
eventKey = _ref.eventKey,
|
|
onClick = _ref.onClick,
|
|
props = _objectWithoutPropertiesLoose(_ref, ["as", "children", "eventKey", "onClick"]);
|
|
|
|
var accordionOnClick = useAccordionToggle(eventKey, onClick);
|
|
return React.createElement(Component, _extends({
|
|
ref: ref,
|
|
onClick: accordionOnClick
|
|
}, props), children);
|
|
});
|
|
export default AccordionToggle; |