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

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

Open
1 task done
michal-kurz opened this issue Mar 15, 2024 · 4 comments
Open
1 task done
Labels

Comments

@michal-kurz
Copy link
Contributor

michal-kurz commented Mar 15, 2024

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 michal-kurz added needs triage Initial label given, to be assigned correct labels and assigned question labels Mar 15, 2024
@heath-freenome
Copy link
Member

@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 heath-freenome removed the needs triage Initial label given, to be assigned correct labels and assigned label Mar 15, 2024
@michal-kurz
Copy link
Contributor Author

@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 :)

@heath-freenome
Copy link
Member

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

@nickgros
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants