Skip to content

Commit

Permalink
fix(Form): reset non-touched fields when form default values change (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tenphi authored Jan 10, 2025
1 parent 18b4557 commit 106f4b2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-cows-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cube-dev/ui-kit': minor
---

Apply form `defaultValues` change synchronously to avoid inconsistency.
11 changes: 5 additions & 6 deletions src/components/form/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
forwardRef,
ReactNode,
useContext,
useEffect,
useRef,
} from 'react';
import { DOMRef } from '@react-types/shared';
Expand Down Expand Up @@ -117,6 +116,7 @@ function Form<T extends FieldTypes>(
onSubmitFailed,
...otherProps
} = props;
const defaultValuesRef = useRef(defaultValues);
const firstRunRef = useRef(true);
const isHorizontal = orientation === 'horizontal';

Expand Down Expand Up @@ -224,11 +224,10 @@ function Form<T extends FieldTypes>(
}
}

useEffect(() => {
if (defaultValues) {
form?.setInitialFieldsValue(defaultValues);
}
}, [defaultValues]);
if (defaultValuesRef.current !== defaultValues) {
form?.setInitialFieldsValue(defaultValues ?? {});
defaultValuesRef.current = defaultValues;
}

return (
<FormElement
Expand Down

0 comments on commit 106f4b2

Please sign in to comment.