vazco / uniforms

A React library for building forms from any schema.
https://uniforms.tools
MIT License
1.94k stars 239 forks source link

Option to use JSON Schema `description` property as a helper text #1264

Closed dyatko closed 1 year ago

dyatko commented 1 year ago

We have a version field with the following schema generated by JSONSchemaBridge:

{
    "type": "string",
    "description": "The version of the sequencer, e.g., \"0.1.1\"."
}

At the moment mui AutoForm renders such field:

Screenshot 2023-05-11 at 13 57 29

While we would love to have the option to use the field description as a helper text, e.g.:

Screenshot 2023-05-11 at 14 00 02
radekmie commented 1 year ago

Hi @dyatko! Such changes won't go into uniforms core packages, as this would tie a bridge (here: JSON Schema) with a theme (here: MUI, I guess). However, you can do it easily without core changes:

  1. Pre-process the schema by copying (or renaming) the description field into uniforms.helperText (or any other).
  2. Subclass the bridge and override its getProps method to copy (or rename) the description prop to helperText (or any other).

Of course, these two can be mixed and matched if needed, depending on the use case.

radekmie commented 1 year ago

No response so far, so I'm closing. Feel free to comment further, though!

arthurdenner commented 6 months ago

I just wanted to thank you, @radekmie, for your comment. It helped me a lot!

For others: In my case, I was using the uniforms-antd and uniforms-bridge-json-schema packages and all it took me was to extend the JSONSchemaBridge to map the description field to the info prop.

I decided to patch-package uniforms-antd because I wanted an InfoCircleOutlined icon instead but that's beside the point a bit. Snippet:

class CustomJSONSchemaBridge extends JSONSchemaBridge {
  getProps(name: string) {
    const { description, ...props } = super.getProps(name);
    return { ...props, info: description };
  }
}

new CustomJSONSchemaBridge({...}) // Instead of new JSONSchemaBridge