Skip to content
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

Unmounting and Mounting of fields with useFieldArray is not working as expected #4115

Closed
2 of 5 tasks
sebastian-ludwig opened this issue Feb 2, 2023 · 2 comments
Closed
2 of 5 tasks
Labels
🐛 bug Unintended behavior

Comments

@sebastian-ludwig
Copy link

What happened?

When I list multiple items using useFieldArray and they are unmounted and then mounted back, the last items is visible again. I would expect the array to be empty again because of default keepValueOnUnmount = false

Reproduction steps

  1. Go to Code-Sandbox
  2. Click "Add" and enter some item data
  3. Click "Add" and enter some other item data
  4. Activate "Disable Fields"-Checkbox
  5. Deactivate "Disable Fields"-Checkbox
  6. Bug => The last item appears again

Version

Vue.js 3.x and vee-validate 4.x

What browsers are you seeing the problem on?

  • Firefox
  • Chrome
  • Safari
  • Microsoft Edge

Relevant log output

No response

Demo link

https://codesandbox.io/s/vee-validate-array-fields-forked-1gq0k7

Code of Conduct

@logaretm logaretm added the 🐛 bug Unintended behavior label Feb 5, 2023
@logaretm
Copy link
Owner

logaretm commented Feb 5, 2023

Thanks for reporting this. This is indeed a bug that happens because the field value deletion tries to delete the value at the old index when it doesn't exist because the earlier values were removed so the last item will always fail deletion.

So I need to figure out a way for fields to track their real-current-index in a form value.

@logaretm
Copy link
Owner

logaretm commented May 9, 2023

This was a tough one to fix, the issue was when a field array item is unset by v-if it removes its own path. However subsequent items' value becomes out of sync with their path name.

The fix is theoretically sound, we batch unsets into one batch but do them in a safe order where removals don't affect other fields' existence. In the case of a field array, we remove the bigger indexes first.

Will patch a release shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants