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

[material-ui][Rating] fix defaultLabelText a11y issue with undefine value input and hint #42810

Open
wants to merge 3 commits into
base: next
Choose a base branch
from

Conversation

ZouYouShun
Copy link
Contributor

when I use Rating readOnly mode, I found there is a issue with the aria-label by default defaultLabelText

which will render like below aria-label="null Hearts"

image

@aarongarciah aarongarciah self-assigned this Jul 1, 2024
@aarongarciah aarongarciah added bug 🐛 Something doesn't work package: material-ui Specific to @mui/material component: rating This is the name of the generic UI component, not the React module! accessibility a11y labels Jul 1, 2024
Copy link
Member

@aarongarciah aarongarciah left a comment

Choose a reason for hiding this comment

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

Thank you @ZouYouShun. I left some small comments.

Comment on lines 329 to 330
const _value = value || 0;
return `${_value} Star${_value > 1 ? 's' : ''}`;
Copy link
Member

Choose a reason for hiding this comment

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

This would give us:

  • null (or any falsy value) => "0 Star" (it should be in plural)
  • 0 => "0 Star" (it should be in plural)
  • 1 => "1 Star"
  • 2 => "2 Stars"

If we change it to this, we get better results:

Suggested change
const _value = value || 0;
return `${_value} Star${_value > 1 ? 's' : ''}`;
return `${value || "0"} Star${value !== 1 ? 's' : ''}`;
  • null (or any falsy value) => "0 Stars"
  • 0 => "0 Stars"
  • 1 => "1 Star"
  • 2 => "2 Stars"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @aarongarciah

LGTM, updated😎

Comment on lines 687 to 688
* const _value = value || 0;
* return `${_value} Star${_value > 1 ? 's' : ''}`;
Copy link
Member

Choose a reason for hiding this comment

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

Modify this to match the previous comment in this file.

it('can be labelled with getLabelText when non set value', () => {
render(<Rating readOnly />);

expect(screen.getByRole('img')).toHaveAccessibleName('0 Heart');
Copy link
Member

Choose a reason for hiding this comment

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

It should be in plural.

Suggested change
expect(screen.getByRole('img')).toHaveAccessibleName('0 Heart');
expect(screen.getByRole('img')).toHaveAccessibleName('0 Hearts');

@@ -225,6 +225,12 @@ describe('<Rating />', () => {
expect(screen.getByRole('img')).toHaveAccessibleName('Stars: 2');
});

it('can be labelled with getLabelText when non set value', () => {
Copy link
Member

Choose a reason for hiding this comment

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

This test title seems incorrect.

Suggested change
it('can be labelled with getLabelText when non set value', () => {
it('should have a correct label when no value is set', () => {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accessibility a11y bug 🐛 Something doesn't work component: rating This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants