Skip to content

Commit

Permalink
remove reset on unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
honzabrecka committed Nov 27, 2023
1 parent 5b61fe8 commit 48bf4ea
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 76 deletions.
62 changes: 0 additions & 62 deletions __tests__/ui.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Form>
<Field
name="name"
label="Name"
key={key}
initialValue="John"
preserveStateAfterUnmount={false}
/>
<button type="submit">submit</button>
<button type="button" onClick={() => setKey((k) => k + 1)}>
change key
</button>
</Form>
);
};

render(<App />, { 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 = () => {
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ export type UseFieldProps = {
to?: ToTransformer;
required?: boolean;
dirtyComparator?: DirtyComparator;
preserveStateAfterUnmount?: boolean;
};

export type UseFieldResult = {
Expand All @@ -211,7 +210,6 @@ export type UseFieldResult = {
export type UseListProps = FieldIdentification & {
initialValue?: Dict<any>[];
dirtyComparator?: DirtyComparator;
preserveStateAfterUnmount?: boolean;
};

export type MappedFieldProp = {
Expand Down
6 changes: 0 additions & 6 deletions src/useField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
// Loadable,
useRecoilState,
useRecoilValueLoadable,
useResetRecoilState,
} from './minimalRecoil';
import {
fieldId,
Expand Down Expand Up @@ -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);

Expand All @@ -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)),
);
Expand Down Expand Up @@ -147,9 +144,6 @@ export default function useField({
setInited(true);

return () => {
if (!preserveStateAfterUnmount) {
reset();
}
registration.remove([name]);
};
}, []);
Expand Down
6 changes: 0 additions & 6 deletions src/useList.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useEffect, useMemo, useState } from 'react';
import {
useRecoilState,
useResetRecoilState,
useRecoilCallback,
/* eslint-disable-next-line camelcase */
useRecoilTransaction_UNSTABLE,
Expand Down Expand Up @@ -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);

Expand All @@ -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<Dict<any>>({});

Expand Down Expand Up @@ -244,9 +241,6 @@ const useList = ({
}

return () => {
if (!preserveStateAfterUnmount) {
reset();
}
registration.remove([name]);
};
}, []);
Expand Down

0 comments on commit 48bf4ea

Please sign in to comment.