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

feat: splitButton accessibility improvements #1458

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
23 changes: 22 additions & 1 deletion components/button/src/split-button/split-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,25 @@ class SplitButton extends Component {
state = {
open: false,
}

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) {
Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
event.stopPropagation()
this.setState({ open: false })
this.anchorRef.current && this.anchorRef.current.focus()
}
}

onClick = (payload, event) => {
if (this.props.onClick) {
this.props.onClick(
Expand All @@ -33,7 +50,9 @@ class SplitButton extends Component {
}
}

onToggle = () => this.setState({ open: !this.state.open })
onToggle = () => {
this.setState((prevState) => ({ open: !prevState.open }))
}

render() {
const { open } = this.state
Expand Down Expand Up @@ -94,6 +113,8 @@ class SplitButton extends Component {
tabIndex={tabIndex}
className={cx(className, rightButton.className)}
dataTest={`${dataTest}-toggle`}
title="Toggle dropdown"
Copy link
Collaborator

Choose a reason for hiding this comment

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

use i18n for the labels

aria-label="Toggle dropdown"
>
{arrow}
</Button>
Expand Down
104 changes: 104 additions & 0 deletions components/button/src/split-button/split-button.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { render, fireEvent, cleanup, waitFor } from '@testing-library/react'
import React from 'react'
import '@testing-library/jest-dom/extend-expect'
import { SplitButton } from './split-button.js'

describe('SplitButton', () => {
afterEach(cleanup)

it('renders button with children', () => {
const { getByText } = render(<SplitButton>Click me</SplitButton>)
expect(getByText('Click me')).toBeInTheDocument()
})

it('toggles dropdown when right button is clicked', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

it's left button, not right?

const { getByTestId, queryByTestId } = render(<SplitButton />)
const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle')

fireEvent.click(toggleButton)
expect(
queryByTestId('dhis2-uicore-splitbutton-menu')
).toBeInTheDocument()

fireEvent.click(toggleButton)
expect(
queryByTestId('dhis2-uicore-splitbutton-menu')
).not.toBeInTheDocument()
})

it('renders dropdown content when open is true', () => {
const { getByTestId } = render(
<SplitButton component={<div>Dropdown Content</div>} />
)

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

expect(getByTestId('dhis2-uicore-splitbutton-menu')).toBeInTheDocument()
})

it("does not close dropdown 'Enter' key is pressed", async () => {
const { getByTestId } = render(
<SplitButton component={<div>Dropdown Content</div>} />
)

const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle')
fireEvent.click(toggleButton)
expect(getByTestId('dhis2-uicore-splitbutton-menu')).toBeInTheDocument()

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

// Use waitFor to wait for the DOM to update
await waitFor(() => {
expect(
getByTestId('dhis2-uicore-splitbutton-menu')
).toBeInTheDocument()
})
})

it("does not close dropdown 'SpaceBar' key is pressed", async () => {
const { getByTestId } = render(
<SplitButton component={<div>Dropdown Content</div>} />
)

const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle')
fireEvent.click(toggleButton)
expect(getByTestId('dhis2-uicore-splitbutton-menu')).toBeInTheDocument()

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

// Use waitFor to wait for the DOM to update
await waitFor(() => {
expect(
getByTestId('dhis2-uicore-splitbutton-menu')
).toBeInTheDocument()
})
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

one of these tests is enough (maybe remvoe the space bar one)


it('closes dropdown when escape key is pressed', async () => {
const { getByTestId, queryByTestId } = render(
<SplitButton component={<div>Dropdown Content</div>} />
)

const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle')
fireEvent.click(toggleButton)
expect(getByTestId('dhis2-uicore-splitbutton-menu')).toBeInTheDocument()

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

// Use waitFor to wait for the DOM to update
await waitFor(() => {
expect(
queryByTestId('dhis2-uicore-splitbutton-menu')
).not.toBeInTheDocument()
})
})

it('adds title and aria-label attributes to the right button', () => {
const { getByTestId } = render(<SplitButton />)
const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle')

expect(toggleButton).toHaveAttribute('title', 'Toggle dropdown')
expect(toggleButton).toHaveAttribute('aria-label', 'Toggle dropdown')
})
})
Loading