react-jsonschema-form icon indicating copy to clipboard operation
react-jsonschema-form copied to clipboard

How to figure out the changed `formData` object property from inside `onChange` callback?

Open michal-kurz opened this issue 1 year ago • 5 comments

Prerequisites

What theme are you using?

core

What is your question?

New formData provided on the first parameter of onChange form handler seems to be an edited deep copy of the previous state (unchanged formData properties do not retain reference). I need to work around this to live-detect state changes between initial and current formData states with reasonable performance on large forms.

This comment claims to have solved the issue, but seems like a rather bloaty approach.

This comment seems to claim that the id prop provided as a second argument should be able to solve this.

I attempted to parse the changed formData property name like this:

 <Form 
     onChange={ ({ formData }, id) => {
          const changedField = id?.split(ID_SEPARATOR)[1]  
          // ...
     }}
  />

And it doest the trick most of the time, but on further testing I found out that when I call change the value in an anyOf select on a property named validator, I get this id in the callback:

// idSeparator === "---"
root---validator__anyof_select

which results in the incorrect property being updated in my state (validator__anyof_select, when the property is simply validator).

I could easily check and adjust for the __anyof_select suffix, but I'm not sure what other cases I can encounter.

Is this documented somewhere? How can I reliably parse the actual formData object property name?

Thank you very much!

michal-kurz avatar Mar 15 '24 14:03 michal-kurz

@michal-kurz The __anyof_select and __oneof_select are components added for anyOf and oneOfs. Those are the dropdowns. Besides that I'm not sure you need to worry about anything else. Maybe you can join next weeks meeting on Friday at noon Pacific time to talk about this?

heath-freenome avatar Mar 15 '24 20:03 heath-freenome

@heath-freenome Thank you very much! And thank you for the invite. I would like to join, but I'll likely be busy this Friday. I should be able to join same time next week, though :)

michal-kurz avatar Mar 20 '24 12:03 michal-kurz

@michal-kurz I believe that @nickgros will be there next Friday... I am on vacation

heath-freenome avatar Mar 21 '24 19:03 heath-freenome

I'll also be out on 3/29, so we will probably cancel that meeting. Should be back to normal the following week though

nickgros avatar Mar 22 '24 19:03 nickgros

@michal-kurz We are still waiting for you to join a meeting. Maybe next week?

heath-freenome avatar Jul 05 '24 19:07 heath-freenome