Skip to content

Commit

Permalink
Improve DX by passing to id to onToggle callback
Browse files Browse the repository at this point in the history
  • Loading branch information
stereobooster committed May 30, 2019
1 parent 8edb4df commit 37131d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ import { Accordion } from "./components/Accordion";
import { AccordionSection } from "./components/AccordionSection";

function App() {
const [expanded1, setExpanded1] = useState(false);
const [expanded2, setExpanded2] = useState(true);
const [expanded, setExpanded] = useState({ "2": true });
const toggle = id => {
setExpanded({
...expanded,
[id]: !expanded[id]
});
};

return (
<div className={styles.Wrapper}>
<Accordion>
<AccordionSection
title="section 1"
id="1"
expanded={expanded1}
onToggle={() => setExpanded1(!expanded1)}
expanded={expanded["1"]}
onToggle={toggle}
>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In varius
Expand All @@ -32,8 +37,8 @@ function App() {
<AccordionSection
title="section 2"
id="2"
expanded={expanded2}
onToggle={() => setExpanded2(!expanded2)}
expanded={expanded["2"]}
onToggle={toggle}
>
<p>
<a href="/">test link</a>
Expand Down
4 changes: 2 additions & 2 deletions src/components/AccordionSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export const AccordionSection = ({
id={labelId}
tabIndex={0}
className={styles.Label}
onClick={onToggle}
onClick={() => onToggle(id)}
onKeyDown={e => {
switch (e.key) {
case " ":
case "Enter":
onToggle();
onToggle(id);
break;
default:
}
Expand Down

0 comments on commit 37131d7

Please sign in to comment.