Skip to content

Commit

Permalink
feat: Add useAccordionToggle hook
Browse files Browse the repository at this point in the history
Adds a hook that API users can utilize to make custom
AccordionToggle components.y
  • Loading branch information
bpas247 committed Jul 13, 2019
1 parent d879923 commit 140ddf7
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 19 deletions.
23 changes: 4 additions & 19 deletions src/AccordionToggle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useContext } from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import SelectableContext from './SelectableContext';
import AccordionContext from './AccordionContext';
import useAccordionToggle from './useAccordionToggle';

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

return (
<Component
ref={ref}
onClick={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}
>
<Component ref={ref} onClick={accordionOnClick} {...props}>
{children}
</Component>
);
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export Accordion from './Accordion';
export useAccordionToggle from './useAccordionToggle';
export Alert from './Alert';
export Badge from './Badge';
export Breadcrumb from './Breadcrumb';
Expand Down
19 changes: 19 additions & 0 deletions src/useAccordionToggle.js
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);
};
};
2 changes: 2 additions & 0 deletions www/src/examples/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@

"ThemeProvider": false,

"useAccordionToggle": false,

"mountNode": false,
"yup": false,
"formik": false,
Expand Down
40 changes: 40 additions & 0 deletions www/src/examples/Accordion/CustomToggle.js
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 />);
13 changes: 13 additions & 0 deletions www/src/pages/components/accordion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ReactPlayground from '../../components/ReactPlayground';
import Basic from '../../examples/Accordion/Basic';
import AllCollapse from '../../examples/Accordion/AllCollapse';
import EntireHeaderClickable from '../../examples/Accordion/EntireHeaderClickable';
import CustomToggle from '../../examples/Accordion/CustomToggle.js';

# Accordion

Expand Down Expand Up @@ -37,12 +38,24 @@ underlying component to be a CardHeader component.

<ReactPlayground codeText={EntireHeaderClickable} />

### Custom Toggle

You can now hook into the Accordion toggle functionality via `useAccordionToggle` to make custom toggle components.

<ReactPlayground codeText={CustomToggle} />

## API

<ComponentApi metadata={props.data.Accordion} />
<ComponentApi metadata={props.data.AccordionToggle} exportedBy={props.data.Accordion} />
<ComponentApi metadata={props.data.AccordionCollapse} exportedBy={props.data.Accordion} />

### useAccordionToggle

```js
const decoratedOnClick = useAccordionToggle(eventKey, onClick);
```

export const query = graphql`
query AccordionQuery {
Accordion: componentMetadata(displayName: { eq: "Accordion" }) {
Expand Down

0 comments on commit 140ddf7

Please sign in to comment.