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: update warning in button component #1455

Merged
merged 6 commits into from
Mar 18, 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
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: 2024-02-12T14:58:56.792Z\n"
"PO-Revision-Date: 2024-02-12T14:58:56.792Z\n"
"POT-Creation-Date: 2024-02-15T15:06:08.983Z\n"
"PO-Revision-Date: 2024-02-15T15:06:08.983Z\n"

msgid "Upload file"
msgstr "Upload file"
Expand Down
46 changes: 46 additions & 0 deletions components/button/src/button/__tests__/Button.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,52 @@ import React from 'react'
import { Button } from '../button.js'

describe('<Button>', () => {
let consoleSpy

beforeEach(() => {
consoleSpy = jest.spyOn(console, 'debug').mockImplementation()
})

afterEach(() => {
consoleSpy.mockRestore()
})

describe('warning for missing aria-label and title', () => {
it('No warning if children exist but aria-label and title is missing', () => {
render(<Button>Children content</Button>)

expect(consoleSpy).not.toHaveBeenCalled()
})

it('does not warn if aria-label and title is present', () => {
render(
<Button aria-label="Test" title="Test">
Children content
</Button>
)

expect(consoleSpy).not.toHaveBeenCalled()
})

it('warns if no children are present with no arial-label and title', () => {
render(<Button>{/* No children */}</Button>)

expect(consoleSpy).toHaveBeenCalledWith(
'Button component has no children but is missing title and ariaLabel attribute.'
)
})

it('No warning if there are no children but arial label and title', () => {
render(
<Button aria-label="Test" title="Test">
{/* No children */}
</Button>
)

expect(consoleSpy).not.toHaveBeenCalled()
})
})

it('renders a default data-test attribute', () => {
const dataTest = 'dhis2-uicore-button'
const wrapper = mount(<Button dataTest={dataTest} />)
Expand Down
8 changes: 8 additions & 0 deletions components/button/src/button/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@
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 { 'aria-label': ariaLabel, title } = otherProps
Copy link
Collaborator

Choose a reason for hiding this comment

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

not part of this PR - but I think ariaLabel in the previous tests should be updated to aria-label like in this test

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah we should make sure these are aria-label (not camelcased). This applies to your previous PR as well @d-rita - let's update them to make them consistent aria-*

In React, all DOM properties and attributes (including event handlers) should be camelCased. For example, the HTML attribute tabindex corresponds to the attribute tabIndex in React. The exception is aria-* and data-* attributes, which should be lowercased. For example, you can keep aria-label as aria-label.

https://legacy.reactjs.org/docs/dom-elements.html

Copy link
Member Author

Choose a reason for hiding this comment

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

alright


if (!children && !title && !ariaLabel) {
console.debug(
'Button component has no children but is missing title and ariaLabel attribute.'
)
}

const handleClick = (event) => onClick && onClick({ value, name }, event)
const handleBlur = (event) => onBlur && onBlur({ value, name }, event)
const handleFocus = (event) => onFocus && onFocus({ value, name }, event)
Expand Down
Loading