Skip to content

Commit 106f4b2

Browse files
authored
fix(Form): reset non-touched fields when form default values change (#564)
1 parent 18b4557 commit 106f4b2

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

.changeset/olive-cows-compare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cube-dev/ui-kit': minor
3+
---
4+
5+
Apply form `defaultValues` change synchronously to avoid inconsistency.

src/components/form/Form/Form.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
forwardRef,
66
ReactNode,
77
useContext,
8-
useEffect,
98
useRef,
109
} from 'react';
1110
import { DOMRef } from '@react-types/shared';
@@ -117,6 +116,7 @@ function Form<T extends FieldTypes>(
117116
onSubmitFailed,
118117
...otherProps
119118
} = props;
119+
const defaultValuesRef = useRef(defaultValues);
120120
const firstRunRef = useRef(true);
121121
const isHorizontal = orientation === 'horizontal';
122122

@@ -224,11 +224,10 @@ function Form<T extends FieldTypes>(
224224
}
225225
}
226226

227-
useEffect(() => {
228-
if (defaultValues) {
229-
form?.setInitialFieldsValue(defaultValues);
230-
}
231-
}, [defaultValues]);
227+
if (defaultValuesRef.current !== defaultValues) {
228+
form?.setInitialFieldsValue(defaultValues ?? {});
229+
defaultValuesRef.current = defaultValues;
230+
}
232231

233232
return (
234233
<FormElement

0 commit comments

Comments
 (0)