Skip to content

Commit

Permalink
fix: Accordion Component not collapsing when toggled twice
Browse files Browse the repository at this point in the history
Fixes #3726
  • Loading branch information
bpas247 committed May 14, 2019
1 parent cc46b5f commit cd842f7
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/AccordionToggle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import SelectableContext from './SelectableContext';
import AccordionContext from './AccordionContext';

const propTypes = {
/** Set a custom element for this component */
Expand Down Expand Up @@ -31,13 +32,20 @@ const AccordionToggle = React.forwardRef(
},
ref,
) => {
const contextEventKey = useContext(AccordionContext);
const onSelect = useContext(SelectableContext);

return (
<Component
ref={ref}
onClick={e => {
onSelect(eventKey, e);
/*
Compare the event key in context with the given event key.
If they are the same, then collapse the component.
*/
let eventKeyPassed = eventKey === contextEventKey ? null : eventKey;

onSelect(eventKeyPassed, e);
if (onClick) onClick(e);
}}
{...props}
Expand Down

0 comments on commit cd842f7

Please sign in to comment.