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 custom widget Array even without enums props #939

Closed
1 task done
Paul-Vandell opened this issue May 30, 2018 · 2 comments
Closed
1 task done

Add custom widget Array even without enums props #939

Paul-Vandell opened this issue May 30, 2018 · 2 comments

Comments

@Paul-Vandell
Copy link

Prerequisites

Version

"react-jsonschema-form": "^1.0.3"

Description

Hey there and THX .... no a really big thx for this package.
I would like talking about adding component Widget that manage itself Array item string | number | ...
But keep the json-schema validate future data like `["foo", "bar"]. All this things without specified enum data cause :

  • They are too much (Dates)
  • They are not yet know maybe. (Async Data)

Actual behavior

Take this simple example :
I use this package to get an Array of string representing some dates :
FlatPickr
so a simple schema will be :

const schema = {
  DateArray: {
      type: "array",
      minItems: 1,
      uniqueItems: true,
      items: { type: "string" }
    }
}
const uiSchema = {
  DateArray: {
    "ui-widget": "DateArrayWidget",
  }
}

But this will create a multiline DateField with (+) and (-) button... I'm not expected this. As you can see, even if i specified the uniqueItems property to true.

Expected behavior

I expected only one DateComponent where i can select multiple date Right ? Even if items props contain only the type props;

Personal Solution Found

So the only solution that i found is to Customize the ArrayField if enum property is empty and if uniqueItems is specified. It is maybe not the right way but i share for anyone get this issue:

import * as React from "react";
import { widgets } from "../autoform/widgets";
const ArrayField = require("react-jsonschema-form/lib/components/fields/ArrayField")
  .default;

const MyArrayField = props => {
  const { schema, idSchema, uiSchema, formData } = props;
  if (!schema.items.enum && schema.uniqueItems) {
    const Widget = widgets[uiSchema["ui-widget"]];
    const options = widgets[uiSchema["ui-options"]];
    if (Widget)
      return (
        <Widget
          id={idSchema && idSchema.$id}
          multiple={true}
          options={options}
          value={formData}
          {...props}
        />
      );
  }
  return <ArrayField {...props} />;
};
const fields =  {"ArrayField": MyArrayField}
*** Some where ...
<JsonForm ... ... fields={fields} />

It is the much cleaner way that i Found but maybe i wrong. But at this time it is working for my needs...

Question & Suggestion

Can we have a simplest way to handle this type of component in your code ?
Do you have a better solution for this request ?
And then, add it to the documentation to avoid frustation development... because this package is great. Ajv is fast & json-schema should be ( for me ) the must used verification for front & back end development.

@epicfaace
Copy link
Member

@Vandell63 can you provide a JSFiddle / Codepen demonstrating how you would like the field to look like?

@epicfaace
Copy link
Member

I think #1010 will solve this issue

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

No branches or pull requests

2 participants