Adding the "numbered" props to array field to display value position in array

This commit is contained in:
2025-05-03 20:33:25 +02:00
parent 2f2c5a035d
commit 3dc91b329f

View File

@@ -9,6 +9,8 @@ import {
FormContextType, FormContextType,
} from '@rjsf/utils'; } from '@rjsf/utils';
import { CrudTextRJSFSchema } from "../widgets/crud-text-widget"; import { CrudTextRJSFSchema } from "../widgets/crud-text-widget";
import Stack from "@mui/material/Stack";
import Typography from "@mui/material/Typography";
/** The `ArrayFieldTemplate` component is the template used to render all items in an array. /** The `ArrayFieldTemplate` component is the template used to render all items in an array.
* *
@@ -21,13 +23,6 @@ export default function ArrayFieldTemplate<
>(props: ArrayFieldTemplateProps<T, S, F>) { >(props: ArrayFieldTemplateProps<T, S, F>) {
const { canAdd, disabled, idSchema, uiSchema, items, onAddClick, readonly, registry, required, schema, title } = const { canAdd, disabled, idSchema, uiSchema, items, onAddClick, readonly, registry, required, schema, title } =
props; props;
let gridSize = 12;
if (schema.props) {
if (schema.props.hasOwnProperty("items_per_row")) {
gridSize = gridSize / schema.props.items_per_row;
}
}
const uiOptions = getUiOptions<T, S, F>(uiSchema); const uiOptions = getUiOptions<T, S, F>(uiSchema);
const ArrayFieldDescriptionTemplate = getTemplate<'ArrayFieldDescriptionTemplate', T, S, F>( const ArrayFieldDescriptionTemplate = getTemplate<'ArrayFieldDescriptionTemplate', T, S, F>(
'ArrayFieldDescriptionTemplate', 'ArrayFieldDescriptionTemplate',
@@ -48,6 +43,18 @@ export default function ArrayFieldTemplate<
const { const {
ButtonTemplates: { AddButton }, ButtonTemplates: { AddButton },
} = registry.templates; } = registry.templates;
let gridSize = 12;
let numbered = false
if (schema.props) {
if (schema.props.hasOwnProperty("items_per_row")) {
gridSize = gridSize / schema.props.items_per_row;
}
if (schema.props.hasOwnProperty("numbered")) {
numbered = schema.props.numbered;
}
}
return ( return (
<Paper elevation={2}> <Paper elevation={2}>
<Box p={2}> <Box p={2}>
@@ -68,8 +75,13 @@ export default function ArrayFieldTemplate<
/> />
<Grid2 container justifyContent='flex-end'> <Grid2 container justifyContent='flex-end'>
{items && {items &&
items.map(({ key, ...itemProps }: ArrayFieldTemplateItemType<T, S, F>) => ( items.map(({ key, ...itemProps }: ArrayFieldTemplateItemType<T, S, F>, index) => (
<Grid2 key={key} size={gridSize} ><ArrayFieldItemTemplate key={key} {...itemProps} /></Grid2> <Grid2 key={key} size={gridSize} >
<Stack direction="row" sx={{alignItems: "center"}}>
{numbered &&<Typography variant="h4">{index + 1}</Typography>}
<ArrayFieldItemTemplate key={key} {...itemProps} />
</Stack>
</Grid2>
))} ))}
</Grid2> </Grid2>
{canAdd && ( {canAdd && (