rjsf-team / react-jsonschema-form

A React component for building Web forms from JSON Schema.
https://rjsf-team.github.io/react-jsonschema-form/
Apache License 2.0
13.85k stars 2.17k forks source link

Ability to extend widgetMap to specify widgets for custom formats #3960

Open nickgros opened 7 months ago

nickgros commented 7 months ago

Prerequisites

What theme are you using?

core

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

In my scenario, we have users who create JSON Schemas and users who consume the form output of the JSON Schema. So the contents and keys of the JSON Schema are arbitrary.

We would like a way to display custom widgets for custom formats that have not been defined in the JSON Schema Specification (e.g. for string).

For example, we would like to allow users to write JSON Schema definitions like this:

{
  "type": "string",
  "format": "com.myorg.userId"
}

In the rendered form, we would like to render a custom widget based on the custom format (in this case, a user-picker that shows rich metadata instead of just an ID) for just these fields.

Describe the solution you'd like

I think it would make sense to allow merging a custom configuration with the widgetMap.

I could provide these widgets and this widgetMap augmentation as a prop to Form:

<Form
  {...typicalProps}
  widgets={{
    UserPickerWidget: UserPickerWidget
  }}
  widgetMap={{
    string: {
      "com.myorg.userId": "UserPickerWidget"
    }
  }}
/>

I think this is a desirable solution because you're describing which widget to use based on the contents of your JSON Schema, so it's pretty easy to accomplish with limited knowledge of RJSF internals.

Describe alternatives you've considered

I found #704 which is similar but I think is slightly different from what I'm proposing. Custom formats are allowed in JSON Schema. From the specification (emphasis mine):

There is a bias toward networking-related formats in the JSON Schema specification, most likely due to its heritage in web technologies. However, custom formats may also be used, as long as the parties exchanging the JSON documents also exchange information about the custom format types. A JSON Schema validator will ignore any format type that it does not understand.

I think this is probably possible today with some custom field magic, but I'd love to make this a low-code solution because mixing small amounts of custom logic into copy-pasted code is hard to maintain.

heath-freenome commented 7 months ago

@nickgros Can you see if you register a widget with the format name works?