Skip to content

Commit 7de751e

Browse files
mufasubhaiAdrian Apodaca
andauthored
fix(components/accordion): allow opened panel to be closed #684 - add accordion tests (#705)
* fix(accordion.tsx): fixed bug where Accordion.Content would not close after being opened Accordion content can now be closed and opened leaving all items collapsed. #618 * style(accordion.tsx): update spacing after adding toggleOpen function * test(accordion.spec.tsx): add tests for accordion click to toggle #684 * style(accordion.spec): prettier * style(accordion.tsx): change conditional to ternary for toggleOpen * style(accordion.tsx): prettier update for cloneElement format --------- Co-authored-by: Adrian Apodaca <[email protected]>
1 parent 03c8e56 commit 7de751e

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/lib/components/Accordion/Accordion.spec.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ describe('Components / Accordion', () => {
5858
expect(content()[1]).toBeVisible();
5959
});
6060

61+
it('it should open and close self when `Space is pressed on the same`Accordion.Panel`', async () => {
62+
const user = userEvent.setup();
63+
render(<TestAccordion />);
64+
65+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
66+
for (const _ of titles()) {
67+
await user.tab();
68+
}
69+
await user.keyboard('[Space]');
70+
await user.keyboard('[Space]');
71+
72+
expect(content()[0]).not.toBeVisible();
73+
expect(content()[1]).not.toBeVisible();
74+
});
6175
it('should open focused panel without closing others on an `Accordion.Panel` with `alwaysOpen={true}`', async () => {
6276
const user = userEvent.setup();
6377
render(<TestAccordion alwaysOpen />);
@@ -237,6 +251,20 @@ describe('Components / Accordion', () => {
237251
});
238252
});
239253
});
254+
describe('Click to toggle open', () => {
255+
beforeEach(() => {
256+
render(<TestAccordion />);
257+
});
258+
259+
it('should open and close the accordion when title is clicked', async () => {
260+
const titleElements = titles();
261+
262+
await userEvent.click(titleElements[1]); // open second panel
263+
await userEvent.click(titleElements[1]); // close second panel
264+
expect(content()[0]).not.toBeVisible(); // content should not be visible
265+
expect(content()[1]).not.toBeVisible(); // content should not be visible
266+
});
267+
});
240268
});
241269

242270
const TestAccordion: FC<Omit<AccordionProps, 'children'>> = (props) => (

src/lib/components/Accordion/Accordion.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,17 @@ const AccordionComponent: FC<AccordionProps> = ({
4444
...props
4545
}) => {
4646
const [isOpen, setOpen] = useState(collapseAll ? -1 : 0);
47+
4748
const panels = useMemo(
4849
() =>
4950
Children.map(children, (child, i) =>
50-
cloneElement(child, { alwaysOpen, arrowIcon, flush, isOpen: isOpen === i, setOpen: () => setOpen(i) }),
51+
cloneElement(child, {
52+
alwaysOpen,
53+
arrowIcon,
54+
flush,
55+
isOpen: isOpen === i,
56+
setOpen: () => setOpen(isOpen === i ? -1 : i),
57+
}),
5158
),
5259
[alwaysOpen, arrowIcon, children, flush, isOpen],
5360
);

0 commit comments

Comments
 (0)