LayoutMultiSchemaField rebuilds SchemaField's FieldTemplate contract by hand, and keeps drifting from it
Prerequisites
- I have read the documentation
What theme are you using?
core
Is your feature request related to a problem? Please describe.
LayoutGridField dispatches a discriminated oneOf/anyOf cell to LayoutMultiSchemaField instead of SchemaField:
// packages/core/src/components/fields/LayoutGridField.tsx:703
const Field = optionsInfo?.hasDiscriminator ? LayoutMultiSchemaField : SchemaField;LayoutMultiSchemaField then renders its own FieldTemplate and builds that template's props itself, so it holds a second copy of a contract SchemaField owns. Every part of that contract has had to be ported across by hand, one fix at a time:
| Arrived in | Props |
|---|---|
| #5324 | rawErrors, errorSchema, hideError |
| #5406 (for #5327) | classNames, style, hidden, formData, description, rawDescription |
| #5406, during review | ui:disabled and the ui:deprecatedHandling modes |
Each of those was a user-visible bug. A discriminated grid cell was the one field in a form that no rjsf-field* rule or ui:classNames ever reached, that ui:widget: 'hidden' did not hide, that rendered no ui:description or schema.description, and that ignored ui:disabled and a deprecated schema entirely.
The pattern matters more than any one of them: nothing makes the next addition to FieldTemplateProps reach this field, so the next one will be found the same way — from a report, after it ships.
Raised by @jimmycallin while reviewing #5406, in three threads that converge on this: altitude, reuse of the class list, and hidden vs. deprecatedHandling.
Describe the solution you'd like
Two options, both from that review:
- Render the discriminated cell through
SchemaField, via aFieldComponentthat renders only the selector widget.SchemaFieldthen owns theFieldTemplatecontract for every field without exception, and there is no second copy to keep in sync. - One shared builder for the
FieldTemplateprops, called by both fields.
(1) is the one that actually ends the class of bug. It is a change to the LayoutGridField dispatch rather than to either field, which is why it wants its own issue rather than riding along with a prop fix.
Describe alternatives you've considered
#5406 took (2) as far as it goes without a redesign. The duplicated computation is now shared, in @rjsf/utils:
getFieldClassNames()— therjsf-field/rjsf-field-<type>/rjsf-field-error/ui:classNameslistomitConsumedStyling()— the #439 strip of the class names and style a field's ownFieldTemplateconsumedgetDeprecatedHandling()— thehide/disable/labelresolution, including thelabeldefault
A single shared fieldProps function does not factor as cleanly. SchemaField's block also carries propertyNamesEnum, onKeyRename/onKeyRenameBlur/onRemoveProperty and the XxxOfField error suppression, none of which mean anything to a field that renders one selector widget — LayoutMultiSchemaField passes noop for three of them. Most of such a function's output would come straight back in as arguments.
What is still divergent, as of #5406
propertyNamesEnumis the onlyFieldTemplatePropsentryLayoutMultiSchemaFieldstill leaves unset.ui:requiredis resolved for this field by its caller, because the field has no override logic of its own (LayoutGridField.tsx:707-712). That one option's resolution sits in the caller for discriminated cells and in the field for every other.ui:readonlyis likewise pre-resolved bycomputeFieldUiSchema().LayoutMultiSchemaFieldreads only thereadonlyprop, whereSchemaFieldresolves the option itself, so the field is correct only as long asLayoutGridFieldis what renders it.- Under
ui:deprecatedHandling: 'label', an empty label is left undecorated, whereSchemaFieldproduces a label consisting of just the deprecation marker. That difference is deliberate and documented — a typeless discriminated cell is commonly titleless, andSchemaFieldhas a property-name fallback this field does not — but it is a difference, and a shared builder would have to pick one behavior for both.
Related
computeFieldUiSchema() builds { ...rawLocalUiOptions, ...restUiProps, ...globalUiOptions } (LayoutGridField.tsx:171), putting the globals last, so ui:globalOptions beat a cell's own ui:options — the reverse of getUiOptions(), where the local value wins. It affects every option on every grid field, not just the ones #5406 touches. It is in the same function as the dispatch and would be worth fixing in the same pass. Also raised by @jimmycallin: thread.
Source: rjsf-team/react-jsonschema-form