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

[@mantine/core] Rating: Fix count and fractions parameters to accept integers only. #2763

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/mantine-core/src/Rating/Rating.tsx
Expand Up @@ -116,7 +116,10 @@ export const Rating = forwardRef<HTMLInputElement, RatingProps>((props, ref) =>
const [hovered, setHovered] = useState(-1);
const [isOutside, setOutside] = useState(true);

const decimalUnit = 1 / fractions;
const _fractions = Math.floor(fractions);
const _count = Math.floor(count);

const decimalUnit = 1 / _fractions;
const stableValueRounded = roundValueTo(_value, decimalUnit);
const finalValue = hovered !== -1 ? hovered : stableValueRounded;

Expand All @@ -133,15 +136,15 @@ export const Rating = forwardRef<HTMLInputElement, RatingProps>((props, ref) =>
}

const { left, right, width } = rootRef.current.getBoundingClientRect();
const symbolWidth = width / count;
const symbolWidth = width / _count;

const hoverPosition = theme.dir === 'rtl' ? right - event.clientX : event.clientX - left;
const hoverValue = hoverPosition / symbolWidth;

const rounded = clamp(
roundValueTo(hoverValue + decimalUnit / 2, decimalUnit),
decimalUnit,
count
_count
);

setHovered(rounded);
Expand All @@ -167,11 +170,11 @@ export const Rating = forwardRef<HTMLInputElement, RatingProps>((props, ref) =>
setValue(resultedValue);
};

const items = Array(count)
const items = Array(_count)
.fill(0)
.map((_, index) => {
const integerValue = index + 1;
const fractionItems = Array.from(new Array(index === 0 ? fractions + 1 : fractions));
const fractionItems = Array.from(new Array(index === 0 ? _fractions + 1 : _fractions));
const isGroupActive = !readOnly && Math.ceil(hovered) === integerValue;

return (
Expand Down