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] Use Rating name as prefix of input element ids #43829

Merged
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
2 changes: 1 addition & 1 deletion docs/translations/api-docs/rating/rating.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"IconContainerComponent": { "description": "The component containing the icon." },
"max": { "description": "Maximum rating." },
"name": {
"description": "The name attribute of the radio <code>input</code> elements. This input <code>name</code> should be unique within the page. Being unique within a form is insufficient since the <code>name</code> is used to generated IDs."
"description": "The name attribute of the radio <code>input</code> elements. This input <code>name</code> should be unique within the page. Being unique within a form is insufficient since the <code>name</code> is used to generate IDs."
},
"onChange": {
"description": "Callback fired when the value changes.",
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/Rating/Rating.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface RatingProps
/**
* The name attribute of the radio `input` elements.
* This input `name` should be unique within the page.
* Being unique within a form is insufficient since the `name` is used to generated IDs.
* Being unique within a form is insufficient since the `name` is used to generate IDs.
*/
name?: string;
/**
Expand Down
9 changes: 7 additions & 2 deletions packages/mui-material/src/Rating/Rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ function RatingItem(props) {
const isFocused = itemValue <= focus;
const isChecked = itemValue === ratingValueRounded;

const id = useId();
// "name" ensures unique IDs across different Rating components in React 17,
// preventing one component from affecting another. React 18's useId already handles this.
// Update to const id = useId(); when React 17 support is dropped.
// More details: https://github.com/mui/material-ui/issues/40997
const id = `${name}-${useId()}`;
Copy link
Member

Choose a reason for hiding this comment

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

I changed it to this since name will always be available either through provided prop or generated through useId below.


const container = (
<RatingIcon
as={IconContainerComponent}
Expand Down Expand Up @@ -705,7 +710,7 @@ Rating.propTypes /* remove-proptypes */ = {
/**
* The name attribute of the radio `input` elements.
* This input `name` should be unique within the page.
* Being unique within a form is insufficient since the `name` is used to generated IDs.
* Being unique within a form is insufficient since the `name` is used to generate IDs.
*/
name: PropTypes.string,
/**
Expand Down
10 changes: 10 additions & 0 deletions packages/mui-material/src/Rating/Rating.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ describe('<Rating />', () => {
expect(new Set(radios.map((radio) => radio.name))).to.have.length(1);
});

it('should use `name` as prefix of input element ids', () => {
render(<Rating name="rating-test" />);

const radios = document.querySelectorAll('input[type="radio"]');

for (let i = 0; i < radios.length; i += 1) {
expect(radios[i].getAttribute('id')).to.match(/^rating-test-/);
}
});

describe('prop: readOnly', () => {
it('renders a role="img"', () => {
render(<Rating readOnly value={2} />);
Expand Down