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.14k stars 2.18k forks source link

Custom Field on property vs Custom Field at Schema Level #4053

Open codisfy opened 8 months ago

codisfy commented 8 months ago

Prerequisites

What theme are you using?

bootstrap-4

What is your question?

Hello,

I am encountering an issue with globally overriding all instances of StringField in a form. While I can successfully override a specific field using ui:field in the uiSchema, attempting to apply a similar override globally across all string fields in the form does not yield the same result.

Issue Description:

When using ui:field in uiSchema to override a specific string field (e.g., api_version), the entire field including its label is replaced by the custom component. However, when trying to globally override all string fields by passing a custom component to the fields prop, only the input widget is replaced, but the label remains rendered by RJSF's default field template.

Reproduction Link:

I have created a CodeSandbox example to demonstrate this issue: https://codesandbox.io/p/sandbox/strange-firefly-mn859g

In this example, please notice the following behaviors:

Expected Behavior:

I would like to globally override all instances of StringField in a similar manner to how ui:field works for individual fields, where both the input widget and the label are replaced by the custom component.

Questions:

  1. Is this behavior (partial override using the fields prop) intentional, or might it be a bug?
  2. If it is intentional, could you provide guidance on how to achieve a full override of all string fields (including labels) globally across the form?

Any insights or suggestions on this matter would be greatly appreciated.

heath-freenome commented 8 months ago

@codisfy If you look at the second example in the documentation for custom fields you will see how to do what you want.

codisfy commented 7 months ago

Thanks for your response.

Do you mean this example?

import { RegistryFieldsType, RegistryWidgetsType } from '@rjsf/utils';
import { FormProps } from '@rjsf/core';

const customFields: RegistryFieldsType = { StringField: CustomString };
const customWidgets: RegistryWidgetsType = { CheckboxWidget: CustomCheckbox };

function MyForm(props: FormProps) {
  return <Form fields={customFields} widgets={customWidgets} {...props} />;
}

The code sandbox that I supplied along with this question, has single field override as well as fields override(like shown in example above). The full override still has the label, the single field override works as expected. I wanted to have all of the string type fields to be without label.

Also created a video of the sandbox just in case it doesn't open as expected:

screencast_00015.webm