-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a hook that API users can utilize to make custom AccordionToggle components.y
- Loading branch information
Showing
6 changed files
with
79 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useContext } from 'react'; | ||
import SelectableContext from './SelectableContext'; | ||
import AccordionContext from './AccordionContext'; | ||
|
||
export default (eventKey, onClick) => { | ||
const contextEventKey = useContext(AccordionContext); | ||
const onSelect = useContext(SelectableContext); | ||
|
||
return 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); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
function CustomToggle({ children, eventKey }) { | ||
const decoratedOnClick = useAccordionToggle(eventKey, () => | ||
console.log('totally custom!'), | ||
); | ||
|
||
return ( | ||
<button | ||
type="button" | ||
style={{ backgroundColor: 'pink' }} | ||
onClick={decoratedOnClick} | ||
> | ||
{children} | ||
</button> | ||
); | ||
} | ||
|
||
function Example() { | ||
return ( | ||
<Accordion defaultActiveKey="0"> | ||
<Card> | ||
<Card.Header> | ||
<CustomToggle eventKey="0">Click me!</CustomToggle> | ||
</Card.Header> | ||
<Accordion.Collapse eventKey="0"> | ||
<Card.Body>Hello! I'm the body</Card.Body> | ||
</Accordion.Collapse> | ||
</Card> | ||
<Card> | ||
<Card.Header> | ||
<CustomToggle eventKey="1">Click me!</CustomToggle> | ||
</Card.Header> | ||
<Accordion.Collapse eventKey="1"> | ||
<Card.Body>Hello! I'm another body</Card.Body> | ||
</Accordion.Collapse> | ||
</Card> | ||
</Accordion> | ||
); | ||
} | ||
|
||
render(<Example />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters