Schema dependencies with Boolean field
Prerequisites
- [X] I have searched the existing issues
- [X] I understand that providing a SSCCE example is tremendously useful to the maintainers.
- [X] I have read the documentation
- [X] Ideally, I'm providing a sample JSFiddle, Codesandbox.io or preferably a shared playground link demonstrating the issue.
What theme are you using?
core
Version
5.13.6
Current Behavior
I use boolean field enabled to render dependent field billing address.
When checked, it works fine. But when unchecked, the billing address is still rendered.
Expected Behavior
I would expect that billing address disappears, the same way as it happens if the enabled field is a string.
This way, when text in the field is deleted, the dependent field (billing address) disappears.
I have tried to define enabled as
"enabled": {
"type": ["boolean", "null"],
"default": null,
"enum": [null, true]
}
but no luck.
Steps To Reproduce
Environment
- OS:
- Node:
- npm:
Anything else?
If you say this is not a bug, then please suggest a workaround.
Are you ok with an if-then as shown via this playground link?
I would be, yes, but I overly simplified my use case.
I have dependent subschema inside allOf
Please see this playground link
The same structure works well with enabled as a string field with dependencies instead of if-then.
playground link
Can you use strings instead? The way RJSF does booleans will always set a value to true/false once data is provided. To make it support the indeterminate/undefined state would be a new feature.
Thanks for the suggestion.
I could have enabled as a string but in order to make sense, it has to take values from the enum and render as a select box. But in that case, I am back to the original boolean problem (no undefined state) that could be used here.
I see, (I could be wrong) that this schema dependency is mostly for opening up additional fields if the main field is non-empty, but not really for the enable/disable switch that should open the subschema for the enabled entity (my use case).
Again, I feel like I am hitting the wall here. Do you have any idea why this playground link
does not work? Is that a known problem with the corresponding issue created?
@magaton You seem close to a solution. When there are no default values in the formData the if seem to render the code as if the condition passes. Perhaps making some things required and providing the appropriate "falsy" value for the fields will help?
I ended up with the following hack:
I have a boolean field enabled and an accompanied string field isEnabled that is hidden.
isEnabled is handled by a custom component defined in UI schema that reads the enabled field and if true, sets isEnabled to anything, e.g. true, otherwise unsets it in formData.
Then I can use schema dependencies which seems to be better supported than allOf + if-then.
It would be much nicer to have a custom field or widget that has 2 states, defined and undefined which could be rendered as a checkbox. But this goes beyond my current capabilities.
Hi @magaton!! Could you provide your solution please?? Thank you