fix(striker-ui): allow optional item button in List

main
Tsu-ba-me 2 years ago
parent 604b95ffa9
commit 1c28f4028c
  1. 56
      striker-ui/components/List.tsx
  2. 23
      striker-ui/types/List.d.ts

@ -8,6 +8,7 @@ import {
Box as MUIBox, Box as MUIBox,
List as MUIList, List as MUIList,
ListItem as MUIListItem, ListItem as MUIListItem,
ListItemButton,
ListItemIcon as MUIListItemIcon, ListItemIcon as MUIListItemIcon,
SxProps, SxProps,
Theme, Theme,
@ -23,7 +24,7 @@ import {
} from 'react'; } from 'react';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { BLUE, GREY, RED } from '../lib/consts/DEFAULT_THEME'; import { BLUE, BORDER_RADIUS, GREY, RED } from '../lib/consts/DEFAULT_THEME';
import Checkbox from './Checkbox'; import Checkbox from './Checkbox';
import Divider from './Divider'; import Divider from './Divider';
@ -36,6 +37,7 @@ const List = forwardRef(
{ {
allowCheckAll: isAllowCheckAll = false, allowCheckAll: isAllowCheckAll = false,
allowEdit: isAllowEdit = false, allowEdit: isAllowEdit = false,
allowItemButton: isAllowItemButton = false,
edit: isEdit = false, edit: isEdit = false,
flexBoxProps, flexBoxProps,
header, header,
@ -53,6 +55,7 @@ const List = forwardRef(
onEdit, onEdit,
onAllCheckboxChange, onAllCheckboxChange,
onItemCheckboxChange, onItemCheckboxChange,
onItemClick,
renderListItem = (key) => <BodyText>{key}</BodyText>, renderListItem = (key) => <BodyText>{key}</BodyText>,
renderListItemCheckboxState, renderListItemCheckboxState,
scroll: isScroll = false, scroll: isScroll = false,
@ -203,32 +206,49 @@ const List = forwardRef(
const entries = Object.entries(listItems); const entries = Object.entries(listItems);
if (entries.length > 0) { if (entries.length > 0) {
result = entries.map(([key, value]) => ( result = entries.map(([key, value]) => {
<MUIListItem const listItem = renderListItem(key, value);
{...restListItemProps}
key={`${listItemKeyPrefix}-${key}`} return (
sx={{ paddingLeft: 0, paddingRight: 0, ...listItemSx }} <MUIListItem
> {...restListItemProps}
{listItemCheckbox( key={`${listItemKeyPrefix}-${key}`}
key, sx={{ paddingLeft: 0, paddingRight: 0, ...listItemSx }}
renderListItemCheckboxState?.call(null, key, value), >
)} {listItemCheckbox(
{renderListItem(key, value)} key,
</MUIListItem> renderListItemCheckboxState?.call(null, key, value),
)); )}
{isAllowItemButton ? (
<ListItemButton
onClick={(...args) => {
onItemClick?.call(null, value, key, ...args);
}}
sx={{ borderRadius: BORDER_RADIUS }}
>
{listItem}
</ListItemButton>
) : (
listItem
)}
</MUIListItem>
);
});
} }
} }
return result; return result;
}, [ }, [
listEmptyElement, listEmptyElement,
listItemCheckbox,
listItemKeyPrefix,
listItems, listItems,
listItemSx,
renderListItem, renderListItem,
renderListItemCheckboxState,
restListItemProps, restListItemProps,
listItemKeyPrefix,
listItemSx,
listItemCheckbox,
renderListItemCheckboxState,
isAllowItemButton,
onItemClick,
]); ]);
const listScrollSx: SxProps<Theme> | undefined = useMemo( const listScrollSx: SxProps<Theme> | undefined = useMemo(
() => (isScroll ? { maxHeight: '100%', overflowY: 'scroll' } : undefined), () => (isScroll ? { maxHeight: '100%', overflowY: 'scroll' } : undefined),

@ -1,12 +1,18 @@
type OnCheckboxChange = Exclude<CheckboxProps['onChange'], undefined>; type CheckboxChangeEventHandler = Exclude<CheckboxProps['onChange'], undefined>;
type ListItemButtonChangeEventHandler = Exclude<
import('@mui/material').ListItemButtonProps['onClick'],
undefined
>;
type ListOptionalProps<T extends unknown = unknown> = { type ListOptionalProps<T extends unknown = unknown> = {
allowCheckAll?: boolean;
allowAddItem?: boolean; allowAddItem?: boolean;
allowCheckAll?: boolean;
allowCheckItem?: boolean; allowCheckItem?: boolean;
allowEdit?: boolean;
allowDelete?: boolean; allowDelete?: boolean;
allowEdit?: boolean;
allowEditItem?: boolean; allowEditItem?: boolean;
allowItemButton?: boolean;
edit?: boolean; edit?: boolean;
flexBoxProps?: import('../components/FlexBox').FlexBoxProps; flexBoxProps?: import('../components/FlexBox').FlexBoxProps;
header?: import('react').ReactNode; header?: import('react').ReactNode;
@ -22,11 +28,16 @@ type ListOptionalProps<T extends unknown = unknown> = {
onAdd?: import('../components/IconButton').IconButtonProps['onClick']; onAdd?: import('../components/IconButton').IconButtonProps['onClick'];
onDelete?: import('../components/IconButton').IconButtonProps['onClick']; onDelete?: import('../components/IconButton').IconButtonProps['onClick'];
onEdit?: import('../components/IconButton').IconButtonProps['onClick']; onEdit?: import('../components/IconButton').IconButtonProps['onClick'];
onAllCheckboxChange?: OnCheckboxChange; onAllCheckboxChange?: CheckboxChangeEventHandler;
onItemCheckboxChange?: ( onItemCheckboxChange?: (
key: string, key: string,
...onChangeParams: Parameters<OnCheckboxChange> ...checkboxChangeEventHandlerArgs: Parameters<CheckboxChangeEventHandler>
) => ReturnType<OnCheckboxChange>; ) => ReturnType<CheckboxChangeEventHandler>;
onItemClick?: (
value: T,
key: string,
...listItemButtonChangeEventHandlerArgs: Parameters<ListItemButtonChangeEventHandler>
) => ReturnType<ListItemButtonChangeEventHandler>;
renderListItem?: (key: string, value: T) => import('react').ReactNode; renderListItem?: (key: string, value: T) => import('react').ReactNode;
renderListItemCheckboxState?: (key: string, value: T) => boolean; renderListItemCheckboxState?: (key: string, value: T) => boolean;
scroll?: boolean; scroll?: boolean;

Loading…
Cancel
Save