rjsf-team / react-jsonschema-form

A React component for building Web forms from JSON Schema.
https://rjsf-team.github.io/react-jsonschema-form/
Apache License 2.0
14.38k stars 2.2k forks source link

When I add extraError prop in react, the form refreshes its state #4355

Closed inspiring71 closed 4 weeks ago

inspiring71 commented 1 month ago

Prerequisites

What theme are you using?

core

Version

5.x

Current Behavior

When the extraError prop is passed to the Form component, any updates to extraError cause the library to remove all dynamically added array fields and refresh the UI components unexpectedly.

https://github.com/user-attachments/assets/09ded61c-f741-4714-bd82-15c2c786af95

You can find the check the example code here: https://codesandbox.io/p/devbox/2gvwxk

Expected Behavior

I expect the fields to remain on the page and not disappear.

Steps To Reproduce

  1. Pass the extraError prop to the Form component.
  2. Provide a schema with an array.
  3. Add a couple of array elements
  4. Update the extraError value.
  5. Observe that all previously added array fields are removed, and the UI refreshes.

Environment

- OS: Mac and Ubuntu
- Node: 22.2
- npm: 10.7

Anything else?

I was trying to implement this feature when face with this error: https://github.com/rjsf-team/react-jsonschema-form/issues/617#issuecomment-1003141946

Also, Let me thank you for all the hard works, I know it is not easy to be an OSS developer.

BTW, I am willing to fix the issue myself but as I am new to this repo, needs some pointers on where this problem is originated.

abdalla-rko commented 4 weeks ago

Hi @inspiring71, the form refreshes its state because it rerenders when you call setErrors() in blurHandler and the formData is not passed as a prop, that's why you lose that state. The formData needs to be passed as a prop so that when you rerender the form doesn't think you have cleared the formData . I have changed the code in your example to make it work, click here to see it.

inspiring71 commented 4 weeks ago

Thanks @abdalla-rko , both for the tip and the attached sandbox. As you know your ways around this library better than me, I was wondering if there is any straightforward way to get the field path of an input when onBlur event is called?

I found a hack that works fine for now, but it seems like finding the field path of the element that invoked the event should be more straight forward. Any idea?

heath-freenome commented 4 weeks ago

Thanks @abdalla-rko , both for the tip and the attached sandbox. As you know your ways around this library better than me, I was wondering if there is any straightforward way to get the field path of an input when onBlur event is called?

I found a hack that works fine for now, but it seems like finding the field path of the element that invoked the event should be more straight forward. Any idea?

@inspiring71 you can use the idSeparator prop on the form to change to use a . and then you can split on . to get the field path array that you can use in lodash.

Also, all of the onBlur() callbacks should be passing in the field id as the first parameter.

inspiring71 commented 4 weeks ago

Thanks for the response @heath-freenome . I will try idSeparator and I think it would simplify what I am doing drastically. I will close this issue now.