Skip to content

Commit 7867864

Browse files
Zneekyrkaraivanov
andauthored
fix(rating): Value rounding calculation when step is not 1 (#1561)
Updated rounding function to round up to step and added a test --------- Co-authored-by: Radoslav Karaivanov <[email protected]>
1 parent 2efd060 commit 7867864

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## Unreleased
8+
### Fixed
9+
- #### Rating
10+
- Inaccurate value rounding calculation when step is not 1 [#1560](https://github.com/IgniteUI/igniteui-webcomponents/issues/1560)
11+
712
## [5.2.1] - 2025-01-23
813
### Added
914
- #### Dialog

src/components/rating/rating.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,19 @@ describe('Rating component', () => {
306306
expect(el.value).to.equal(2.5);
307307
});
308308

309+
it('rounds correctly to the next step, when the step is different than 1', async () => {
310+
const eventSpy = spy(el, 'emitEvent');
311+
el.step = 0.4;
312+
await elementUpdated(el);
313+
314+
const symbol = getRatingSymbols(el).item(0);
315+
const { x, width } = getBoundingRect(symbol);
316+
simulateClick(symbol, { clientX: x + width * 0.55 }); // Click 55% across the width of the symbol
317+
318+
expect(eventSpy).calledOnceWithExactly('igcChange', { detail: 0.8 });
319+
expect(el.value).to.equal(0.8);
320+
});
321+
309322
it('issue-1548 - Inaccurate value calculation when precision == 1', async () => {
310323
const eventSpy = spy(el, 'emitEvent');
311324

src/components/rating/rating.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,16 @@ export default class IgcRatingComponent extends FormAssociatedMixin(
325325
protected calcNewValue(x: number) {
326326
const { width, left, right } = this.container.getBoundingClientRect();
327327
const percent = isLTR(this) ? (x - left) / width : (right - x) / width;
328-
const value = this.round(this.max * percent + this.step / 2);
328+
const value = this.round(this.max * percent);
329329

330330
return clamp(value, this.step, this.max);
331331
}
332332

333333
protected round(value: number) {
334-
return roundPrecise(value, numberOfDecimals(this.step));
334+
return roundPrecise(
335+
Math.ceil(value / this.step) * this.step,
336+
numberOfDecimals(this.step)
337+
);
335338
}
336339

337340
protected clipSymbol(index: number, isLTR = true) {

0 commit comments

Comments
 (0)