@rjsf/core
Version:
A simple React component capable of building HTML forms out of a JSON schema.
25 lines (24 loc) • 2.39 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { canExpand, descriptionId, getTemplate, getUiOptions, titleId, buttonId, } from '@rjsf/utils';
/** The `ObjectFieldTemplate` is the template to use to render all the inner properties of an object along with the
* title and description if available. If the object is expandable, then an `AddButton` is also rendered after all
* the properties.
*
* @param props - The `ObjectFieldTemplateProps` for this component
*/
export default function ObjectFieldTemplate(props) {
const { className, description, disabled, formData, fieldPathId, onAddProperty, optionalDataControl, properties, readonly, registry, required, schema, title, uiSchema, } = props;
const options = getUiOptions(uiSchema);
const TitleFieldTemplate = getTemplate('TitleFieldTemplate', registry, options);
const DescriptionFieldTemplate = getTemplate('DescriptionFieldTemplate', registry, options);
// For "pure union" schemas (oneOf/anyOf without properties), skip rendering the empty fieldset wrapper.
// The AnyOfField/OneOfField will handle rendering the union selector and selected variant's content directly.
const isPureUnionSchema = (schema.oneOf || schema.anyOf) && !schema.properties && properties.length === 0;
if (isPureUnionSchema) {
return null;
}
const showOptionalDataControlInTitle = !readonly && !disabled;
// Button templates are not overridden in the uiSchema
const { ButtonTemplates: { AddButton }, } = registry.templates;
return (_jsxs("fieldset", { className: className, id: fieldPathId.$id, children: [title && (_jsx(TitleFieldTemplate, { id: titleId(fieldPathId), title: title, required: required, schema: schema, uiSchema: uiSchema, registry: registry, optionalDataControl: showOptionalDataControlInTitle ? optionalDataControl : undefined })), description && (_jsx(DescriptionFieldTemplate, { id: descriptionId(fieldPathId), description: description, schema: schema, uiSchema: uiSchema, registry: registry })), !showOptionalDataControlInTitle ? optionalDataControl : undefined, properties.map((prop) => prop.content), canExpand(schema, uiSchema, formData) && (_jsx(AddButton, { id: buttonId(fieldPathId, 'add'), className: 'rjsf-object-property-expand', onClick: onAddProperty, disabled: disabled || readonly, uiSchema: uiSchema, registry: registry }))] }));
}