docs/data/material/components/rating/rating.md
{{"component": "@mui/internal-core-docs/ComponentLinkHeader"}}
{{"demo": "BasicRating.js"}}
The rating can display any float number with the value prop.
Use the precision prop to define the minimum increment value change allowed.
{{"demo": "HalfRating.js"}}
You can display a label on hover to help the user pick the correct rating value.
The demo uses the onChangeActive prop.
{{"demo": "HoverRating.js"}}
For larger or smaller ratings use the size prop.
{{"demo": "RatingSize.js"}}
Here are some examples of customizing the component. You can learn more about this in the overrides documentation page.
{{"demo": "CustomizedRating.js"}}
The rating is implemented with a radio group, set highlightSelectedOnly to restore the natural behavior.
{{"demo": "RadioGroupRating.js"}}
The accessibility of this component relies on:
name prop that is unique to the parent form.getLabelText prop when the page is in a language other than English. You can use the included locales, or provide your own.{{"demo": "TextRating.js"}}
The read only rating has a role of "img", and an aria-label that describes the displayed rating.
Because the rating component uses radio buttons, keyboard interaction follows the native browser behavior. Tab will focus the current rating, and cursor keys control the selected rating.
The read only rating is not focusable.
When testing the Rating component in environments such as Jest with jsdom, certain user interactions—especially hover-based interactions—may not behave as expected.
This is because the component relies on getBoundingClientRect() to calculate the position of each icon and determine which icon is currently being hovered.
In jsdom, getBoundingClientRect() returns 0 values by default, which can lead to incorrect behavior such as NaN being passed to the onChange handler.
To avoid this issue in your test suite:
fireEvent over userEvent when simulating click events.getBoundingClientRect() manually for more advanced interactions.// @vitest-environment jsdom
import { Rating } from '@mui/material';
import { render, fireEvent, screen } from '@testing-library/react';
import { describe, test, vi } from 'vitest';
describe('Rating', () => {
test('should update rating on click', () => {
const handleChange = vi.fn();
render(<Rating onChange={(_, newValue) => handleChange(newValue)} />);
fireEvent.click(screen.getByLabelText('2 Stars'));
expect(handleChange).toHaveBeenCalledWith(2);
});
});