From 1c28f4028c3cef88fccf6d23317f9bffa15d0946 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 17 Feb 2023 22:04:53 -0500 Subject: [PATCH] fix(striker-ui): allow optional item button in List --- striker-ui/components/List.tsx | 56 +++++++++++++++++++++++----------- striker-ui/types/List.d.ts | 23 ++++++++++---- 2 files changed, 55 insertions(+), 24 deletions(-) diff --git a/striker-ui/components/List.tsx b/striker-ui/components/List.tsx index d2db75f2..e73543e1 100644 --- a/striker-ui/components/List.tsx +++ b/striker-ui/components/List.tsx @@ -8,6 +8,7 @@ import { Box as MUIBox, List as MUIList, ListItem as MUIListItem, + ListItemButton, ListItemIcon as MUIListItemIcon, SxProps, Theme, @@ -23,7 +24,7 @@ import { } from 'react'; 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 Divider from './Divider'; @@ -36,6 +37,7 @@ const List = forwardRef( { allowCheckAll: isAllowCheckAll = false, allowEdit: isAllowEdit = false, + allowItemButton: isAllowItemButton = false, edit: isEdit = false, flexBoxProps, header, @@ -53,6 +55,7 @@ const List = forwardRef( onEdit, onAllCheckboxChange, onItemCheckboxChange, + onItemClick, renderListItem = (key) => {key}, renderListItemCheckboxState, scroll: isScroll = false, @@ -203,32 +206,49 @@ const List = forwardRef( const entries = Object.entries(listItems); if (entries.length > 0) { - result = entries.map(([key, value]) => ( - - {listItemCheckbox( - key, - renderListItemCheckboxState?.call(null, key, value), - )} - {renderListItem(key, value)} - - )); + result = entries.map(([key, value]) => { + const listItem = renderListItem(key, value); + + return ( + + {listItemCheckbox( + key, + renderListItemCheckboxState?.call(null, key, value), + )} + {isAllowItemButton ? ( + { + onItemClick?.call(null, value, key, ...args); + }} + sx={{ borderRadius: BORDER_RADIUS }} + > + {listItem} + + ) : ( + listItem + )} + + ); + }); } } return result; }, [ listEmptyElement, - listItemCheckbox, - listItemKeyPrefix, listItems, - listItemSx, renderListItem, - renderListItemCheckboxState, restListItemProps, + listItemKeyPrefix, + listItemSx, + listItemCheckbox, + renderListItemCheckboxState, + isAllowItemButton, + onItemClick, ]); const listScrollSx: SxProps | undefined = useMemo( () => (isScroll ? { maxHeight: '100%', overflowY: 'scroll' } : undefined), diff --git a/striker-ui/types/List.d.ts b/striker-ui/types/List.d.ts index 329dccd6..0560ca85 100644 --- a/striker-ui/types/List.d.ts +++ b/striker-ui/types/List.d.ts @@ -1,12 +1,18 @@ -type OnCheckboxChange = Exclude; +type CheckboxChangeEventHandler = Exclude; + +type ListItemButtonChangeEventHandler = Exclude< + import('@mui/material').ListItemButtonProps['onClick'], + undefined +>; type ListOptionalProps = { - allowCheckAll?: boolean; allowAddItem?: boolean; + allowCheckAll?: boolean; allowCheckItem?: boolean; - allowEdit?: boolean; allowDelete?: boolean; + allowEdit?: boolean; allowEditItem?: boolean; + allowItemButton?: boolean; edit?: boolean; flexBoxProps?: import('../components/FlexBox').FlexBoxProps; header?: import('react').ReactNode; @@ -22,11 +28,16 @@ type ListOptionalProps = { onAdd?: import('../components/IconButton').IconButtonProps['onClick']; onDelete?: import('../components/IconButton').IconButtonProps['onClick']; onEdit?: import('../components/IconButton').IconButtonProps['onClick']; - onAllCheckboxChange?: OnCheckboxChange; + onAllCheckboxChange?: CheckboxChangeEventHandler; onItemCheckboxChange?: ( key: string, - ...onChangeParams: Parameters - ) => ReturnType; + ...checkboxChangeEventHandlerArgs: Parameters + ) => ReturnType; + onItemClick?: ( + value: T, + key: string, + ...listItemButtonChangeEventHandlerArgs: Parameters + ) => ReturnType; renderListItem?: (key: string, value: T) => import('react').ReactNode; renderListItemCheckboxState?: (key: string, value: T) => boolean; scroll?: boolean;