Skip to content

material-renderers: Validation Icon on individual ExpandPanel, ListWithDetail items #2456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/core/src/mappers/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,10 +791,20 @@ export const mapStateToMasterListItemProps = (
state.jsonforms.i18n.translate,
uischema
);
const subErrors = getSubErrorsAt(childPath, schema)(state);
const childErrors = getCombinedErrorMessage(
subErrors,
getErrorTranslator()(state),
getTranslator()(state),
undefined,
undefined,
undefined
);

return {
...ownProps,
childLabel,
childErrors,
};
};

Expand Down Expand Up @@ -823,6 +833,7 @@ export interface OwnPropsOfMasterListItem {

export interface StatePropsOfMasterItem extends OwnPropsOfMasterListItem {
childLabel: string;
childErrors: string;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ import {
ListItemSecondaryAction,
ListItemText,
Tooltip,
SxProps,
} from '@mui/material';
import DeleteIcon from '@mui/icons-material/Delete';
import React from 'react';
import ValidationIcon from '../complex/ValidationIcon';

const avatarSx: SxProps = { display: 'inline-flex', marginRight: '.25rem' };

export const ListWithDetailMasterItem = ({
index,
childLabel,
childErrors,
selected,
enabled,
handleSelect,
Expand All @@ -50,7 +55,10 @@ export const ListWithDetailMasterItem = ({
return (
<ListItemButton selected={selected} onClick={handleSelect(index)}>
<ListItemAvatar>
<Avatar aria-label='Index'>{index + 1}</Avatar>
<Avatar aria-label='Index' sx={avatarSx}>
{index + 1}
</Avatar>
<ValidationIcon id='tooltip-validation' errorMessages={childErrors} />
</ListItemAvatar>
<ListItemText primary={childLabel} />
{enabled && !disableRemove && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,16 @@ export const MaterialListWithDetailRenderer = ({
}: ArrayLayoutProps & { translations: ArrayTranslations }) => {
const [selectedIndex, setSelectedIndex] = useState(undefined);
const handleRemoveItem = useCallback(
(p: string, value: any) => () => {
(p: string, value: any) => (e?: React.MouseEvent) => {
e?.stopPropagation();
removeItems(p, [value])();
if (selectedIndex === value) {
setSelectedIndex(undefined);
} else if (selectedIndex > value) {
setSelectedIndex(selectedIndex - 1);
}
},
[removeItems, setSelectedIndex]
[removeItems, setSelectedIndex, selectedIndex]
);
const handleListItemClick = useCallback(
(index: number) => () => setSelectedIndex(index),
Expand Down
37 changes: 34 additions & 3 deletions packages/material-renderers/src/layouts/ExpandPanelRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import {
ArrayTranslations,
computeChildLabel,
UpdateArrayContext,
subErrorsAt,
fetchErrorTranslator,
fetchTranslator,
getCombinedErrorMessage,
} from '@jsonforms/core';
import {
Accordion,
Expand All @@ -39,13 +43,16 @@ import {
Grid,
IconButton,
Tooltip,
SxProps,
} from '@mui/material';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import ArrowUpward from '@mui/icons-material/ArrowUpward';
import ArrowDownward from '@mui/icons-material/ArrowDownward';
import DeleteIcon from '@mui/icons-material/Delete';
import ValidationIcon from '../complex/ValidationIcon';

const iconStyle: any = { float: 'right' };
const avatarSx: SxProps = { display: 'inline-flex', marginRight: '.25rem' };

interface OwnPropsOfExpandPanel {
enabled: boolean;
Expand All @@ -70,6 +77,7 @@ interface OwnPropsOfExpandPanel {
interface StatePropsOfExpandPanel extends OwnPropsOfExpandPanel {
childLabel: string;
childPath: string;
childErrors: string;
enableMoveUp: boolean;
enableMoveDown: boolean;
}
Expand Down Expand Up @@ -100,6 +108,7 @@ const ExpandPanelRendererComponent = (props: ExpandPanelProps) => {
enabled,
childLabel,
childPath,
childErrors,
index,
expanded,
moveDown,
Expand Down Expand Up @@ -146,11 +155,19 @@ const ExpandPanelRendererComponent = (props: ExpandPanelProps) => {
onChange={handleExpansion(childPath)}
>
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
<Grid container alignItems={'center'}>
<Grid container alignItems='center'>
<Grid item xs={7} md={9}>
<Grid container alignItems={'center'}>
<Grid container alignItems='center'>
<Grid item xs={2} md={1}>
<Avatar aria-label='Index'>{index + 1}</Avatar>
<Avatar aria-label='Index' sx={avatarSx}>
{index + 1}
</Avatar>
{childErrors.length !== 0 && (
<ValidationIcon
id='tooltip-validation'
errorMessages={childErrors}
/>
)}
</Grid>
<Grid item xs={10} md={11}>
<span id={labelHtmlId}>{childLabel}</span>
Expand Down Expand Up @@ -379,12 +396,26 @@ export const withContextToExpandPanelProps = (
uischema,
]);

const childErrors = useMemo(
() =>
getCombinedErrorMessage(
subErrorsAt(childPath, schema)(ctx.core),
fetchErrorTranslator(ctx.i18n),
fetchTranslator(ctx.i18n),
undefined,
undefined,
undefined
),
[ctx.core.errors, ctx.core.validationMode, ctx.i18n, schema, childPath]
);

return (
<Component
{...props}
{...dispatchProps}
childLabel={childLabel}
childPath={childPath}
childErrors={childErrors}
uischemas={uischemas}
/>
);
Expand Down