skorphil / nextjs-form

Educational NextJs project to build a complex mobile-first form
https://www.chromatic.com/library?appId=65b2882ac1586a6b21cba0dd
MIT License
3 stars 0 forks source link

update institution scenario #47

Closed skorphil closed 7 months ago

skorphil commented 7 months ago

When value inside Institution differs from defaultValues, the label should appear on the InstitutionTab.

Image


Probably can be achieved with built-in use-hook-form features like dirty state

skorphil commented 7 months ago
institutions: [
   {
      name:"City Bank",
      assets: [
        {1}
        {2}
      ]
   },
{
      name:"Bank of America",
      assets: [
        {1}
        {2} // removing or adding asset will result in all `institutions.X` being dirty
      ]
   }
]

however, all individual fields inside institutions.X are not dirty (when checked with dirtyFields)

skorphil commented 7 months ago

https://www.reddit.com/r/react/comments/1ax7bf4/usehookform_how_to_get_dirty_state_of_part_of/

It is not possible to just iterate thru dirtyFields because this way it will not check if fields being added deleted

I'm stuck

skorphil commented 7 months ago

Well, seems like it is possible to solve this by deep comparison of getValues(institutions[x]) and defaultValues(institutions[x])

skorphil commented 7 months ago

Finally, implement this scenario by comparing defaultValues and current Values

const institutionDefaultValues =
      formState.defaultValues.institutions[
        getInstitutionIndex(institutionName)
      ];
    const institutionCurrentValues = getValues(institutionName);
    const isChanged =
      JSON.stringify(institutionDefaultValues) !==
      JSON.stringify(institutionCurrentValues);
skorphil commented 6 months ago

Prob the problem can be fixed by replacing numbers with a strings https://discord.com/channels/754891658327359538/1215263704233414727