Skip to content

Commit

Permalink
feat: Enable custom on click handler passed via accordion item (#2998)
Browse files Browse the repository at this point in the history
  • Loading branch information
kleinschmidtj committed Aug 12, 2024
1 parent 9e3bea7 commit 420483a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/components/Accordion/Accordion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,26 @@ describe('Accordion component', () => {
}
)
})

describe('with a custom click handler', () => {
const customToggleFunction = vi.fn()

const customTestItems: AccordionItemProps[] = [
{
title: 'First Amendment',
content: firstAmendment,
expanded: false,
id: '123',
headingLevel: 'h4',
handleToggle: () => {
customToggleFunction()
},
},
]
it('fires the handler successfully', () => {
const { getByText } = render(<Accordion items={customTestItems} />)
fireEvent.click(getByText(testItems[0].title as string))
expect(customToggleFunction).toHaveBeenCalledOnce()
})
})
})
3 changes: 2 additions & 1 deletion src/components/Accordion/Accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export const Accordion = ({
key={`accordionItem_${i}`}
{...item}
expanded={openItems.indexOf(item.id) > -1}
handleToggle={(): void => {
handleToggle={(e): void => {
if (item.handleToggle) item.handleToggle(e)
toggleItem(item.id)
}}
/>
Expand Down

0 comments on commit 420483a

Please sign in to comment.