Get oneOf selection 'title' to be in generated json

Hello,

I am using:
@jsonforms/core”: “^2.4.0”,
@jsonforms/material-renderers”: “^2.4.0”,
@jsonforms/react”: “^2.4.0”.

Based on the oneOf examples, I wanted to get the title from one of theelements in the oneOf arrays (either address or socials) to also be populated in the generated json depending on what the user selects.

You can see my implementation here, admiring-fermi-wy57n - CodeSandbox. This is the output I wanted to receive if the user select “address” and enter the info:

{
  "name": "Tom",
  "infoType": "address",
  "info": {
    "infoType": "address", // or this
    "street_address": "110 Main Street",
    "city": "Los Angeles",
    "state": "CA"
  }
}

or if the user selects “socials” then:

{
  "name": "Tom",
  "infoType": "socials",
  "info": {
    "infoType": "socials", // or this
    "name": "Tom"
    "twitter": "@tom",
    "mail": "tom@tom.com"
  }
}

I was able to use ajv defaults to populate the default, but if the user clicks another selection the default does not update. Do you have any other recommendations on how I can achieve this? The infoType key does not necessarily have to be nested in the info object it can also be on the same level as the info key.

Thank you

[original thread by Andrew Vathanakamsang]

Hi @avath(avath) ,
just for clarification you want to have defaults for the info object based on the selection of the infoType? And switching the infoType does not repopulate the default values?

[Andrew Vathanakamsang]

Yes, it appears switching the infoType does not repopulate the default values. If I hit “socials” the infoType still says address.

[Andrew Vathanakamsang]

I am not sure if using defaults is the right way to do this, but ultimately, I want to be able to capture the title value (in this case “address” or “socials”) from the “oneOf” when it is selected.

      oneOf: [
        {
          $ref: "#/definitions/address",
          title: "address"
        },
        {
          $ref: "#/definitions/socials",
          title: "socials"
        }
      ]

You can listen to the data change and do something then see the onChange property on the jsonforms element

[Andrew Vathanakamsang]

[Andrew Vathanakamsang]

I am not sure if I follow. Currently, the form is listening to the data on change and you can see the json data I get populated on the left. The only field I am missing there is a key “infoType” with the value of “socials”. No Matter which button I click the default remains to be “address”.

[Andrew Vathanakamsang]

This is how I am currently setting the defaults

    address: {
      type: "object",
      properties: {
        infoType: { type: "string", default: "address" },
        street_address: { type: "string" },
        city: { type: "string" },
        state: { type: "string" }
      },
      required: ["street_address", "city", "state"]
    },
    socials: {
      type: "object",
      properties: {
        infoType: { type: "string", default: "social" },
        twitter: { type: "string" },
        mail: { type: "string" }
      },
      required: ["name", "mail"]
    },

ok, sorry mu fault, I misunderstood you.
Ok so the infotype itself is not changing if you switch between address and socials? sorry I thought it worked when I tried it out, but it doesn’t as I can now see.
I would say it is a bug and should be fixed in jsonforms. It looks like the wrong schema is used.
Could you open a bug for this please?

[Andrew Vathanakamsang]

Will do thanks for looking into this

[Andrew Vathanakamsang]

I created the issue here Default value associated to oneOf element does not change with user selection · Issue #1634 · eclipsesource/jsonforms · GitHub