diff --git a/packages/x/src/DecimalFieldI18n.tsx b/packages/x/src/DecimalFieldI18n.tsx index f08f59d1..1fae42f0 100644 --- a/packages/x/src/DecimalFieldI18n.tsx +++ b/packages/x/src/DecimalFieldI18n.tsx @@ -1,13 +1,7 @@ import React, { createContext, PropsWithChildren } from 'react'; import merge from 'lodash/merge'; -export const defaultFormat = (value: number | null | undefined, precision: number) => { - if (typeof value !== 'number' || !Number.isFinite(value)) { - return ''; - } - - return value.toFixed(precision).toString(); -}; +import { formatDecimal } from './formatDecimal'; export type DecimalFieldI18n = { required: string; @@ -19,8 +13,8 @@ export type DecimalFieldI18n = { export const defaultDecimalFieldI18n: DecimalFieldI18n = { required: 'Field is required', invalidInput: 'Must be decimal', - minValue: (min: number, precision: number) => `Value should not be less than ${defaultFormat(min, precision)}`, - maxValue: (max: number, precision: number) => `Value should not be greater than ${defaultFormat(max, precision)}`, + minValue: (min: number, precision: number) => `Value should not be less than ${formatDecimal(min, precision)}`, + maxValue: (max: number, precision: number) => `Value should not be greater than ${formatDecimal(max, precision)}`, }; export const DecimalFieldI18nContext = createContext(defaultDecimalFieldI18n); diff --git a/packages/x/src/formatDecimal.ts b/packages/x/src/formatDecimal.ts new file mode 100644 index 00000000..8da992d3 --- /dev/null +++ b/packages/x/src/formatDecimal.ts @@ -0,0 +1,7 @@ +export const formatDecimal = (value: number | null | undefined, precision: number) => { + if (typeof value !== 'number' || !Number.isFinite(value)) { + return ''; + } + + return value.toFixed(precision).toString(); +}; diff --git a/packages/x/src/useDecimalField.ts b/packages/x/src/useDecimalField.ts index 7ac9a252..98f42961 100644 --- a/packages/x/src/useDecimalField.ts +++ b/packages/x/src/useDecimalField.ts @@ -1,7 +1,8 @@ import { useCallback, useContext } from 'react'; import { FieldConfig, useFieldValidator } from '@reactive-forms/core'; -import { DecimalFieldI18nContext, defaultFormat } from './DecimalFieldI18n'; +import { DecimalFieldI18nContext } from './DecimalFieldI18n'; +import { formatDecimal } from './formatDecimal'; import { ConversionError, ConverterFieldBag, useConverterField } from './useConverterField'; const DECIMAL_REGEX = /^\d*\.?\d*$/; @@ -58,7 +59,7 @@ export const useDecimalField = ({ const formatValue = useCallback( (value: number | null | undefined) => { - return (format ?? defaultFormat)(value, precision); + return (format ?? formatDecimal)(value, precision); }, [format, precision], ); diff --git a/packages/x/tests/useDecimalField.test.tsx b/packages/x/tests/useDecimalField.test.tsx index df288b19..4235c366 100644 --- a/packages/x/tests/useDecimalField.test.tsx +++ b/packages/x/tests/useDecimalField.test.tsx @@ -2,12 +2,8 @@ import React from 'react'; import { ReactiveFormProvider, useForm } from '@reactive-forms/core'; import { act, renderHook, waitFor } from '@testing-library/react'; -import { - DecimalFieldI18n, - DecimalFieldI18nContextProvider, - defaultDecimalFieldI18n, - defaultFormat, -} from '../src/DecimalFieldI18n'; +import { DecimalFieldI18n, DecimalFieldI18nContextProvider, defaultDecimalFieldI18n } from '../src/DecimalFieldI18n'; +import { formatDecimal } from '../src/formatDecimal'; import { DecimalFieldConfig, defaultPrecision, useDecimalField } from '../src/useDecimalField'; type Config = Omit & { @@ -49,7 +45,7 @@ describe('Decimal field', () => { it('Should format initial value correctly', () => { const [{ result }] = renderUseDecimalField(); - expect(result.current.text).toBe(defaultFormat(0, defaultPrecision)); + expect(result.current.text).toBe(formatDecimal(0, defaultPrecision)); expect(result.current.value).toBe(0); });