Skip to content

[Question] Dynamic titles for array fields #649

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
joshhornby opened this issue Jul 26, 2017 · 4 comments
Closed

[Question] Dynamic titles for array fields #649

joshhornby opened this issue Jul 26, 2017 · 4 comments
Labels

Comments

@joshhornby
Copy link

Prerequisites

I am looking into a way of generating dynamic titles for items in an Array Field. In my current schema I have two array fields so only want to target one with the custom dynamic titles.

In this case each title should be a day of the week, this data is coming from the formData being passed in. So each object in the array would have a title.

My questions are:

  1. Is it possible to render a dynamic title based off a property?
  2. If not, how can I define a custom template for only one array field at a time?

Thanks

@shortwavedave
Copy link

I also need this feature - is there any way to accomplish this?

@epicfaace
Copy link
Member

  1. There is currently no way defined by JSON Schema to change the schema based on the data. There is a proposal for the $data keyword, but it looks like it's still in discussion and may or may not be standardized.

If the form data is based on a property that is not part of the array element, you could do something with dependencies though. For example:

{
    "type": "object",
    "defintions": {
        "b": {"type": "array", "items": {"type": "string"}}
    },
    "properties": {
        "a": {"type": "string"}  
    },
    "dependencies": {
        "a": {
            "oneOf": [
                {
                    "properties": {
                        "a": {"const": "Option1"},
                        "b": {"$ref": "#/definitions/b", "title": "Title based on Option1"}
                    }
                },
                {
                    "properties": {
                        "a": {"const": "Option2"},
                        "b": {"$ref": "#/definitions/b", "title": "Title based on Option2"}
                    }
                }
            ]
        }
    }
}
  1. You can define a custom template for a single array element by using ui:ArrayFieldTemplate as mentioned in https://react-jsonschema-form.readthedocs.io/en/latest/advanced-customization/#array-field-template.

@epicfaace
Copy link
Member

Closing this issue, but feel free to reopen it if you have any additional questions!

@vxb1766
Copy link

vxb1766 commented Feb 24, 2022

@epicfaace I have a similar issue. how do I add title for array items?
https://codepen.io/veena-balasubramanya-gmail-com/pen/xxPJxoW
When I click plus, I want the title to show as test1, test2, test3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants