Skip to content
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

Add ability to give a form multiple templates of a given type and pick one by name through uiSchema #3962

Closed
1 task done
elevesque-nexapp opened this issue Nov 16, 2023 · 2 comments
Labels
feature Is a feature request help wanted templates Related to the templates capabilities of RJSF

Comments

@elevesque-nexapp
Copy link

Prerequisites

What theme are you using?

core

Is your feature request related to a problem? Please describe.

In my project, we define a variety of schemas in our backend API and dynamically fetch those from the frontend to display relevant forms. In some of these forms, we have object fields that we would like to be able to display horizontally instead of vertically. Take for example an array of HTTP headers:

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "header" { "type": "string" },
      "value": { "type": "string" }
    }
  }
}

We would like to be able to display those side-by-side as below:
Screenshot 2023-11-16 at 3 42 26 PM

However, we have other object fields (the form itself being one example) for which we want to keep the default vertical layout.

Because schemas are defined on the backend, we can't easily provide a custom ObjectFieldTemplate for this field.

Describe the solution you'd like

We'd like to be able to provide multiple ObjectFieldTemplate implementations to our form (say VerticalObjectFieldTemplate and HorizontalObjectFieldTemplate) and have the ability to pick one from the ui schema. For example:

function MyForm() {
  const { schema, uiSchema } = useDynamicSchema()

  return <Form schema={schema} uiSchema={uiSchema} templates={{ ObjectFieldTemplate: {'vertical': VerticalObjectFieldTemplate, 'horizontal': HorizontalObjectFieldTemplate}} />

schema:

{
  "type": "object",
  "properties": {
    "headers": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "header" { "type": "string" },
          "value": { "type": "string" }
        }
      }
    }
  }
}

uiSchema:

{
  "headers": {
    "items": {
      "ui:ObjectFieldTemplate": "horizontal"
    }
  }
}

Describe alternatives you've considered

Our current solution is to do basically that, but by modifying the ui schema by replacing "ui:ObjectFieldTemplate": "horizontal" by its implementation in the frontend after fetching it from the backend. This works, but it's not exactly simple nor elegant.

We've also looked into custom widgets and fields, but the former is too low level and the latter appears too generic. With a custom field, we weren't able to get the object field's properties without digging around in the registry and form data in somewhat complex ways.

We've also considered the options of simply adding some arbitrary key to they ui schema and using that as a sort of additional prop to a custom ObjectFieldTemplate. It's simple enough, but it opens the door to all sorts of implicit contracts between backend and frontend that would be hard to track, maintain and debug.

We've also tried playing around with ui:classNames and ui:style, but between the various layers of Template, Field and Render components that don't always pass all props down to their children, this quickly ends up becoming confusing and finicky.

@elevesque-nexapp elevesque-nexapp added feature Is a feature request needs triage Initial label given, to be assigned correct labels and assigned labels Nov 16, 2023
@heath-freenome heath-freenome removed the needs triage Initial label given, to be assigned correct labels and assigned label Nov 17, 2023
@nickgros
Copy link
Contributor

@elevesque-nexapp this is very similar to #3695, which is being solved in #3881, and has a slightly different solution than you're suggesting. I'll post a comment in that PR referencing this issue and maybe we can settle on the right path forward.

@elevesque-nexapp
Copy link
Author

Agreed, this issue is a duplicate. I've commented on #3881 that the solution proposed there is fine with me and aligns better with RJSF's current APIs. I'll close this to avoid diluting the discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is a feature request help wanted templates Related to the templates capabilities of RJSF
Projects
None yet
Development

No branches or pull requests

3 participants