import * as React from 'react'; import { memo, useContext, useEffect, useState } from 'react'; import _ from 'lodash'; import { useJsonForms, withArrayTranslationProps, withJsonFormsArrayLayoutProps, withTranslateProps, } from '@jsonforms/react'; import { DataContext } from '../../context/Context'; import { Box, styled } from '@mui/system'; import { Accordion, AccordionDetails, AccordionProps, AccordionSummary, AccordionSummaryProps, IconButton, Typography, } from '@mui/material'; import ArrowRightIcon from '@mui/icons-material/ArrowRight'; import Grid from '@mui/material/Grid2'; import ArrayLayout from './ArrayLayout'; import { ButtonIcon } from '../../common/ButtonIcon'; import { createDefaultValue } from '@jsonforms/core'; const StyledAccordion = styled((props: AccordionProps) => ( ))(({ theme }) => ({ border: `none`, })); const StyledAccordionSummary = styled((props: AccordionSummaryProps) => ( } {...props} /> ))(({ theme }: any) => ({ flexDirection: 'row', justifyContent: 'start', border: 'none', '& .MuiAccordionSummary-expandIconWrapper': { '& .MuiSvgIcon-root': { fontSize: theme.typography.fontSize * 2.3, color: theme.palette.grey.A400, }, }, '& .MuiAccordionSummary-expandIconWrapper.Mui-expanded': { transform: 'rotate(90deg)', }, '& .MuiAccordionSummary-content': { flexGrow: 0, }, })); const Array = function (props: any) { const { uischema, path, data, schema, enabled, renderers, rootSchema, cells, addItem, removeItems, } = props; // console.log(schema) const [expanded, setExpanded] = React.useState('panel'); const { theme, handleToggleArray, arrayExpendState, serviceProvider } = useContext(DataContext); useEffect(() => { handleToggleArray(path, uischema.config?.main?.allExpanded ?? false); }, [uischema.config?.main?.allExpanded]); const ctx = useJsonForms(); const dynamicStyle = React.useMemo(() => { return serviceProvider( ctx, { onClick: uischema.config?.main?.styleFunction || 'getStyle', }, { event: { _reactName: 'onClick' }, path, }, ); }, [data, path]); const componentStyle = { ...uischema.config?.style, ...dynamicStyle }; const handleAccordionChange = () => (event: React.SyntheticEvent, newExpanded: boolean) => { setExpanded(newExpanded ? 'panel' : false); }; const handleChildAccordionMaximize = () => { handleToggleArray(path, true); }; const handleChildAccordionMinimize = () => { handleToggleArray(path, false); }; const handleDelete = (index: number) => { removeItems(path, [index])(); }; return ( {uischema?.config?.main?.label && ( {uischema.config?.main?.label} )} {data.length === 0 && 'No data'} {!uischema?.config?.main?.disableAddButton && ( {' '} {ButtonIcon('TableAddIcon', {})} Add )} {!uischema?.config?.main?.disableExpandAllButton && ( <> { e.preventDefault(); e.stopPropagation(); handleChildAccordionMaximize(); }} sx={{ textAlign: 'center', padding: '8px 4px 4px', color: data.length === 0 ? `${theme.myTheme.palette.common.black}80` : theme.myTheme.palette.common.black, fill: data.length === 0 ? `${theme.myTheme.palette.common.black}80` : theme.myTheme.palette.common.black, display: 'flex', flexDirection: 'column', justifyContent: 'center', '&:hover': { color: data.length === 0 ? `${theme.myTheme.palette.common.black}80` : theme.myTheme.palette.primary.main, fill: data.length === 0 ? `${theme.myTheme.palette.common.black}80` : theme.myTheme.palette.primary.main, ...uischema.config.style?.ExpandAllButtontyle, }, }} > {' '} {ButtonIcon('Maximize', {})} Maximise All { e.preventDefault(); e.stopPropagation(); handleChildAccordionMinimize(); }} sx={{ textAlign: 'center', padding: '8px 4px 4px', color: data.length === 0 ? `${theme.myTheme.palette.common.black}80` : theme.myTheme.palette.common.black, fill: data.length === 0 ? `${theme.myTheme.palette.common.black}80` : theme.myTheme.palette.common.black, display: 'flex', flexDirection: 'column', justifyContent: 'center', '&:hover': { color: data.length === 0 ? `${theme.myTheme.palette.common.black}80` : theme.myTheme.palette.primary.main, fill: data.length === 0 ? `${theme.myTheme.palette.common.black}80` : theme.myTheme.palette.primary.main, }, }} > {ButtonIcon('Minimize', {})} Minimise All )} {_.range(data)?.map((item: any, index: number) => { return ( ); })} ); }; export default withJsonFormsArrayLayoutProps( withTranslateProps(withArrayTranslationProps(Array)), );