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: update accessibility in button component #1451

Merged
merged 2 commits into from
Feb 13, 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
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16
4 changes: 2 additions & 2 deletions collections/forms/i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2023-05-05T12:46:11.864Z\n"
"PO-Revision-Date: 2023-05-05T12:46:11.864Z\n"
"POT-Creation-Date: 2024-02-12T14:58:56.792Z\n"
"PO-Revision-Date: 2024-02-12T14:58:56.792Z\n"

msgid "Upload file"
msgstr "Upload file"
Expand Down
14 changes: 14 additions & 0 deletions components/button/src/button/__tests__/Button.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ describe('<Button>', () => {
expect(actual.length).toBe(1)
})

it('has the accessibility attributes', () => {
const dataTest = 'button-data-test'
const wrapper = mount(
<Button
dataTest={dataTest}
ariaLabel="test aria label"
title="title for button"
/>
)
const buttonElement = wrapper.find('button').getDOMNode()
expect(buttonElement).toHaveAttribute('title', 'title for button')
expect(buttonElement).toHaveAttribute('ariaLabel', 'test aria label')
})

describe('toggle', () => {
it('should have class "toggled" if toggled-prop is true', () => {
const wrapper = mount(<Button toggled />)
Expand Down
3 changes: 3 additions & 0 deletions components/button/src/button/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
onFocus,
onKeyDown,
loading,
...otherProps
}) => {
const ref = useRef()

Expand All @@ -34,7 +35,7 @@
if (initialFocus && ref.current) {
ref.current.focus()
}
}, [initialFocus, ref.current])

Check warning on line 38 in components/button/src/button/button.js

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has an unnecessary dependency: 'ref.current'. Either exclude it or remove the dependency array. Mutable values like 'ref.current' aren't valid dependencies because mutating them doesn't re-render the component

const handleClick = (event) => onClick && onClick({ value, name }, event)
const handleBlur = (event) => onBlur && onBlur({ value, name }, event)
Expand Down Expand Up @@ -67,6 +68,7 @@
onClick={handleClick}
onFocus={handleFocus}
onKeyDown={handleKeyDown}
{...otherProps}
>
{loading && (
<span className="loader">
Expand Down Expand Up @@ -149,6 +151,7 @@
* Called with args `({ value, name }, event)`
* */
onClick: PropTypes.func,

/** Callback to trigger on focus. Called with same args as `onClick` */
onFocus: PropTypes.func,
/** Callback to trigger on key-down. Called with same args as `onClick` */
Expand Down
4 changes: 4 additions & 0 deletions components/button/src/button/button.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export default {
children: 'Label me!',
value: 'default',
onClick: logger,
title: 'Button',
ariaLabel: 'Button',
},
argTypes: {
primary: { ...buttonVariantArgType },
Expand Down Expand Up @@ -219,6 +221,8 @@ IconOnly.args = {
icon: DemoIcon24,
name: 'Icon only button',
children: null,
title: 'Icon Button',
ariaLabel: 'Icon Button',
}

export const IconSmall = Template.bind({})
Expand Down
2 changes: 1 addition & 1 deletion components/button/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ButtonOpenEventHandler<
Event extends React.SyntheticEvent = React.MouseEvent<HTMLButtonElement>
> = (arg0: ButtonEventPayload & { open: boolean }, event: Event) => void

export interface ButtonProps {
export interface ButtonProps extends HTMLButtonElement {
/**
* Component to render inside the button
*/
Expand Down
Loading