fix(striker-ui): add render when List is empty

main
Tsu-ba-me 2 years ago
parent 556b550f2c
commit 68f019414b
  1. 59
      striker-ui/components/List.tsx

@ -33,6 +33,7 @@ type ListOptionalProps<T = unknown> = {
isAllowEditItem?: boolean; isAllowEditItem?: boolean;
isEdit?: boolean; isEdit?: boolean;
isScroll?: boolean; isScroll?: boolean;
listEmpty?: ReactNode;
listItemKeyPrefix?: string; listItemKeyPrefix?: string;
listItemProps?: MUIListItemProps; listItemProps?: MUIListItemProps;
listProps?: MUIListProps; listProps?: MUIListProps;
@ -56,6 +57,7 @@ const LIST_DEFAULT_PROPS: Required<
| 'isAllowCheckItem' | 'isAllowCheckItem'
| 'isAllowDelete' | 'isAllowDelete'
| 'isAllowEditItem' | 'isAllowEditItem'
| 'listEmpty'
| 'onAdd' | 'onAdd'
| 'onDelete' | 'onDelete'
| 'onEdit' | 'onEdit'
@ -69,6 +71,7 @@ const LIST_DEFAULT_PROPS: Required<
| 'isAllowCheckItem' | 'isAllowCheckItem'
| 'isAllowDelete' | 'isAllowDelete'
| 'isAllowEditItem' | 'isAllowEditItem'
| 'listEmpty'
| 'onAdd' | 'onAdd'
| 'onDelete' | 'onDelete'
| 'onEdit' | 'onEdit'
@ -82,6 +85,7 @@ const LIST_DEFAULT_PROPS: Required<
isAllowEditItem: undefined, isAllowEditItem: undefined,
isEdit: false, isEdit: false,
isScroll: false, isScroll: false,
listEmpty: undefined,
listItemKeyPrefix: uuidv4(), listItemKeyPrefix: uuidv4(),
listItemProps: {}, listItemProps: {},
listProps: {}, listProps: {},
@ -97,6 +101,7 @@ const List = <T,>({
isAllowEdit = LIST_DEFAULT_PROPS.isAllowEdit, isAllowEdit = LIST_DEFAULT_PROPS.isAllowEdit,
isEdit = LIST_DEFAULT_PROPS.isEdit, isEdit = LIST_DEFAULT_PROPS.isEdit,
isScroll = LIST_DEFAULT_PROPS.isScroll, isScroll = LIST_DEFAULT_PROPS.isScroll,
listEmpty = LIST_DEFAULT_PROPS.listEmpty,
listItemKeyPrefix = LIST_DEFAULT_PROPS.listItemKeyPrefix, listItemKeyPrefix = LIST_DEFAULT_PROPS.listItemKeyPrefix,
listItemProps: { listItemProps: {
sx: listItemSx, sx: listItemSx,
@ -177,6 +182,15 @@ const List = <T,>({
), ),
[addItemButton, deleteItemButton, editItemButton, header], [addItemButton, deleteItemButton, editItemButton, header],
); );
const listEmptyElement = useMemo(
() =>
typeof listEmpty === 'string' ? (
<BodyText>{listEmpty}</BodyText>
) : (
listEmpty
),
[listEmpty],
);
const listItemCheckbox = useMemo( const listItemCheckbox = useMemo(
() => () =>
isEdit && isAllowCheckItem ? ( isEdit && isAllowCheckItem ? (
@ -186,27 +200,30 @@ const List = <T,>({
) : undefined, ) : undefined,
[isAllowCheckItem, isEdit, onItemCheckboxChange], [isAllowCheckItem, isEdit, onItemCheckboxChange],
); );
const listItemElements = useMemo( const listItemElements = useMemo(() => {
() => const entries = Object.entries(listItems);
Object.entries(listItems).map(([key, value]) => (
<MUIListItem return entries.length > 0
{...restListItemProps} ? entries.map(([key, value]) => (
key={`${listItemKeyPrefix}-${key}`} <MUIListItem
sx={{ paddingLeft: 0, paddingRight: 0, ...listItemSx }} {...restListItemProps}
> key={`${listItemKeyPrefix}-${key}`}
{listItemCheckbox} sx={{ paddingLeft: 0, paddingRight: 0, ...listItemSx }}
{renderListItem(key, value)} >
</MUIListItem> {listItemCheckbox}
)), {renderListItem(key, value)}
[ </MUIListItem>
listItemCheckbox, ))
listItemKeyPrefix, : listEmptyElement;
listItems, }, [
listItemSx, listEmptyElement,
renderListItem, listItemCheckbox,
restListItemProps, listItemKeyPrefix,
], listItems,
); listItemSx,
renderListItem,
restListItemProps,
]);
const listScrollSx: SxProps<Theme> | undefined = useMemo( const listScrollSx: SxProps<Theme> | undefined = useMemo(
() => (isScroll ? { maxHeight: '100%', overflowY: 'scroll' } : undefined), () => (isScroll ? { maxHeight: '100%', overflowY: 'scroll' } : undefined),
[isScroll], [isScroll],

Loading…
Cancel
Save