-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: resetField respects current reactive initial value #5137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "vee-validate": patch | ||
| --- | ||
|
|
||
| Fix resetField to use current reactive initial value instead of stale value (#4827) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -126,6 +126,11 @@ export function _useFieldValue<TValue = unknown>( | |||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function resolveInitialValue() { | ||||||||||||||||||||||||||||
| if (!form) { | ||||||||||||||||||||||||||||
| // If the original modelValue is a ref, read from it directly to maintain reactivity | ||||||||||||||||||||||||||||
| if (isRef(modelValue)) { | ||||||||||||||||||||||||||||
| return modelValue.value as TValue; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return unref(modelRef) as TValue; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -160,6 +165,15 @@ export function _useFieldValue<TValue = unknown>( | |||||||||||||||||||||||||||
| // #3429 | ||||||||||||||||||||||||||||
| const currentValue = resolveModelValue(modelValue, form, initialValue, path); | ||||||||||||||||||||||||||||
| form.stageInitialValue(unref(path), currentValue, true); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // If the modelValue is a ref, watch it and update the form's initial value when it changes | ||||||||||||||||||||||||||||
| // This ensures resetField uses the current reactive initial value (#4827) | ||||||||||||||||||||||||||||
| if (isRef(modelValue)) { | ||||||||||||||||||||||||||||
| watch(modelValue, newVal => { | ||||||||||||||||||||||||||||
| form.setFieldInitialValue(unref(path), newVal as any, true); | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
Comment on lines
+172
to
+174
|
||||||||||||||||||||||||||||
| watch(modelValue, newVal => { | |
| form.setFieldInitialValue(unref(path), newVal as any, true); | |
| }); | |
| watch( | |
| modelValue, | |
| newVal => { | |
| form.setFieldInitialValue(unref(path), newVal as any, true); | |
| }, | |
| { | |
| flush: 'sync', | |
| deep: true, | |
| } | |
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor type-safety: inside the watcher callback
newValshould already be typed asTValue, andsetFieldInitialValueacceptsunknown, so theas anycast is unnecessary and hides potential typing issues. Prefer passingnewValdirectly (oras TValueif needed).