Debian Codegraph @

packages/mui/src/ObjectFieldTemplate/ObjectFieldTemplate.tsx

raw
1 import Grid from '@mui/material/Grid';
2 import {
3 FormContextType,
4 ObjectFieldTemplateProps,
5 RJSFSchema,
6 StrictRJSFSchema,
7 canExpand,
8 descriptionId,
9 getTemplate,
10 getUiOptions,
11 titleId,
12 } from '@rjsf/utils';
13
14 /** The `ObjectFieldTemplate` is the template to use to render all the inner properties of an object along with the
15 * title and description if available. If the object is expandable, then an `AddButton` is also rendered after all
16 * the properties.
17 *
18 * @param props - The `ObjectFieldTemplateProps` for this component
19 */
20 export default function ObjectFieldTemplate<
21 T = any,
22 S extends StrictRJSFSchema = RJSFSchema,
23 F extends FormContextType = any
24 >(props: ObjectFieldTemplateProps<T, S, F>) {
25 const {
26 description,
27 title,
28 properties,
29 required,
30 disabled,
31 readonly,
32 uiSchema,
33 idSchema,
34 schema,
35 formData,
36 onAddClick,
37 registry,
38 } = props;
39 const uiOptions = getUiOptions<T, S, F>(uiSchema);
40 const TitleFieldTemplate = getTemplate<'TitleFieldTemplate', T, S, F>('TitleFieldTemplate', registry, uiOptions);
41 const DescriptionFieldTemplate = getTemplate<'DescriptionFieldTemplate', T, S, F>(
42 'DescriptionFieldTemplate',
43 registry,
44 uiOptions
45 );
46 // Button templates are not overridden in the uiSchema
47 const {
48 ButtonTemplates: { AddButton },
49 } = registry.templates;
50 return (
51 <>
52 {title && (
53 <TitleFieldTemplate
54 id={titleId<T>(idSchema)}
55 title={title}
56 required={required}
57 schema={schema}
58 uiSchema={uiSchema}
59 registry={registry}
60 />
61 )}
62 {description && (
63 <DescriptionFieldTemplate
64 id={descriptionId<T>(idSchema)}
65 description={description}
66 schema={schema}
67 uiSchema={uiSchema}
68 registry={registry}
69 />
70 )}
71 <Grid container={true} spacing={2} style={{ marginTop: '10px' }}>
72 {properties.map((element, index) =>
73 // Remove the <Grid> if the inner element is hidden as the <Grid>
74 // itself would otherwise still take up space.
75 element.hidden ? (
76 element.content
77 ) : (
78 <Grid item={true} xs={12} key={index} style={{ marginBottom: '10px' }}>
79 {element.content}
80 </Grid>
81 )
82 )}
83 {canExpand<T, S, F>(schema, uiSchema, formData) && (
84 <Grid container justifyContent='flex-end'>
85 <Grid item={true}>
86 <AddButton
87 className='object-property-expand'
88 onClick={onAddClick(schema)}
89 disabled={disabled || readonly}
90 uiSchema={uiSchema}
91 registry={registry}
92 />
93 </Grid>
94 </Grid>
95 )}
96 </Grid>
97 </>
98 );
99 }