react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

SimpleForm isDirty is always true if ArrayInput is used

Open AlexDroll opened this issue 1 year ago • 4 comments

What you were expecting:

  • if no input is changed the form should still be not dirty

What happened instead:

  • isDirty property using the useFormState() hook from react-hook-form is set to true
  • for the initial rendering isDirty is still false but becomes true in the following renderings (see console output in the sandbox)
  • the dirtyFields are still empty as expected
  • in the defaultValues the source of the ArrayInput groups disappears after the first rendering (see console output in the sandbox)

Steps to reproduce:

  • open the code sandbox
  • open the console
  • reload the page

Related code:

  • Code sandbox with minimal example: here

Other information:

  • this issue is also related with react-hook-form updates
  • the isDirty worked with [email protected]
  • The issue with the disappearing default value existed already for [email protected]

Environment

  • React-admin version: 4.16.10
  • Last version that did not exhibit the issue (if applicable):
  • React version: 18.2.0
  • Browser: firefox, chrome

AlexDroll avatar Feb 13 '24 13:02 AlexDroll

Reproduced, thanks! Appreciate the great sandbox btw :blush:

Until this is fixed, you can use the same workaround we used in SaveButton.tsx:

    const { dirtyFields, isValidating, isSubmitting } = useFormState();
    // useFormState().isDirty might differ from useFormState().dirtyFields (https://github.com/react-hook-form/react-hook-form/issues/4740)
    const isDirty = Object.keys(dirtyFields).length > 0;

slax57 avatar Feb 15 '24 13:02 slax57

Yes. We use the same workaround. 🙏

Just one hint, this only works partially. If I change one value in the array input and then revert the changes the field is still listed in the dirtyFields. As a consequence also the SaveButton is enabled.

AlexDroll avatar Feb 16 '24 13:02 AlexDroll

I am not sure if I should open separate issue or just comment in this issue. so I will start with a comment and maybe separate it if needed.

I am facing relatively same inconveniences with ArrayInput and dirty behaviour. in my case, I am using zod resolver.

What you were expecting:

  1. the form is not dirty
  2. clicking the plus button adds an element and makes the form dirty
  3. clicking the save button (without properly filling the inputs) the form should report the errors and the save button should stay enabled (as the array input still has an element added and the logic of save button rely on dirty state not validation state)
  4. properly fill the input and click save should successfully submit the form

What happened instead:

https://user-images.githubusercontent.com/1198975/a06ba812-72af-4f2b-8624-fadd249ff55d

Steps to reproduce:

here are the two stackblitz that reproduces the two behaviours on the right side (browser) navigate to /#/posts/create

Other information: note that I fixed the react-hook-form version to 7.50.1 as the next release 7.51.0 is the one that introduced my issue

Nilegfx avatar Mar 22 '24 23:03 Nilegfx

I think this issue on react-hook-form side is related

Nilegfx avatar Mar 26 '24 08:03 Nilegfx