From 0a189d7b9df523ee6c0b2e1eeb7f9f043c48719c Mon Sep 17 00:00:00 2001 From: Zhulien Ivanov Date: Sat, 22 Oct 2022 12:15:03 +0300 Subject: [PATCH] [@mantine/core] Rating: Fix count and fractions parameters to accept integers only (#2763) --- src/mantine-core/src/Rating/Rating.tsx | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/mantine-core/src/Rating/Rating.tsx b/src/mantine-core/src/Rating/Rating.tsx index 0cc71382ebc..ce46df0b5a7 100644 --- a/src/mantine-core/src/Rating/Rating.tsx +++ b/src/mantine-core/src/Rating/Rating.tsx @@ -116,7 +116,10 @@ export const Rating = forwardRef((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; @@ -133,7 +136,7 @@ export const Rating = forwardRef((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; @@ -141,7 +144,7 @@ export const Rating = forwardRef((props, ref) => const rounded = clamp( roundValueTo(hoverValue + decimalUnit / 2, decimalUnit), decimalUnit, - count + _count ); setHovered(rounded); @@ -167,11 +170,11 @@ export const Rating = forwardRef((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 (