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

Schema dependencies with Boolean field

Open magaton opened this issue 2 years ago • 7 comments

Prerequisites

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

playground linke

Environment

- OS:
- Node:
- npm:

Anything else?

If you say this is not a bug, then please suggest a workaround.

magaton avatar Nov 10 '23 21:11 magaton

Are you ok with an if-then as shown via this playground link?

heath-freenome avatar Nov 11 '23 02:11 heath-freenome

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

magaton avatar Nov 11 '23 15:11 magaton

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.

heath-freenome avatar Nov 13 '23 20:11 heath-freenome

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 avatar Nov 13 '23 22:11 magaton

@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?

heath-freenome avatar Nov 17 '23 21:11 heath-freenome

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.

magaton avatar Nov 18 '23 23:11 magaton

Hi @magaton!! Could you provide your solution please?? Thank you

edgar86cru avatar Nov 23 '23 09:11 edgar86cru