MultiSchemaFieldTemplate lays out an empty selector row when it is given no selector
Prerequisites
- I have searched the existing issues
- I understand that providing a SSCCE example is tremendously useful to the maintainers
- I have read the documentation
What theme are you using?
core (and every theme except mantine)
Version
7.x (v7 branch)
Current Behavior
MultiSchemaFieldTemplate receives selector={undefined} whenever MultiSchemaField renders an optional data control that has no data yet — MultiSchemaField.tsx:
const selector =
!isOptionalRender || hasFormData ? (
<Widget … />
) : undefined;Every theme's template except mantine's still renders a wrapper element around that nothing, so the field gets an empty row — and in most themes the margin or padding meant for the selector.
Measured on @rjsf/core with this schema and uiSchema:
const schema: RJSFSchema = {
type: 'object',
properties: {
val: {
type: 'object',
anyOf: [
{ title: 'A', properties: { a: { type: 'string' } } },
{ title: 'B', properties: { b: { type: 'string' } } },
],
},
},
};
const uiSchema: UiSchema = { 'ui:globalOptions': { enableOptionalDataFieldForType: ['object'] } };the rendered output contains:
<div class="panel panel-default panel-body">
<div class="form-group"></div> <!-- the selector row, with no selector in it -->
<div class="form-group rjsf-field rjsf-field-object">
<fieldset class="rjsf-optional-object-field" id="root_val">…The wrapper each theme renders, and whether it contributes space when empty:
| theme | wrapper around {selector} |
empty-row space |
|---|---|---|
| core | <div className='form-group'> |
yes |
| chakra-ui | <Box mb={4}> |
yes |
| daisyui | <div className='mb-4'> |
yes |
| mui | <FormControl fullWidth sx={{ mb: 2 }}> |
yes |
| react-bootstrap | <Card.Body> |
yes (a card body's padding) |
| shadcn | <div className='mb-4'> |
yes |
| fluentui-rc | <div> under '> div': { marginBottom: '4px' } |
yes |
| antd | <div> |
no (unstyled, but still an empty element) |
| mantine | none — renders {selector} directly into a Stack |
not affected |
mantine is the one theme that already handles it, and its template even says so:
{selector}
{/* Without a selector, such as while an optional field has no data, the option's field renders the errors */}Expected Behavior
A template should not lay out a row for a selector it was not given. The selector wrapper should render only when selector is truthy, as mantine's already effectively does.
Whether a theme should also drop its surrounding card or panel when there is no selector is a per-theme layout decision, which is itself the argument for the guard living in each theme template rather than in the field.
Steps To Reproduce
- Render the schema above with
enableOptionalDataFieldForType: ['object']. - Before touching the Add button, inspect the
anyOffield. - The selector row is present and empty.
Notes
Found while reviewing #5362, which fixes the same symptom for one other caller of a selector-less template: FallbackField used to render FallbackFieldTemplate (which delegates to MultiSchemaFieldTemplate) for a schema allowing a single type, where the type selector is null. That PR resolves it by having the field return the value field directly and skip the template, and adds a test asserting no zero-child .form-group for that case. The same invariant is still violated on the optional-data path described here, which no single field can fix — hence this issue.
Fixing it in the templates would have covered both callers for every theme at once; it was left out of #5362 because it is 8 theme templates plus the shared snapshots in 9 packages, on a path that PR does not otherwise touch.
Source: rjsf-team/react-jsonschema-form