import { Box, List as MUIList, ListItem as MUIListItem } from '@mui/material';
import { capitalize } from 'lodash';
import { FC, ReactElement } from 'react';
import FlexBox from './FlexBox';
import { BodyText, MonoText, SensitiveText } from './Text';
const capEntryLabel: CapFormEntryLabel = (value) => {
const spaced = value.replace(/([a-z\d])([A-Z])/g, '$1 $2');
const lcased = spaced.toLowerCase();
return capitalize(lcased);
};
const renderEntryValueWithMono: RenderFormValueFunction = ({ entry }) => (
{String(entry)}
);
const renderEntryValueWithPassword: RenderFormValueFunction = (args) => {
const { entry, key } = args;
return /passw/i.test(key) ? (
{String(entry)}
) : (
renderEntryValueWithMono(args)
);
};
const buildEntryList = ({
depth = 0,
entries,
getEntryLabel,
getListProps,
getListItemProps,
listKey,
maxDepth,
renderEntry,
renderEntryValue,
}: {
depth?: number;
entries: FormEntries;
getEntryLabel: GetFormEntryLabelFunction;
getListProps?: GetFormEntriesPropsFunction;
getListItemProps?: GetFormEntryPropsFunction;
listKey?: string;
maxDepth: number;
renderEntry: RenderFormEntryFunction;
renderEntryValue: RenderFormValueFunction;
}): ReactElement => {
const result: ReactElement[] = [];
Object.entries(entries).forEach(([itemKey, entry]) => {
const itemId = `form-summary-entry-${itemKey}`;
const nest = entry !== null && typeof entry === 'object';
const value = nest ? null : entry;
result.push(
{renderEntry({
depth,
entry: value,
getLabel: getEntryLabel,
key: itemKey,
nest,
renderValue: renderEntryValue,
})}
,
);
if (nest && depth < maxDepth) {
result.push(
buildEntryList({
depth: depth + 1,
entries: entry,
getEntryLabel,
listKey: itemKey,
maxDepth,
renderEntry,
renderEntryValue,
}),
);
}
});
const listId = `form-summary-list-${listKey ?? 'root'}`;
return (
{result}
);
};
const FormSummary = ({
entries,
getEntryLabel = ({ cap, key }) => cap(key),
getListProps,
getListItemProps,
hasPassword,
maxDepth = 3,
renderEntry = ({ depth, entry, getLabel, key, nest, renderValue }) => (
{getLabel({ cap: capEntryLabel, depth, entry, key })}
{!nest && renderValue({ depth, entry, key })}
),
// Prop(s) that rely on other(s).
renderEntryValue = (args) => {
const { entry } = args;
if (['', null, undefined].some((bad) => entry === bad)) {
return none;
}
return hasPassword
? renderEntryValueWithPassword(args)
: renderEntryValueWithMono(args);
},
}: FormSummaryProps): ReturnType>> =>
buildEntryList({
entries,
getEntryLabel,
getListProps,
getListItemProps,
maxDepth,
renderEntry,
renderEntryValue,
});
export default FormSummary;