From a6e9d71a092d2330d437acd4127a6dc86f28b03f Mon Sep 17 00:00:00 2001 From: Aleksandras Sukelovic Date: Sun, 10 Sep 2023 16:46:46 +0300 Subject: [PATCH] Removed `formatter` prop --- packages/x/src/useStringField.ts | 21 +++++---------------- packages/x/tests/useStringField.test.tsx | 15 --------------- 2 files changed, 5 insertions(+), 31 deletions(-) diff --git a/packages/x/src/useStringField.ts b/packages/x/src/useStringField.ts index 31aabe0c..c0c1da65 100644 --- a/packages/x/src/useStringField.ts +++ b/packages/x/src/useStringField.ts @@ -1,3 +1,4 @@ +import { useCallback } from 'react'; import { FieldConfig, FieldContext, useField, useFieldValidator } from '@reactive-forms/core'; import isFunction from 'lodash/isFunction'; @@ -10,8 +11,6 @@ export const defaultErrors = { export type ErrorTuple = [value: T, message: string | ((value: T) => string)]; export type StringFieldConfig = FieldConfig & { - formatter?: (value: string) => string; - required?: boolean | string; minLength?: number | ErrorTuple; maxLength?: number | ErrorTuple; @@ -21,20 +20,11 @@ export type StringFieldBag = FieldContext & { onBlur: () => void; }; -export const useStringField = ({ - name, - validator, - schema, - formatter = (val) => val, - required, - maxLength, - minLength, -}: StringFieldConfig) => { +export const useStringField = ({ name, validator, schema, required, maxLength, minLength }: StringFieldConfig) => { const fieldBag = useField({ name, validator, schema }); const { - control: { setTouched, setValue }, - value, + control: { setTouched }, } = fieldBag; useFieldValidator({ @@ -76,10 +66,9 @@ export const useStringField = ({ }, }); - const onBlur = () => { + const onBlur = useCallback(() => { setTouched({ $touched: true }); - setValue(formatter(value ?? '')); - }; + }, [setTouched]); return { onBlur, diff --git a/packages/x/tests/useStringField.test.tsx b/packages/x/tests/useStringField.test.tsx index 205bfa0a..5adbbdb9 100644 --- a/packages/x/tests/useStringField.test.tsx +++ b/packages/x/tests/useStringField.test.tsx @@ -270,19 +270,4 @@ describe('String field', () => { expect(result.current.meta.error?.$error).toBe('custom'); }); }); - - it('Should set formatted value in form state on blur', async () => { - const [{ result }] = renderUseStringField({ - formatter: (value) => `+${value}`, - initialValue: 'hello', - }); - - await act(() => { - result.current.onBlur(); - }); - - await waitFor(() => { - expect(result.current.value).toBe('+hello'); - }); - }); });