Skip to content

Commit

Permalink
[material-ui][Rating] Fix the hover highlighting for spaced icons (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeeshanTamboli committed Nov 8, 2023
1 parent efcdf73 commit aff59c2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
8 changes: 4 additions & 4 deletions packages/mui-material/src/Rating/Rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,14 @@ const Rating = React.forwardRef(function Rating(inProps, ref) {
}

const rootNode = rootRef.current;
const { right, left } = rootNode.getBoundingClientRect();
const { width } = rootNode.firstChild.getBoundingClientRect();
const { right, left, width: containerWidth } = rootNode.getBoundingClientRect();

let percent;

if (theme.direction === 'rtl') {
percent = (right - event.clientX) / (width * max);
percent = (right - event.clientX) / containerWidth;
} else {
percent = (event.clientX - left) / (width * max);
percent = (event.clientX - left) / containerWidth;
}

let newHover = roundValueToPrecision(max * percent + precision / 2, precision);
Expand Down
48 changes: 46 additions & 2 deletions packages/mui-material/src/Rating/Rating.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,62 @@ describe('<Rating />', () => {
stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
left: 0,
right: 100,
width: 100,
}));
stub(container.firstChild.firstChild, 'getBoundingClientRect').callsFake(() => ({
width: 20,
fireEvent.mouseMove(container.firstChild, {
clientX: 19,
});
expect(container.querySelectorAll(`.${classes.iconHover}`).length).to.equal(1);
fireEvent.mouseMove(container.firstChild, {
clientX: 21,
});
expect(container.querySelectorAll(`.${classes.iconHover}`).length).to.equal(2);
});

it('should handle mouse hover correctly for icons with spacing', () => {
const { container } = render(
<Rating
sx={{
[`.${classes.decimal}`]: { marginRight: 2 },
}}
precision={0.5}
/>,
);
stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
left: 0,
right: 200,
width: 200,
}));

fireEvent.mouseMove(container.firstChild, {
clientX: 19,
});
// half star highlighted
expect(container.querySelectorAll(`.${classes.iconHover}`).length).to.equal(1);

fireEvent.mouseMove(container.firstChild, {
clientX: 21,
});
// one full star highlighted
expect(container.querySelectorAll(`.${classes.iconHover}`).length).to.equal(2);

fireEvent.mouseMove(container.firstChild, {
clientX: 39,
});
// Still one star remains highlighted as the total item width (40px) has not been reached yet, considering 24px for the icon width and 16px for margin-right.
expect(container.querySelectorAll(`.${classes.iconHover}`).length).to.equal(2);

fireEvent.mouseMove(container.firstChild, {
clientX: 41,
});
// one and half star highlighted
expect(container.querySelectorAll(`.${classes.iconHover}`).length).to.equal(3);

fireEvent.mouseMove(container.firstChild, {
clientX: 60,
});
// two full stars highlighted
expect(container.querySelectorAll(`.${classes.iconHover}`).length).to.equal(4);
});

it('should clear the rating', () => {
Expand Down

0 comments on commit aff59c2

Please sign in to comment.