fix(striker-ui): correct nested appearance in FormSummary

main
Tsu-ba-me 2 years ago
parent 09b69820b4
commit 2ec8031e0e
  1. 59
      striker-ui/components/FormSummary.tsx
  2. 2
      striker-ui/components/ManageManifest/ManageManifestPanel.tsx
  3. 2
      striker-ui/components/ManageUps/ManageUpsPanel.tsx
  4. 2
      striker-ui/components/StrikerConfig/ManageUsersForm.tsx
  5. 40
      striker-ui/types/FormSummary.d.ts

@ -8,7 +8,17 @@ import { FC, ReactElement, createElement } from 'react';
import FlexBox from './FlexBox'; import FlexBox from './FlexBox';
import { BodyText, MonoText, SensitiveText } from './Text'; import { BodyText, MonoText, SensitiveText } from './Text';
const renderEntryValueWithPassword: RenderFormValueFunction = (key, entry) => { const capEntryLabel: CapFormEntryLabel = (value) => {
const spaced = value.replace(/([a-z\d])([A-Z])/g, '$1 $2');
const lcased = spaced.toLowerCase();
return capitalize(lcased);
};
const renderEntryValueWithPassword: RenderFormValueFunction = ({
entry,
key,
}) => {
const textElement = /passw/i.test(key) ? SensitiveText : MonoText; const textElement = /passw/i.test(key) ? SensitiveText : MonoText;
return createElement(textElement, { monospaced: true }, String(entry)); return createElement(textElement, { monospaced: true }, String(entry));
@ -46,10 +56,17 @@ const buildEntryList = ({
result.push( result.push(
<MUIListItem <MUIListItem
key={itemId} key={itemId}
sx={{ paddingLeft: `.${depth * 2}em` }} sx={{ paddingLeft: `${depth}em` }}
{...getListItemProps?.call(null, itemKey, value, depth)} {...getListItemProps?.call(null, { depth, entry: value, key: itemKey })}
> >
{renderEntry(itemKey, value, getEntryLabel, renderEntryValue)} {renderEntry({
depth,
entry: value,
getLabel: getEntryLabel,
key: itemKey,
nest,
renderValue: renderEntryValue,
})}
</MUIListItem>, </MUIListItem>,
); );
@ -73,8 +90,9 @@ const buildEntryList = ({
return ( return (
<MUIList <MUIList
dense dense
disablePadding
key={listId} key={listId}
{...getListProps?.call(null, listKey, entries, depth)} {...getListProps?.call(null, { depth, entries, key: listKey })}
> >
{result} {result}
</MUIList> </MUIList>
@ -83,26 +101,31 @@ const buildEntryList = ({
const FormSummary = <T extends FormEntries>({ const FormSummary = <T extends FormEntries>({
entries, entries,
getEntryLabel = (key) => { getEntryLabel = ({ cap, key }) => cap(key),
const spaced = key.replace(/([a-z\d])([A-Z])/g, '$1 $2');
const lcased = spaced.toLowerCase();
return capitalize(lcased);
},
getListProps, getListProps,
getListItemProps, getListItemProps,
hasPassword, hasPassword,
maxDepth = 3, maxDepth = 3,
renderEntry = (key, entry, getLabel, renderValue) => ( renderEntry = ({ depth, entry, getLabel, key, nest, renderValue }) => (
<FlexBox fullWidth growFirst row> <FlexBox fullWidth growFirst row maxWidth="100%">
<BodyText>{getLabel(key, entry)}</BodyText> <BodyText>{getLabel({ cap: capEntryLabel, depth, entry, key })}</BodyText>
{renderValue(key, entry)} {!nest && renderValue({ depth, entry, key })}
</FlexBox> </FlexBox>
), ),
// Prop(s) that rely on other(s). // Prop(s) that rely on other(s).
renderEntryValue = hasPassword renderEntryValue = (args) => {
? renderEntryValueWithPassword const { entry } = args;
: (key, entry) => <MonoText>{String(entry)}</MonoText>,
if (['', null, undefined].some((bad) => entry === bad)) {
return <BodyText>none</BodyText>;
}
return hasPassword ? (
renderEntryValueWithPassword(args)
) : (
<MonoText>{String(entry)}</MonoText>
);
},
}: FormSummaryProps<T>): ReturnType<FC<FormSummaryProps<T>>> => }: FormSummaryProps<T>): ReturnType<FC<FormSummaryProps<T>>> =>
buildEntryList({ buildEntryList({
entries, entries,

@ -463,7 +463,7 @@ const ManageManifestPanel: FC = () => {
}); });
}, },
getConfirmDialogTitle: (count) => `Delete ${count} manifest(s)?`, getConfirmDialogTitle: (count) => `Delete ${count} manifest(s)?`,
renderEntry: (key) => ( renderEntry: ({ key }) => (
<BodyText>{manifestOverviews?.[key].manifestName}</BodyText> <BodyText>{manifestOverviews?.[key].manifestName}</BodyText>
), ),
}), }),

@ -277,7 +277,7 @@ const ManageUpsPanel: FC = () => {
url: '/ups', url: '/ups',
}); });
}, },
renderEntry: (key) => ( renderEntry: ({ key }) => (
<BodyText>{upsOverviews?.[key].upsName}</BodyText> <BodyText>{upsOverviews?.[key].upsName}</BodyText>
), ),
}), }),

@ -204,7 +204,7 @@ const ManageUsersForm: FC = () => {
}, },
}, },
formSummaryProps: { formSummaryProps: {
renderEntry: (key) => ( renderEntry: ({ key }) => (
<BodyText>{users?.[key].userName}</BodyText> <BodyText>{users?.[key].userName}</BodyText>
), ),
}, },

@ -4,30 +4,40 @@ type FormEntries = {
[key: string]: FormEntries | FormEntry; [key: string]: FormEntries | FormEntry;
}; };
type GetFormEntryLabelFunction = (key: string, entry: FormEntry) => string; type CommonFormEntryHandlerArgs = {
depth: number;
entry: FormEntry;
key: string;
};
type CapFormEntryLabel = (value: string) => string;
type GetFormEntryLabelFunction = (
args: CommonFormEntryHandlerArgs & {
cap: CapFormEntryLabel;
},
) => string;
type GetFormEntryPropsFunction = ( type GetFormEntryPropsFunction = (
key: string, args: CommonFormEntryHandlerArgs,
entry: FormEntry,
depth: number,
) => import('@mui/material').ListItemProps; ) => import('@mui/material').ListItemProps;
type GetFormEntriesPropsFunction = ( type GetFormEntriesPropsFunction = (args: {
key: string | undefined, depth: number;
entries: FormEntries, entries: FormEntries;
depth: number, key?: string;
) => import('@mui/material').ListProps; }) => import('@mui/material').ListProps;
type RenderFormValueFunction = ( type RenderFormValueFunction = (
key: string, args: CommonFormEntryHandlerArgs,
entry: FormEntry,
) => import('react').ReactElement; ) => import('react').ReactElement;
type RenderFormEntryFunction = ( type RenderFormEntryFunction = (
key: string, args: CommonFormEntryHandlerArgs & {
entry: FormEntry, getLabel: GetFormEntryLabelFunction;
getLabel: GetFormEntryLabelFunction, nest: boolean;
renderValue: RenderFormValueFunction, renderValue: RenderFormValueFunction;
},
) => import('react').ReactElement; ) => import('react').ReactElement;
type FormSummaryOptionalProps = { type FormSummaryOptionalProps = {

Loading…
Cancel
Save