Skip to content

Show conditional field when checked boxes includes some options? #2625

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

Closed
3 tasks done
gentcys opened this issue Dec 3, 2021 · 12 comments
Closed
3 tasks done

Show conditional field when checked boxes includes some options? #2625

gentcys opened this issue Dec 3, 2021 · 12 comments

Comments

@gentcys
Copy link

gentcys commented Dec 3, 2021

Prerequisites

  • I have read the documentation;
  • In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.
  • Ideally, I'm providing a shared playground link demonstrating the issue.

Description

Suppose we defined a field of checkboxes with 4 options(A, B, C, D) and another field will show dependent on its checked options included A option.
e.g. we checked (A, B)/(A, C)/(A, B, C)/(A, B, C, D) the other field should be shown in all these cases
And no matter the order we checked these options, like for (A, B), we can checked B then A or checked A then B

Version

1.8.1

@epicfaace
Copy link
Member

epicfaace commented Dec 3, 2021 via email

@gentcys
Copy link
Author

gentcys commented Dec 3, 2021

@epicfaace Thanks for you reply.

Did you mean

{
  "title": "A registration form",
  "description": "A simple form example.",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "array",
      "title": "First name",
      "items": {
        "type": "string",
        "enum": [
          "A",
          "B",
          "C",
          "D"
        ]
      }
    }
  },
  "dependencies": {
    "firstName": {
      "oneOf": [
        {
          "properties": {
            "firstName": {
              "contains": "A"
            },
            "description": {
              "type": "string"
            }
          }
        }
      ]
    }
  }
}

@epicfaace
Copy link
Member

epicfaace commented Dec 3, 2021 via email

@dylanfprice
Copy link

Similar use case--it worked for me. Just one small adjustment since contains is supposed to be a schema:

"contains": {
  "const": "A"
}

Playground link - if A is checked (along with any others), description field shows.

@Zhenglish
Copy link

Zhenglish commented Dec 16, 2021

Hum, the 'contains' in the oneOf stops working when I have multiple controls. In this example, I have 2 checkboxes, and each controls a text field. They work by themselves, but stop working when both selected.

Any suggestions?

playground link

@dylanfprice
Copy link

Yeah I just encountered that one too. You have to enumerate the cases which is a bit unsavory but it works. Playground link.

Beyond about 3 controls it becomes a good interview question to generate all the cases 😄

You'll want to follow #2506 which should make it a lot easier.

@Zhenglish
Copy link

There is a warning message in console "react_devtools_backend.js:4045 ignoring oneOf in dependencies because there isn't exactly one subschema that is valid". This occurs when no item is select, so I added a subschema for that scenario. However, I couldn't make it right. Can you please look at my playground?

@dylanfprice
Copy link

contains matches items within an array so contains: {const: []} doesn't do what you want. You need something that will match an empty array, maxItems: 0 works. playground

@dylanfprice
Copy link

@epicfaace I think you can close this

@epicfaace
Copy link
Member

thanks!

@jorisw
Copy link
Contributor

jorisw commented Jan 31, 2022

const Form = JSONSchemaForm.default;

const schema = {
  "title": "Title",
  "type": "object",
  "properties": {
    "intent_goal": {
      "title": "What is your goal?",
      "type": "array",
      "items": {
        "type": "string",
        "enum": [
          "advise_me",
          "start_company",
          "other"
        ],
        "enumNames": [
          "Advise me",
          "I want to start a company",
          "Other goal..."
        ]
      },
      "minItems": 1,
      "uniqueItems": true
    }
  },
  "required": [
  ],
  "dependencies": {
    "intent_goal": {
      "oneOf": [
        {
          "properties": {
            "intent_goal": {
              "items":{
                "enum": [null]
              }
            }
          }
        },{
          "properties": {
            "intent_goal": {
              "contains": { enum :  ["other"] }  
            },
            "other_goal": {
              "type": "string"
            }
          }
        }
      ]
    }
  }
};

const uiSchema = {
  "intent_goal": {
    "ui:widget": "checkboxes"
  },
  "other_goal": {
    "ui:widget": "textarea"
  }
}

ReactDOM.render((
  <Form schema={schema} uiSchema={uiSchema} />
), document.getElementById("app"));

@xyy7260
Copy link

xyy7260 commented Mar 10, 2023

是的,我也刚遇到那个。你必须举一些命令人反对但有有效的例子。游乐场链接

除了大约3个控制之外,它成为生成所有案例的一个很好的面试题😄

你会想要跟随#2506这应该会让它更容易。

The more options you have, the more code you write. Is there a better way to do this?

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

6 participants