| 1 |
import Box from '@mui/material/Box'; |
| 2 |
import Grid from '@mui/material/Grid'; |
| 3 |
import Paper from '@mui/material/Paper'; |
| 4 |
import { |
| 5 |
getTemplate, |
| 6 |
getUiOptions, |
| 7 |
ArrayFieldTemplateProps, |
| 8 |
ArrayFieldTemplateItemType, |
| 9 |
FormContextType, |
| 10 |
RJSFSchema, |
| 11 |
StrictRJSFSchema, |
| 12 |
} from '@rjsf/utils'; |
| 13 |
|
| 14 |
/** The `ArrayFieldTemplate` component is the template used to render all items in an array. |
| 15 |
* |
| 16 |
* @param props - The `ArrayFieldTemplateItemType` props for the component |
| 17 |
*/ |
| 18 |
export default function ArrayFieldTemplate< |
| 19 |
T = any, |
| 20 |
S extends StrictRJSFSchema = RJSFSchema, |
| 21 |
F extends FormContextType = any |
| 22 |
>(props: ArrayFieldTemplateProps<T, S, F>) { |
| 23 |
const { canAdd, disabled, idSchema, uiSchema, items, onAddClick, readonly, registry, required, schema, title } = |
| 24 |
props; |
| 25 |
const uiOptions = getUiOptions<T, S, F>(uiSchema); |
| 26 |
const ArrayFieldDescriptionTemplate = getTemplate<'ArrayFieldDescriptionTemplate', T, S, F>( |
| 27 |
'ArrayFieldDescriptionTemplate', |
| 28 |
registry, |
| 29 |
uiOptions |
| 30 |
); |
| 31 |
const ArrayFieldItemTemplate = getTemplate<'ArrayFieldItemTemplate', T, S, F>( |
| 32 |
'ArrayFieldItemTemplate', |
| 33 |
registry, |
| 34 |
uiOptions |
| 35 |
); |
| 36 |
const ArrayFieldTitleTemplate = getTemplate<'ArrayFieldTitleTemplate', T, S, F>( |
| 37 |
'ArrayFieldTitleTemplate', |
| 38 |
registry, |
| 39 |
uiOptions |
| 40 |
); |
| 41 |
// Button templates are not overridden in the uiSchema |
| 42 |
const { |
| 43 |
ButtonTemplates: { AddButton }, |
| 44 |
} = registry.templates; |
| 45 |
return ( |
| 46 |
<Paper elevation={2}> |
| 47 |
<Box p={2}> |
| 48 |
<ArrayFieldTitleTemplate |
| 49 |
idSchema={idSchema} |
| 50 |
title={uiOptions.title || title} |
| 51 |
schema={schema} |
| 52 |
uiSchema={uiSchema} |
| 53 |
required={required} |
| 54 |
registry={registry} |
| 55 |
/> |
| 56 |
<ArrayFieldDescriptionTemplate |
| 57 |
idSchema={idSchema} |
| 58 |
description={uiOptions.description || schema.description} |
| 59 |
schema={schema} |
| 60 |
uiSchema={uiSchema} |
| 61 |
registry={registry} |
| 62 |
/> |
| 63 |
{items && |
| 64 |
items.map(({ key, ...itemProps }: ArrayFieldTemplateItemType<T, S, F>) => ( |
| 65 |
<ArrayFieldItemTemplate key={key} {...itemProps} /> |
| 66 |
))} |
| 67 |
{canAdd && ( |
| 68 |
<Grid container justifyContent='flex-end'> |
| 69 |
<Grid item={true}> |
| 70 |
<Box mt={2}> |
| 71 |
<AddButton |
| 72 |
className='array-item-add' |
| 73 |
onClick={onAddClick} |
| 74 |
disabled={disabled || readonly} |
| 75 |
uiSchema={uiSchema} |
| 76 |
registry={registry} |
| 77 |
/> |
| 78 |
</Box> |
| 79 |
</Grid> |
| 80 |
</Grid> |
| 81 |
)} |
| 82 |
</Box> |
| 83 |
</Paper> |
| 84 |
); |
| 85 |
} |