fix(striker-ui): allow override render entry value, skip uuid entries in form summary

main
Tsu-ba-me 9 months ago
parent 19f819ec61
commit 60da16f6f6
  1. 82
      striker-ui/components/FormSummary.tsx
  2. 11
      striker-ui/types/FormSummary.d.ts

@ -1,9 +1,9 @@
import { Box, List as MUIList, ListItem as MUIListItem } from '@mui/material'; import { Box, List as MUIList, ListItem as MUIListItem } from '@mui/material';
import { FC, ReactElement } from 'react'; import { FC, ReactElement } from 'react';
import disassembleCamel from '../lib/disassembleCamel';
import FlexBox from './FlexBox'; import FlexBox from './FlexBox';
import { BodyText, MonoText, SensitiveText } from './Text'; import { BodyText, MonoText, SensitiveText } from './Text';
import disassembleCamel from '../lib/disassembleCamel';
const renderEntryValueWithMono: RenderFormValueFunction = ({ entry }) => ( const renderEntryValueWithMono: RenderFormValueFunction = ({ entry }) => (
<MonoText whiteSpace="nowrap">{String(entry)}</MonoText> <MonoText whiteSpace="nowrap">{String(entry)}</MonoText>
@ -19,12 +19,43 @@ const renderEntryValueWithPassword: RenderFormValueFunction = (args) => {
); );
}; };
const buildEntryList = ({ const renderEntryValueBase: RenderFormValueFunction = (args) => {
const { entry, hasPassword } = args;
if (['', null, undefined].some((bad) => entry === bad)) {
return <BodyText>none</BodyText>;
}
return hasPassword
? renderEntryValueWithPassword(args)
: renderEntryValueWithMono(args);
};
const renderEntryBase: RenderFormEntryFunction = (args) => {
const { depth, entry, getLabel, hasPassword, key, nest, renderValue } = args;
return (
<FlexBox fullWidth growFirst row maxWidth="100%">
<BodyText>
{getLabel({ cap: disassembleCamel, depth, entry, hasPassword, key })}
</BodyText>
<Box sx={{ maxWidth: '100%', overflowX: 'scroll' }}>
{!nest &&
renderValue(renderEntryValueBase, { depth, entry, hasPassword, key })}
</Box>
</FlexBox>
);
};
const skipBase: SkipFormEntryFunction = ({ key }) => !/confirm|uuid/i.test(key);
const buildEntryList = <T extends FormEntries>({
depth = 0, depth = 0,
entries, entries,
getEntryLabel, getEntryLabel,
getListProps, getListProps,
getListItemProps, getListItemProps,
hasPassword,
listKey, listKey,
maxDepth, maxDepth,
renderEntry, renderEntry,
@ -37,11 +68,12 @@ const buildEntryList = ({
getListProps?: GetFormEntriesPropsFunction; getListProps?: GetFormEntriesPropsFunction;
getListItemProps?: GetFormEntryPropsFunction; getListItemProps?: GetFormEntryPropsFunction;
listKey?: string; listKey?: string;
maxDepth: number; } & Required<
renderEntry: RenderFormEntryFunction; Pick<
renderEntryValue: RenderFormValueFunction; FormSummaryProps<T>,
skip: Exclude<FormSummaryOptionalProps['skip'], undefined>; 'hasPassword' | 'maxDepth' | 'renderEntry' | 'renderEntryValue' | 'skip'
}): ReactElement => { >
>): ReactElement => {
const result: ReactElement[] = []; const result: ReactElement[] = [];
Object.entries(entries).forEach(([itemKey, entry]) => { Object.entries(entries).forEach(([itemKey, entry]) => {
@ -54,10 +86,11 @@ const buildEntryList = ({
const fnArgs: CommonFormEntryHandlerArgs = { const fnArgs: CommonFormEntryHandlerArgs = {
depth, depth,
entry: value, entry: value,
hasPassword,
key: itemKey, key: itemKey,
}; };
if (skip(({ key }) => !/confirm/i.test(key), fnArgs)) { if (skip(skipBase, fnArgs)) {
result.push( result.push(
<MUIListItem <MUIListItem
key={itemId} key={itemId}
@ -68,6 +101,7 @@ const buildEntryList = ({
depth, depth,
entry: value, entry: value,
getLabel: getEntryLabel, getLabel: getEntryLabel,
hasPassword,
key: itemKey, key: itemKey,
nest, nest,
renderValue: renderEntryValue, renderValue: renderEntryValue,
@ -78,10 +112,11 @@ const buildEntryList = ({
if (nest && depth < maxDepth) { if (nest && depth < maxDepth) {
result.push( result.push(
buildEntryList({ buildEntryList<T>({
depth: depth + 1, depth: depth + 1,
entries: entry, entries: entry,
getEntryLabel, getEntryLabel,
hasPassword,
listKey: itemKey, listKey: itemKey,
maxDepth, maxDepth,
renderEntry, renderEntry,
@ -111,37 +146,18 @@ const FormSummary = <T extends FormEntries>({
getEntryLabel = ({ cap, key }) => cap(key), getEntryLabel = ({ cap, key }) => cap(key),
getListProps, getListProps,
getListItemProps, getListItemProps,
hasPassword, hasPassword = false,
maxDepth = 3, maxDepth = 3,
renderEntry = ({ depth, entry, getLabel, key, nest, renderValue }) => ( renderEntry = renderEntryBase,
<FlexBox fullWidth growFirst row maxWidth="100%"> renderEntryValue = (base, ...args) => base(...args),
<BodyText>
{getLabel({ cap: disassembleCamel, depth, entry, key })}
</BodyText>
<Box sx={{ maxWidth: '100%', overflowX: 'scroll' }}>
{!nest && renderValue({ depth, entry, key })}
</Box>
</FlexBox>
),
// Prop(s) that rely on other(s).
renderEntryValue = (args) => {
const { entry } = args;
if (['', null, undefined].some((bad) => entry === bad)) {
return <BodyText>none</BodyText>;
}
return hasPassword
? renderEntryValueWithPassword(args)
: renderEntryValueWithMono(args);
},
skip = (base, ...args) => base(...args), skip = (base, ...args) => base(...args),
}: FormSummaryProps<T>): ReturnType<FC<FormSummaryProps<T>>> => }: FormSummaryProps<T>): ReturnType<FC<FormSummaryProps<T>>> =>
buildEntryList({ buildEntryList<T>({
entries, entries,
getEntryLabel, getEntryLabel,
getListProps, getListProps,
getListItemProps, getListItemProps,
hasPassword,
maxDepth, maxDepth,
renderEntry, renderEntry,
renderEntryValue, renderEntryValue,

@ -7,6 +7,7 @@ type FormEntries = {
type CommonFormEntryHandlerArgs = { type CommonFormEntryHandlerArgs = {
depth: number; depth: number;
entry: FormEntry; entry: FormEntry;
hasPassword: boolean;
key: string; key: string;
}; };
@ -36,7 +37,10 @@ type RenderFormEntryFunction = (
args: CommonFormEntryHandlerArgs & { args: CommonFormEntryHandlerArgs & {
getLabel: GetFormEntryLabelFunction; getLabel: GetFormEntryLabelFunction;
nest: boolean; nest: boolean;
renderValue: RenderFormValueFunction; renderValue: (
base: RenderFormValueFunction,
...rfvargs: Parameters<RenderFormValueFunction>
) => ReturnType<RenderFormValueFunction>;
}, },
) => import('react').ReactElement; ) => import('react').ReactElement;
@ -49,7 +53,10 @@ type FormSummaryOptionalProps = {
hasPassword?: boolean; hasPassword?: boolean;
maxDepth?: number; maxDepth?: number;
renderEntry?: RenderFormEntryFunction; renderEntry?: RenderFormEntryFunction;
renderEntryValue?: RenderFormValueFunction; renderEntryValue?: (
base: RenderFormValueFunction,
...args: Parameters<RenderFormValueFunction>
) => ReturnType<RenderFormValueFunction>;
skip?: ( skip?: (
base: SkipFormEntryFunction, base: SkipFormEntryFunction,
...args: Parameters<SkipFormEntryFunction> ...args: Parameters<SkipFormEntryFunction>

Loading…
Cancel
Save