react-jsonschema-form
react-jsonschema-form copied to clipboard
Is there some way of knowing when Default values from schema are loaded?
Prerequisites
- [X] I have read the documentation
What theme are you using?
antd
What is your question?
onChange event gets fired but there is no differentiation that i can see between a user updating the form and default values being loaded. wanting to save the initial formData to compare against when the user changes the form data thanks for your help ;)
@brada11en You are right there is not easily discernable way to know whether an onChange was the result of a user entering the data and the default logic kicking in an specifying defaults. Upon the first render with empty form data and defaults in the schema, one would get an onChange that was the result of the defaults being added. But if one is using conditions to expand chunks of the schema based on a user's answer, then it is likely that there will be 2 onChange() calls, one for the user's data change and a second to notify of the additional default values added. In that situation, the first onChange() will get an id for the field the user changes and the second onChange() will NOT get an id. So it may be possible for you to trigger off of whether onChange() gets the optional id passed to it to detect human interactions vs. defaults.
hey @heath-freenome thanks for your detailed response, appreciate your time.. i tried the retrieve the id from an onChange by the user but the id prop is always undefined. i created a new test vite app with the latest form etc and got it working, which indicates its something with my code.
im using https://www.npmjs.com/package/rjsf-conditionals - i guess its that? not sure why this would stop working though as the formWithConditionals is passing in a RJSForm instance when applying rules.
have you done any work with this package? how is everyone else dealing with showing and hiding fields based on formData? i found oneof/ anyof etc to be really painful to use so i went down this path. is there another way these days other than writing logic to manipulate the schama json on the fly? have you thought about incorporating rjsf-conditionals at all?
thanks again
edit: ive found the issue in rjsf-conditionals, id not being passed thru the onChange handler locally to the form onChange prop.
still interested in other ways of handling this
@ba11en The biggest issue I see is that the rjsf-conditionals package was last updated with @rjsf/[email protected] and we are on 5.x, so a LOT has changed. You may reach out to the author and ask them to upgrade to 5.x and maybe things will work for you. Or you can try forking that repo and doing it yourself. Beyond that, I don't know what else to suggest.
@heath-freenome the package im using supports v5, and is the version im running. just in this case it wasnt passing the id through. https://ivarprudnikov.github.io/rjsf-conditionals/
im a big fan of this package. how are conditonals being done generally withtout the use of this package?
Most people use the JSON Schema oneOf/anyOf, if-then-else or dependencies I believe
@ba11en If you look at the source code for applyRules You'll see that the onChange() provided does not pass through the additional props that our onChange() handler returns. So there is your reason why you don't get the id. Might I suggest forking that repo and making an update to the function.
@heath-freenome thats exactly what ive done and is working well
one thing i noticed is that a boolean field has 2 onChange events when a user changes the checkbox value. first one with an id and second event without an id
thanks again for your time on this