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

[Rating] Improve rendering of arbitrary precision #17013

Merged
merged 3 commits into from
Aug 20, 2019
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
22 changes: 18 additions & 4 deletions packages/material-ui-lab/src/Rating/Rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ function clamp(value, min, max) {
return value;
}

function getDecimalPrecision(num) {
const decimalPart = num.toString().split('.')[1];
return decimalPart ? decimalPart.length : 0;
}

function roundValueToPrecision(value, precision) {
const nearest = Math.round(value / precision) * precision;
return Number(nearest.toFixed(getDecimalPrecision(precision)));
}

export const styles = theme => ({
/* Styles applied to the root element. */
root: {
Expand Down Expand Up @@ -133,10 +143,11 @@ const Rating = React.forwardRef(function Rating(props, ref) {
precision = 1,
readOnly = false,
size = 'medium',
value: valueProp = null,
value: valueProp2 = null,
...other
} = props;

const valueProp = roundValueToPrecision(valueProp2, precision);
const theme = useTheme();
const [{ hover, focus }, setState] = React.useState({
hover: -1,
Expand Down Expand Up @@ -174,7 +185,7 @@ const Rating = React.forwardRef(function Rating(props, ref) {
percent = (event.pageX - left - ownerWindow(rootNode).pageXOffset) / (width * max);
}

let newHover = Math.ceil((max * percent) / precision) * precision;
let newHover = roundValueToPrecision(max * percent, precision);
newHover = clamp(newHover, precision, max);
setState(prev =>
prev.hover === newHover && prev.focus === newHover
Expand Down Expand Up @@ -341,7 +352,10 @@ const Rating = React.forwardRef(function Rating(props, ref) {
})}
>
{items.map(($, indexDecimal) => {
const itemDeciamlValue = itemValue - 1 + (indexDecimal + 1) * precision;
const itemDeciamlValue = roundValueToPrecision(
itemValue - 1 + (indexDecimal + 1) * precision,
precision,
);

return item(
{
Expand Down Expand Up @@ -380,7 +394,7 @@ const Rating = React.forwardRef(function Rating(props, ref) {
filled: itemValue <= value,
hover: itemValue <= hover,
focus: itemValue <= focus,
checked: itemValue === Math.round(valueProp),
checked: itemValue === valueProp,
},
);
})}
Expand Down
11 changes: 11 additions & 0 deletions packages/material-ui-lab/src/Rating/Rating.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('<Rating />', () => {
const render = createClientRender({ strict: true });
let classes;
const defaultProps = {
name: 'rating-test',
value: 2,
};

Expand Down Expand Up @@ -39,4 +40,14 @@ describe('<Rating />', () => {

expect(container.firstChild).to.have.class(classes.root);
});

it('should round the value to the provided precision', () => {
const { container, getByLabelText } = render(
<Rating {...defaultProps} value={3.9} precision={0.2} />,
);
const input = getByLabelText('4 Stars');
const checked = container.querySelector('input[name="rating-test"]:checked');
expect(input).to.equal(checked);
expect(input.value).to.equal('4');
});
});