Skip to content

Commit 428d044

Browse files
committed
fix(component-library): round number field step values
1 parent 63df47b commit 428d044

3 files changed

Lines changed: 49 additions & 6 deletions

File tree

.changeset/bright-kings-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@shopware-ag/meteor-component-library": patch
3+
---
4+
5+
Fix floating-point rounding after stepping a number field.

packages/component-library/src/components/mt-number-field/mt-number-field.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,43 @@ describe("mt-number-field", () => {
9090
expect(handler).not.toHaveBeenCalled();
9191
});
9292

93+
it("rounds floating point precision errors when incrementing with the keyboard", async () => {
94+
const updateHandler = vi.fn();
95+
96+
render(MtNumberField, {
97+
props: {
98+
modelValue: 1.62,
99+
step: 0.01,
100+
digits: 2,
101+
"onUpdate:modelValue": updateHandler,
102+
},
103+
});
104+
105+
await userEvent.click(screen.getByRole("textbox"));
106+
await userEvent.keyboard("{ArrowUp}");
107+
108+
expect(updateHandler).toHaveBeenLastCalledWith(1.63);
109+
expect((screen.getByRole("textbox") as HTMLInputElement).value).toBe("1.63");
110+
});
111+
112+
it("rounds floating point precision errors when incrementing with the control", async () => {
113+
const updateHandler = vi.fn();
114+
115+
render(MtNumberField, {
116+
props: {
117+
modelValue: 1.62,
118+
step: 0.01,
119+
digits: 2,
120+
"onUpdate:modelValue": updateHandler,
121+
},
122+
});
123+
124+
await userEvent.click(screen.getByRole("button", { name: "Increase" }));
125+
126+
expect(updateHandler).toHaveBeenLastCalledWith(1.63);
127+
expect((screen.getByRole("textbox") as HTMLInputElement).value).toBe("1.63");
128+
});
129+
93130
it("is not possible to increment the value by pressing the increment button when inheritance is linked", async () => {
94131
// ASSERT
95132
const handler = vi.fn();

packages/component-library/src/components/mt-number-field/mt-number-field.vue

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,16 @@ export default defineComponent({
380380
},
381381
382382
increaseNumberByStep() {
383-
this.computeValue((Number(this.currentValue) + this.realStep).toString());
384-
this.rawUserInput = null;
385-
386-
this.$emit("update:modelValue", this.currentValue);
383+
this.changeNumberByStep(this.realStep);
387384
},
388385
389386
decreaseNumberByStep() {
390-
// @ts-expect-error - wrong type because of component extends
391-
this.computeValue((this.currentValue - this.realStep).toString());
387+
this.changeNumberByStep(-this.realStep);
388+
},
389+
390+
changeNumberByStep(step: number) {
391+
const steppedValue = Number(this.currentValue) + step;
392+
this.computeValue(this.roundToDigits(steppedValue.toString(), this.digits).toString());
392393
this.rawUserInput = null;
393394
394395
this.$emit("update:modelValue", this.currentValue);

0 commit comments

Comments
 (0)