import React, { useEffect, useRef } from 'react'; import { styled } from '@mui/system'; import Button from '@mui/material/Button'; import Card from '@mui/material/Card'; import IconButton from '@mui/material/IconButton'; import DeleteIcon from '@mui/icons-material/Close'; import cloneDeep from 'lodash/cloneDeep'; import FormLabel from '@mui/material/FormLabel'; import utils from './utils'; import useSchemaField from './useSchemaField'; const PREFIX = 'ArrayComponent'; const classes = { arrayItem: `${PREFIX}-arrayItem`, deleteItemButton: `${PREFIX}-deleteItemButton`, addButton: `${PREFIX}-addButton`, elementsContainer: `${PREFIX}-elementsContainer`, title: `${PREFIX}-title`, }; const Root = styled('div')(({ theme }) => ({ [`& .${classes.arrayItem}`]: { position: 'relative', padding: theme.spacing(), marginTop: theme.spacing(), display: 'flex', }, [`& .${classes.deleteItemButton}`]: { margin: [[theme.spacing(-1), theme.spacing(-1), 'auto', 'auto']], }, [`& .${classes.addButton}`]: { marginLeft: theme.spacing(), }, [`& .${classes.elementsContainer}`]: { display: 'flex', flexWrap: 'wrap', }, [`& .${classes.title}`]: { margin: 'auto 0', }, })); let SEQUENCE = 1; const nextItemId = () => { SEQUENCE += 1; return SEQUENCE; }; const setIndex = (index) => (form) => { if (form.key) { // eslint-disable-next-line no-param-reassign form.key[form.key.indexOf('')] = index; } }; const copyWithIndex = (form, index) => { const copy = cloneDeep(form); copy.arrayIndex = index; utils.traverseForm(copy, setIndex(index)); return copy; }; const ArrayComponent = (props) => { const { form, builder, model, mapper, onChange, setDefault, options, localization: { getLocalizedString }, } = props; const { value, onChangeValidate } = useSchemaField(props); const arrayModel = value || []; const itemIds = useRef([]); while (itemIds.current.length < arrayModel.length) { itemIds.current.push(nextItemId()); } if (itemIds.current.length > arrayModel.length) { itemIds.current.length = arrayModel.length; } useEffect(() => { setDefault(form.key, model, form, value); }, []); useEffect(() => { // Always start with one empty form unless configured otherwise. if (form.startEmpty !== true && arrayModel.length === 0) { onAppend(); } }, []); const onAppend = () => { let empty; if (form && form.schema && form.schema.items) { const { items } = form.schema; if (items.type && items.type.indexOf('object') !== -1) { empty = {}; // Check for possible defaults if (!options || options.setSchemaDefaults !== false) { empty = typeof items.default !== 'undefined' ? items.default : empty; // Check for defaults further down in the schema. if (empty) { utils.traverseSchema(items, (prop, path) => { if (typeof prop.default !== 'undefined') { utils.selectOrSet(path, empty, prop.default); } }); } } } else if (items.type && items.type.indexOf('array') !== -1) { empty = []; if (!options || options.setSchemaDefaults !== false) { empty = items.default || empty; } } else if (!options || options.setSchemaDefaults !== false) { // No type? could still have defaults. empty = items.default || empty; } } const newModel = [...arrayModel]; itemIds.current.push(nextItemId()); newModel.push(empty); onChangeValidate(null, newModel); }; const onDelete = (index) => () => { const newModel = [...arrayModel]; newModel.splice(index, 1); itemIds.current.splice(index, 1); onChangeValidate(null, newModel); }; const getAddButton = () => { const AddButton = form.AddButton || ((buttonProps) => ( )); return ( {form.add || 'Add'} ); }; const arrays = []; for (let i = 0; i < arrayModel.length; i += 1) { const forms = form.items.map((eachForm, index) => { const copy = copyWithIndex(eachForm, i); return builder(copy, model, index, mapper, onChange, builder, { arrayIndex: i, }); }); arrays.push( {forms} ); } return ( {form.title && getLocalizedString(form.title)} {getAddButton()} {arrays} ); }; export default ArrayComponent;