Skip to content

Commit

Permalink
[@mantine/core] Rating: Fix count and fractions parameters to accept …
Browse files Browse the repository at this point in the history
…integers only (#2763)
  • Loading branch information
zhulien-ivanov committed Oct 22, 2022
1 parent 4fa634e commit 0a189d7
Showing 1 changed file with 8 additions and 5 deletions.
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

0 comments on commit 0a189d7

Please sign in to comment.