import { Add as MUIAddIcon, Delete as MUIDeleteIcon, Done as MUIDoneIcon, Edit as MUIEditIcon, } from '@mui/icons-material'; import { Box as MUIBox, List as MUIList, ListItem as MUIListItem, ListItemButton, ListItemIcon as MUIListItemIcon, SxProps, Theme, } from '@mui/material'; import { FC, forwardRef, useCallback, useMemo } from 'react'; import { v4 as uuidv4 } from 'uuid'; import { BLUE, BORDER_RADIUS, GREY, RED } from '../lib/consts/DEFAULT_THEME'; import Checkbox from './Checkbox'; import Divider from './Divider'; import FlexBox from './FlexBox'; import IconButton from './IconButton'; import Spinner from './Spinner'; import { BodyText } from './Text'; const List = forwardRef( ({ allowCheckAll: isAllowCheckAll = false, allowEdit: isAllowEdit = false, allowItemButton: isAllowItemButton = false, disableDelete = false, edit: isEdit = false, flexBoxProps, getListCheckboxProps, getListItemCheckboxProps, header, headerSpacing = '.3em', insertHeader: isInsertHeader = true, listEmpty, listItemIconMinWidth = '56px', listItemKeyPrefix = uuidv4(), listItemProps: { sx: listItemSx, ...restListItemProps } = {}, listItems, listProps: { sx: listSx, ...restListProps } = {}, loading, onAdd, onDelete, onEdit, onAllCheckboxChange, onItemCheckboxChange, onItemClick, renderListItem = (key) => {key}, renderListItemCheckboxState, scroll: isScroll = false, // Input props that depend on other input props. allowAddItem: isAllowAddItem = isAllowEdit, allowCheckItem: isAllowCheckItem = isAllowEdit, allowDelete: isAllowDelete = isAllowEdit, allowEditItem: isAllowEditItem = isAllowEdit, }: ListProps) => { const checkAllMinWidth = useMemo( () => `calc(${listItemIconMinWidth} - ${headerSpacing})`, [headerSpacing, listItemIconMinWidth], ); const addItemButton = useMemo( () => isAllowAddItem ? ( ) : undefined, [isAllowAddItem, onAdd], ); const deleteItemButton = useMemo( () => isEdit && isAllowDelete ? ( ) : undefined, [disableDelete, isAllowDelete, isEdit, onDelete], ); const editItemButton = useMemo(() => { if (isAllowEditItem) { return ( {isEdit ? : } ); } return undefined; }, [isAllowEditItem, isEdit, onEdit]); const checkAllElement = useMemo(() => { let element; if (isEdit && isAllowCheckItem) { element = isAllowCheckAll ? ( ) : ( ); } return element; }, [ checkAllMinWidth, getListCheckboxProps, isAllowCheckAll, isAllowCheckItem, isEdit, onAllCheckboxChange, ]); const headerElement = useMemo(() => { const headerType = typeof header; return isInsertHeader && header ? ( {checkAllElement} {['boolean', 'string'].includes(headerType) ? ( <> {headerType === 'string' && {header}} ) : ( header )} {deleteItemButton} {editItemButton} {addItemButton} ) : ( header ); }, [ addItemButton, checkAllElement, deleteItemButton, editItemButton, header, headerSpacing, isInsertHeader, ]); const listEmptyElement = useMemo( () => typeof listEmpty === 'string' ? ( {listEmpty} ) : ( listEmpty ), [listEmpty], ); const listItemCheckbox = useCallback( (key: string, checked?: boolean, props?: CheckboxProps) => isEdit && isAllowCheckItem ? ( onItemCheckboxChange?.call(null, key, ...args) } {...props} /> ) : undefined, [isAllowCheckItem, isEdit, listItemIconMinWidth, onItemCheckboxChange], ); const listItemElements = useMemo(() => { if (loading) return ; if (!listItems) return listEmptyElement; const entries = Object.entries(listItems); if (entries.length <= 0) return listEmptyElement; return entries.map(([key, value]) => { const listItem = renderListItem(key, value); return ( {listItemCheckbox( key, renderListItemCheckboxState?.call(null, key, value), getListItemCheckboxProps?.call(null, key, value), )} {isAllowItemButton ? ( { onItemClick?.call(null, value, key, ...args); }} sx={{ borderRadius: BORDER_RADIUS }} > {listItem} ) : ( listItem )} ); }); }, [ loading, listItems, listEmptyElement, renderListItem, restListItemProps, listItemKeyPrefix, listItemSx, listItemCheckbox, renderListItemCheckboxState, getListItemCheckboxProps, isAllowItemButton, onItemClick, ]); const listScrollSx: SxProps | undefined = useMemo( () => (isScroll ? { maxHeight: '100%', overflowY: 'scroll' } : undefined), [isScroll], ); return ( {headerElement} {listItemElements} ); }, ); List.displayName = 'List'; export default List as (props: ListProps) => ReturnType>>;