Skip to content

Commit

Permalink
fix: deep clone of default form values
Browse files Browse the repository at this point in the history
  • Loading branch information
movpushmov committed Nov 12, 2024
1 parent 7a9093e commit 878a236
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/effector-reform-core/lib/form/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function createForm<T extends AnySchema>(options: CreateFormOptions<T>) {
metaChanged,
} = mapSchema(fields);

const $snapshot = createStore({ ...$values.getState() });
const $snapshot = createStore(structuredClone($values.getState()));

const $isChanged = combine(
$values,
Expand Down
20 changes: 19 additions & 1 deletion packages/effector-reform-core/tests/form/form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ describe('Form tests', () => {
expect(watchedEvent).toBeCalledTimes(1);
});

test('isChanged triggers when changed subfield in array field', async () => {
test('isChanged triggers when changed subfield in empty array field', async () => {
const scope = fork();
const form = createForm({
schema: {
Expand All @@ -744,5 +744,23 @@ describe('Form tests', () => {
'After item changed isChanged must be true',
).toBeTruthy();
});

test('isChanged triggers when changed subfield in filled array field', async () => {
const scope = fork();
const form = createForm({
schema: {
arr: createArrayField<{ name: string }>([{ name: '' }]),
},
});

const item = scope.getState(form.fields.arr.$values)[0];

await allSettled(item.name.change, { scope, params: 'Edward' });

expect(
scope.getState(form.$isChanged),
'After item changed isChanged must be true',
).toBeTruthy();
});
});
});

0 comments on commit 878a236

Please sign in to comment.