Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AccordionHeader toggleExpanded #1121

Merged
merged 1 commit into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ describe('Accordion', () => {
fireEvent.click(header)
expect(header).toHaveAttribute('aria-expanded', 'true')
})
it('triggers onToggle callback', () => {
const mockOnToggle = jest.fn()
render(
<AccordionItem isExpanded>
<AccordionHeader onToggle={mockOnToggle}>Summary 1</AccordionHeader>
</AccordionItem>,
)
const header = screen.queryByText('Summary 1').parentNode
fireEvent.click(header)
expect(mockOnToggle).toHaveBeenCalled()
})
it('Set header level', () => {
render(<SimpleAccordion headerLevel="h3" />)
expect(document.querySelectorAll('h3')).toHaveLength(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ type AccordionHeaderProps = {
disabled?: boolean
/** @ignore */
toggleExpanded?: () => void
/** Accordion item toggle callback */
onToggle?: () => void
} & AccordionProps &
HTMLAttributes<HTMLDivElement>

Expand All @@ -116,13 +118,19 @@ const AccordionHeader = forwardRef<HTMLDivElement, AccordionHeaderProps>(
const handleClick = () => {
if (!disabled) {
toggleExpanded()
if (props.onToggle) {
props.onToggle()
}
}
}

const handleKeyDown = (event: React.KeyboardEvent) => {
const { key } = event
if (key === 'Enter' || key === ' ') {
toggleExpanded()
if (props.onToggle) {
props.onToggle()
}
event.preventDefault()
}
}
Expand Down