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

Handling dependent form fields #16

Closed
spidergears opened this issue Jul 11, 2021 · 4 comments
Closed

Handling dependent form fields #16

spidergears opened this issue Jul 11, 2021 · 4 comments

Comments

@spidergears
Copy link

spidergears commented Jul 11, 2021

Is there a way to handle dependent form fields?
Examples:

  1. Show or hide fields based on the value from a select field
  2. Updating options for states selection based on country selected

Any suggestions or workarounds?

@dgonz64
Copy link
Owner

dgonz64 commented Jul 12, 2021

It's not implemented yet and I don't come up with non convoluted workarounds. I'm thinking about enabling a new attribute in the schema called something like onChange that will allow to update the schema for that field. Let me study it and I will get back to you.

@dgonz64
Copy link
Owner

dgonz64 commented Jul 25, 2021

Sorry for the delay. After a careful study of the problem, I thought the cleanest way was to update to React Hook Form 7 and intercept react-hook-form onChange.

Following 1.3.0 release you can use the onChange attribute in the schemas. It will receive the value and a context object that allows you to change form state. Find the example and documentation here.

If you made your own skin or used skinOverride, you will need to make some changes in your skin code. Sorry about that, I hope this migration guide helps.

This implementation is still experimental (even lacks tests), so please, tell me about your experience with it or if you encounter any problems.

@spidergears
Copy link
Author

Hi thanks for your response. This surely helps.

Does setting field visibility setVisible to false only hides the fields from render or does it also remove the field from DOM and formState?

If not, does the lib currently provide any other helpers to add/remove formFields dynamically

@dgonz64
Copy link
Owner

dgonz64 commented Sep 6, 2021

Hello and thanks to you!

Following 1.3.5 version, already published, setVisible makes the field disappear from submitted doc when not visible.

It doesn't remove the field from the state but hides its value while coercing (always done when submitting).

@dgonz64 dgonz64 closed this as completed Apr 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants