From 8a0b044448e9282503bb46207b63a7ea6a6f6253 Mon Sep 17 00:00:00 2001 From: Daria Larionova Date: Tue, 14 Jan 2025 14:13:12 +0300 Subject: [PATCH] fix(NumberInput): disable rounding decimal to integer without passed step --- src/components/NumberInput/NumberInput.tsx | 4 ++-- .../NumberInput/__tests__/NumberInput.test.tsx | 12 ++++++++++++ src/components/NumberInput/utils.ts | 4 ++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/components/NumberInput/NumberInput.tsx b/src/components/NumberInput/NumberInput.tsx index 7500a36d70..2ee0e7cd85 100644 --- a/src/components/NumberInput/NumberInput.tsx +++ b/src/components/NumberInput/NumberInput.tsx @@ -89,7 +89,7 @@ export const NumberInput = React.forwardRef(f min: externalMin, max: externalMax, shiftMultiplier: externalShiftMultiplier = 10, - step: externalStep = 1, + step: externalStep, size = 'm', view = 'normal', disabled, @@ -136,7 +136,7 @@ export const NumberInput = React.forwardRef(f }); }, [value]); - const clamp = true; + const clamp = !(allowDecimal && !externalStep); const safeValue = value ?? 0; diff --git a/src/components/NumberInput/__tests__/NumberInput.test.tsx b/src/components/NumberInput/__tests__/NumberInput.test.tsx index d365595b55..4f561b3fd6 100644 --- a/src/components/NumberInput/__tests__/NumberInput.test.tsx +++ b/src/components/NumberInput/__tests__/NumberInput.test.tsx @@ -223,6 +223,18 @@ describe('NumberInput input', () => { expect(handleUpdate).toHaveBeenLastCalledWith(6); }); + it('does not clamp decimal without defined step on blur', async () => { + const handleUpdate = jest.fn(); + render(); + const input = getInput(); + + act(() => { + input.focus(); + input.blur(); + }); + + expect(getInput()).toHaveValue('7.123'); + }); it('swaps min/max if max < min', async () => { const handleUpdate = jest.fn(); render(); diff --git a/src/components/NumberInput/utils.ts b/src/components/NumberInput/utils.ts index d8afacf30d..5c3b8752f4 100644 --- a/src/components/NumberInput/utils.ts +++ b/src/components/NumberInput/utils.ts @@ -59,7 +59,7 @@ function roundIfNecessary(value: number, allowDecimal: boolean) { interface VariablesProps { min: number | undefined; max: number | undefined; - step: number; + step: number | undefined; shiftMultiplier: number; value: number | null | undefined; defaultValue: number | null | undefined; @@ -76,7 +76,7 @@ export function getInternalState(props: VariablesProps): { const { min: externalMin, max: externalMax, - step: externalStep, + step: externalStep = 1, shiftMultiplier: externalShiftMultiplier, value: externalValue, allowDecimal,