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
14.35k stars 2.19k forks source link

custom widget for array fields with no `enums` or `uniqueItems` set to true #1385

Closed ghigt closed 2 years ago

ghigt commented 5 years ago

Prerequisites

Description

Why is it not possible to specify a custom widget for array fields with no enums or uniqueItems set to true? I would like to create 2 widgets:

Steps to Reproduce

  1. Having an array without enum or uniqueItems in its items.
    
    const schema = {
    type: "array",
    items: {
    "type": "string"
    },
    };

const uiSchema = { "ui:widget": "myCustomWidget" }



#### Expected behavior

Widget `myCustomWidget` to be used.

#### Actual behavior

Widget used is the default rendered by [`this.renderNormalArray()`](https://github.com/mozilla-services/react-jsonschema-form/blob/master/src/components/fields/ArrayField.js#L439)

### Version

v1.7.0
epicfaace commented 5 years ago

You can't use a widget for array fields. Try either the ArrayFieldTemplate prop or the ui:ArrayFieldTemplate attribute in the uiSchema.

ghigt commented 5 years ago

We thought about using an arrayFieldTemplate in the case where there is no enum attribute but our issue is that it doesn't seem possible to reference an ArrayFieldTemplate with its name in the uiSchema.

As opposed to widgets that supports something like { ui:widget: 'MyWidget' }, arrayFieldTemplate only accepts a component which is quite problematic when you want to serialize the uiSchema.

Would it be interesting to extend the way widgets are handled to arrayFieldTemplates ?

Sankluj commented 5 years ago

Hi!

I'm working in the same team as @ghigt.

We would be pretty happy to propose a PR to implement the use case he explained above if you think it is interesting for the project, i.e.

Allowing the customizations of non enums, non uniqueItems array fields in the ui:schema while keeping it serializable.

However it is not clear what would be the best way to do this between:

Thanks :)

epicfaace commented 5 years ago

Oh, I see -- although it's possible to do this using a custom ArrayFieldTemplate, allowing for ui:widget would parallel the current use of ui:widget when having an array field with enums or uniqueItems.

So please go ahead with doing that! The second option (modifying renderNormalArray) seems like it would definitely work and be the most straightforward way to do so.

OleksiL commented 4 years ago

Hello guys. Has this been implemented? I serialize schemas to json and save them to database. At the moment setting of array template at field level doesn't work for me - obviously component can't be serialized: "ui:ArrayFieldTemplate": MyArrayFieldTemplate

AbigailFernandes commented 2 years ago

Hi, would like to know if and when this would be prioritized. We have a need where we want to use a custom MultiSelect widget, that allows a user to select multiple values, so we would like the type to be array. Currently, we are unable to use type array and specify a Custom Widget without specifying the enums. We would like to be able to specify CustomWidget for ui:widget and for type array without needing to specify enum or uniqueItems to true

alfonsoar commented 2 years ago

@epicfaace to add on to the point above ☝🏾

Is https://github.com/rjsf-team/react-jsonschema-form/pull/2125 the right approach to resolve this issue? If yes, are you accepting PRs? Maybe we can pick it up to close this one out.

epicfaace commented 2 years ago

I think so -- made a review to #2125.

alfonsoar commented 2 years ago

@epicfaace - Not a huge fan of the approach on https://github.com/rjsf-team/react-jsonschema-form/pull/2125 so created https://github.com/rjsf-team/react-jsonschema-form/pull/2697. Would you mind taking a look?

epicfaace commented 2 years ago

Thanks -- do you mind adding it to the weekly meeting agenda? I think a synchronous review would be easiest -- https://github.com/rjsf-team/react-jsonschema-form/issues/2677