From 48bf4ea0820e94ed74f99c00fe2d82a519992aa4 Mon Sep 17 00:00:00 2001 From: Jan Brecka Date: Mon, 27 Nov 2023 17:39:42 +0100 Subject: [PATCH] remove reset on unmount --- __tests__/ui.test.tsx | 62 ------------------------------------------- src/types.ts | 2 -- src/useField.tsx | 6 ----- src/useList.tsx | 6 ----- 4 files changed, 76 deletions(-) diff --git a/__tests__/ui.test.tsx b/__tests__/ui.test.tsx index 224a7ef..6ed18b6 100644 --- a/__tests__/ui.test.tsx +++ b/__tests__/ui.test.tsx @@ -507,68 +507,6 @@ test('forms: field state is preserved in between mounts', async () => { }); }); -test('forms: field state is not preserved in between mounts with flag', async () => { - const onSubmit = jest.fn(); - const App = () => { - const [key, setKey] = useState(1); - const { Form } = useForm({ - onSubmit, - }); - return ( -
- - - - - ); - }; - - render(, { wrapper }); - - const user = userEvent.setup(); - - await user.type(screen.getByLabelText('Name'), ' Doe'); - await user.click(screen.getByText('submit')); - - await waitFor(() => { - expect(onSubmit).toHaveBeenCalledTimes(1); - expectBag(onSubmit.mock.calls[0][0], { - fieldIds: ['name'], - touched: true, - touchedFieldIds: new Set(['name']), - initialValues: { name: 'John' }, - values: { name: 'John Doe' }, - dirty: true, - validation: { isValid: true, isValidStrict: true }, - }); - }); - - await user.click(screen.getByText('change key')); - - await user.click(screen.getByText('submit')); - - await waitFor(() => { - expect(onSubmit).toHaveBeenCalledTimes(2); - expectBag(onSubmit.mock.calls[1][0], { - fieldIds: ['name'], - touched: false, - touchedFieldIds: new Set(), - initialValues: { name: 'John' }, - values: { name: 'John' }, - dirty: false, - validation: { isValid: true, isValidStrict: true }, - }); - }); -}); - test('forms: field state is reset', async () => { const onSubmit = jest.fn(); const App = () => { diff --git a/src/types.ts b/src/types.ts index 02151f8..ae726af 100644 --- a/src/types.ts +++ b/src/types.ts @@ -188,7 +188,6 @@ export type UseFieldProps = { to?: ToTransformer; required?: boolean; dirtyComparator?: DirtyComparator; - preserveStateAfterUnmount?: boolean; }; export type UseFieldResult = { @@ -211,7 +210,6 @@ export type UseFieldResult = { export type UseListProps = FieldIdentification & { initialValue?: Dict[]; dirtyComparator?: DirtyComparator; - preserveStateAfterUnmount?: boolean; }; export type MappedFieldProp = { diff --git a/src/useField.tsx b/src/useField.tsx index 4963b52..d81cda2 100644 --- a/src/useField.tsx +++ b/src/useField.tsx @@ -3,7 +3,6 @@ import { // Loadable, useRecoilState, useRecoilValueLoadable, - useResetRecoilState, } from './minimalRecoil'; import { fieldId, @@ -58,7 +57,6 @@ export default function useField({ to = identity, // NOTE: should be unchanged - used only when initializing, any other update has no effect dirtyComparator, - preserveStateAfterUnmount = true, }: UseFieldProps): UseFieldResult { const formId = useFormId(formIdProp); @@ -72,7 +70,6 @@ export default function useField({ const [fieldState, setFieldState] = useRecoilState( $field(fieldId(formId, name)), ); - const reset = useResetRecoilState($field(fieldId(formId, name))); const validationResult = useRecoilValueLoadable( $fieldValidation(fieldId(formId, name)), ); @@ -147,9 +144,6 @@ export default function useField({ setInited(true); return () => { - if (!preserveStateAfterUnmount) { - reset(); - } registration.remove([name]); }; }, []); diff --git a/src/useList.tsx b/src/useList.tsx index 34f32ba..b2db8db 100644 --- a/src/useList.tsx +++ b/src/useList.tsx @@ -1,7 +1,6 @@ import { useEffect, useMemo, useState } from 'react'; import { useRecoilState, - useResetRecoilState, useRecoilCallback, /* eslint-disable-next-line camelcase */ useRecoilTransaction_UNSTABLE, @@ -36,7 +35,6 @@ const useList = ({ initialValue = emptyArray, // should be unchanged - used only when initializing, any other update has no effect dirtyComparator, - preserveStateAfterUnmount = true, }: UseListProps): UseListResult => { const formId = useFormId(formIdProp); @@ -46,7 +44,6 @@ const useList = ({ const [fieldState, setFieldState] = useRecoilState( $field(fieldId(formId, name)), ); - const reset = useResetRecoilState($field(fieldId(formId, name))); const registration = useFieldRegistration(formId); const [initialValueByName, setInitialValueByName] = useState>({}); @@ -244,9 +241,6 @@ const useList = ({ } return () => { - if (!preserveStateAfterUnmount) { - reset(); - } registration.remove([name]); }; }, []);