Skip to content

Commit

Permalink
Removed formatter prop
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexShukel committed Sep 10, 2023
1 parent f6c637a commit a6e9d71
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 31 deletions.
21 changes: 5 additions & 16 deletions packages/x/src/useStringField.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useCallback } from 'react';
import { FieldConfig, FieldContext, useField, useFieldValidator } from '@reactive-forms/core';
import isFunction from 'lodash/isFunction';

Expand All @@ -10,8 +11,6 @@ export const defaultErrors = {
export type ErrorTuple<T> = [value: T, message: string | ((value: T) => string)];

export type StringFieldConfig = FieldConfig<string | undefined | null> & {
formatter?: (value: string) => string;

required?: boolean | string;
minLength?: number | ErrorTuple<number>;
maxLength?: number | ErrorTuple<number>;
Expand All @@ -21,20 +20,11 @@ export type StringFieldBag = FieldContext<string | undefined | null> & {
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({
Expand Down Expand Up @@ -76,10 +66,9 @@ export const useStringField = ({
},
});

const onBlur = () => {
const onBlur = useCallback(() => {
setTouched({ $touched: true });
setValue(formatter(value ?? ''));
};
}, [setTouched]);

return {
onBlur,
Expand Down
15 changes: 0 additions & 15 deletions packages/x/tests/useStringField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit a6e9d71

Please sign in to comment.