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

add "shouldDirty" to append from useFieldArray #3525

Closed
quertenmont opened this issue Nov 26, 2020 · 6 comments
Closed

add "shouldDirty" to append from useFieldArray #3525

quertenmont opened this issue Nov 26, 2020 · 6 comments
Labels
duplicated duplicated issue

Comments

@quertenmont
Copy link

Is your feature request related to a problem? Please describe.
would it be possible to add the "shouldDirty" flag to the append of useFieldArray ?

The shouldFocus already exist, so I guess it shouldn't be a big deal.
Thanks in advance

@bluebill1049
Copy link
Member

What's the use case?

@quertenmont
Copy link
Author

The use case is typically that I want my form array to always contain X items,
so if this is not the case, I pragmatically append empty items to the form array in a useEffect function.


React.useEffect(() => {
  if (fields.length < 2) {
    append({value:0});
  }
}, fields)

Given that the user has not interacted with the form, it doesn't make sense to have the form marked as dirty.

Thanks in advance
Loic

@bluebill1049 bluebill1049 added the feature request request a feature to be added label Nov 26, 2020
@bluebill1049
Copy link
Member

sounds legit. let's put on the feature board.

@bluebill1049 bluebill1049 added the waiting-up-vote Waiting for votes from the community. label Nov 26, 2020
@bluebill1049 bluebill1049 added this to Pending Feature Requests in React Hook Form Nov 26, 2020
@bluebill1049 bluebill1049 moved this from Pending Feature Requests to API proposal / RFC in React Hook Form Dec 19, 2020
@bluebill1049 bluebill1049 moved this from API proposal / RFC to In progress in React Hook Form Jan 3, 2021
@bluebill1049 bluebill1049 moved this from In progress to Pending Feature Requests in React Hook Form Jan 10, 2021
@bluebill1049
Copy link
Member

After future investigation this is not possible or difficult to implement, because each input gets append/prepend will leave inputs as an empty string, which then fails against deep equal against defaultValues and will result dirty as true, and if we don't enforce such behavior, it will leave inconsistent behavior for the formState.

@bluebill1049 bluebill1049 removed the waiting-up-vote Waiting for votes from the community. label May 29, 2021
@bluebill1049 bluebill1049 removed this from Pending Feature Requests in React Hook Form May 29, 2021
@ezrabowman
Copy link

It would be helpful to be able to add fields to a FieldArray in a non-dirty state.

Sample use case:

  • User has the ability to add sections to my form of the same type of information (i.e. a form capturing multiple addresses).
  • When the form loads, there is one instance of address in the field array
  • User clicks "+ Add Another Address" and I use append to add the new address fields to the FieldArray
  • All new fields are dirty although the user has not interacted with them

Note: This form uses the isDirty property to show the user what field they have changed when editing a record, so all the fields in the new address instance show up as dirty.

I am trying to come up with a work-around using some form of reset, but do not have a solution yet.

Also - @bluebill1049, you rock! Thanks for RHF.

@bluebill1049
Copy link
Member

bluebill1049 commented Jul 11, 2022

thanks for the feedback @ezrabowman

related feature request: #8309 will probably get to it later in the stage, it's not a simple config update. I will mark this as duplicated and track it in the other one.

@bluebill1049 bluebill1049 added duplicated duplicated issue and removed feature request request a feature to be added labels Jul 11, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicated duplicated issue
Projects
None yet
Development

No branches or pull requests

3 participants