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

chore: accessibility on dropdown button #1460

Merged
merged 1 commit into from
Jun 3, 2024
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
@@ -1,8 +1,10 @@
import { Layer } from '@dhis2-ui/layer'
import { Popper } from '@dhis2-ui/popper'
import { render, fireEvent, waitFor } from '@testing-library/react'
import { mount } from 'enzyme'
import React from 'react'
import { act } from 'react-dom/test-utils'
import { Modal } from '../../../../modal/src/modal/modal.js'
import { Button } from '../../index.js'
import { DropdownButton } from '../dropdown-button.js'

Expand Down Expand Up @@ -43,7 +45,64 @@ describe('<DropdownButton>', () => {
})
)
})
it('closes dropdown when escape key is pressed', async () => {
const componentText = 'Dropdown Content'

const { getByTestId, queryByText } = render(
<DropdownButton component={componentText} />
)

const toggleButton = getByTestId(
'dhis2-uicore-dropdownbutton-toggle'
)
fireEvent.click(toggleButton)

await waitFor(() => {
expect(queryByText(componentText)).toBeInTheDocument()
})

fireEvent.keyDown(document, { key: 'Escape' })

await waitFor(() => {
expect(queryByText(componentText)).not.toBeInTheDocument()
})
})
test('modal remains open when dropdown button is closed on escape click', async () => {
const dropdownButtonText = 'Dropdown Content'
const headingText = 'Heading Text'
const modalContent = (
<div>
<h1>{headingText}</h1>
<DropdownButton component={dropdownButtonText} />
</div>
)

const { getByTestId, queryByText } = render(
<Modal hide={false} onClose={() => {}}>
{modalContent}
</Modal>
)

const toggleButton = getByTestId(
'dhis2-uicore-dropdownbutton-toggle'
)
fireEvent.click(toggleButton)

await waitFor(() => {
expect(queryByText(dropdownButtonText)).toBeInTheDocument()
})

fireEvent.keyDown(document, { key: 'Escape' })

await waitFor(() => {
expect(
queryByText(dropdownButtonText)
).not.toBeInTheDocument()
expect(queryByText(headingText)).toBeInTheDocument()
})
})
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have a test that wraps this button in a modal, and make sure that only the dropdown is closed when esc is pressed?


describe('closed state', () => {
const onClick = jest.fn()
const wrapper = mount(
Expand Down
17 changes: 17 additions & 0 deletions components/button/src/dropdown-button/dropdown-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ class DropdownButton extends Component {

anchorRef = React.createRef()

componentDidMount() {
document.addEventListener('keydown', this.handleKeyDown)
}

componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeyDown)
}

handleKeyDown = (event) => {
event.preventDefault()
if (event.key === 'Escape' && this.state.open) {
event.stopPropagation()
this.setState({ open: false })
}
}

onClickHandler = ({ name, value }, event) => {
const handleClick = (open) => {
if (this.props.onClick) {
Expand Down Expand Up @@ -133,6 +149,7 @@ class DropdownButton extends Component {
tabIndex={tabIndex}
type={type}
initialFocus={initialFocus}
data-test="dhis2-uicore-dropdownbutton-toggle"
>
{children}
<ArrowIconComponent className={arrow.className} />
Expand Down
Loading