|
|
@ -9,8 +9,6 @@ import { |
|
|
|
List as MUIList, |
|
|
|
List as MUIList, |
|
|
|
ListItem as MUIListItem, |
|
|
|
ListItem as MUIListItem, |
|
|
|
ListItemIcon as MUIListItemIcon, |
|
|
|
ListItemIcon as MUIListItemIcon, |
|
|
|
ListItemProps as MUIListItemProps, |
|
|
|
|
|
|
|
ListProps as MUIListProps, |
|
|
|
|
|
|
|
SxProps, |
|
|
|
SxProps, |
|
|
|
Theme, |
|
|
|
Theme, |
|
|
|
} from '@mui/material'; |
|
|
|
} from '@mui/material'; |
|
|
@ -18,7 +16,6 @@ import { |
|
|
|
FC, |
|
|
|
FC, |
|
|
|
ForwardedRef, |
|
|
|
ForwardedRef, |
|
|
|
forwardRef, |
|
|
|
forwardRef, |
|
|
|
ReactNode, |
|
|
|
|
|
|
|
useCallback, |
|
|
|
useCallback, |
|
|
|
useImperativeHandle, |
|
|
|
useImperativeHandle, |
|
|
|
useMemo, |
|
|
|
useMemo, |
|
|
@ -28,127 +25,52 @@ import { v4 as uuidv4 } from 'uuid'; |
|
|
|
|
|
|
|
|
|
|
|
import { BLUE, GREY, RED } from '../lib/consts/DEFAULT_THEME'; |
|
|
|
import { BLUE, GREY, RED } from '../lib/consts/DEFAULT_THEME'; |
|
|
|
|
|
|
|
|
|
|
|
import Checkbox, { CheckboxProps } from './Checkbox'; |
|
|
|
import Checkbox from './Checkbox'; |
|
|
|
import Divider from './Divider'; |
|
|
|
import Divider from './Divider'; |
|
|
|
import FlexBox, { FlexBoxProps } from './FlexBox'; |
|
|
|
import FlexBox from './FlexBox'; |
|
|
|
import IconButton, { IconButtonProps } from './IconButton'; |
|
|
|
import IconButton from './IconButton'; |
|
|
|
import { BodyText } from './Text'; |
|
|
|
import { BodyText } from './Text'; |
|
|
|
|
|
|
|
|
|
|
|
type OnCheckboxChange = Exclude<CheckboxProps['onChange'], undefined>; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ListOptionalPropsWithDefaults<T extends unknown = unknown> = { |
|
|
|
|
|
|
|
allowCheckAll?: boolean; |
|
|
|
|
|
|
|
allowEdit?: boolean; |
|
|
|
|
|
|
|
edit?: boolean; |
|
|
|
|
|
|
|
initialCheckAll?: boolean; |
|
|
|
|
|
|
|
insertHeader?: boolean; |
|
|
|
|
|
|
|
listItemKeyPrefix?: string; |
|
|
|
|
|
|
|
listItemProps?: MUIListItemProps; |
|
|
|
|
|
|
|
listProps?: MUIListProps; |
|
|
|
|
|
|
|
renderListItem?: (key: string, value: T) => ReactNode; |
|
|
|
|
|
|
|
scroll?: boolean; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ListOptionalPropsWithoutDefaults<T extends unknown = unknown> = { |
|
|
|
|
|
|
|
allowAddItem?: boolean; |
|
|
|
|
|
|
|
allowCheckItem?: boolean; |
|
|
|
|
|
|
|
allowDelete?: boolean; |
|
|
|
|
|
|
|
allowEditItem?: boolean; |
|
|
|
|
|
|
|
header?: ReactNode; |
|
|
|
|
|
|
|
listEmpty?: ReactNode; |
|
|
|
|
|
|
|
onAdd?: IconButtonProps['onClick']; |
|
|
|
|
|
|
|
onDelete?: IconButtonProps['onClick']; |
|
|
|
|
|
|
|
onEdit?: IconButtonProps['onClick']; |
|
|
|
|
|
|
|
onAllCheckboxChange?: CheckboxProps['onChange']; |
|
|
|
|
|
|
|
onItemCheckboxChange?: ( |
|
|
|
|
|
|
|
key: string, |
|
|
|
|
|
|
|
...onChangeParams: Parameters<OnCheckboxChange> |
|
|
|
|
|
|
|
) => ReturnType<OnCheckboxChange>; |
|
|
|
|
|
|
|
renderListItemCheckboxState?: (key: string, value: T) => boolean; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ListOptionalProps<T extends unknown = unknown> = |
|
|
|
|
|
|
|
ListOptionalPropsWithDefaults<T> & ListOptionalPropsWithoutDefaults<T>; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ListProps<T extends unknown = unknown> = FlexBoxProps & |
|
|
|
|
|
|
|
ListOptionalProps<T> & { |
|
|
|
|
|
|
|
listItems: Record<string, T>; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ListForwardedRefContent = { |
|
|
|
|
|
|
|
setCheckAll?: (value: boolean) => void; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const HEADER_SPACING = '.3em'; |
|
|
|
|
|
|
|
const LIST_DEFAULT_PROPS: Required<ListOptionalPropsWithDefaults> & |
|
|
|
|
|
|
|
ListOptionalPropsWithoutDefaults = { |
|
|
|
|
|
|
|
header: undefined, |
|
|
|
|
|
|
|
allowAddItem: undefined, |
|
|
|
|
|
|
|
allowCheckAll: false, |
|
|
|
|
|
|
|
allowCheckItem: undefined, |
|
|
|
|
|
|
|
allowDelete: undefined, |
|
|
|
|
|
|
|
allowEdit: false, |
|
|
|
|
|
|
|
allowEditItem: undefined, |
|
|
|
|
|
|
|
edit: false, |
|
|
|
|
|
|
|
initialCheckAll: false, |
|
|
|
|
|
|
|
insertHeader: true, |
|
|
|
|
|
|
|
listEmpty: undefined, |
|
|
|
|
|
|
|
listItemKeyPrefix: uuidv4(), |
|
|
|
|
|
|
|
listItemProps: {}, |
|
|
|
|
|
|
|
listProps: {}, |
|
|
|
|
|
|
|
onAdd: undefined, |
|
|
|
|
|
|
|
onDelete: undefined, |
|
|
|
|
|
|
|
onEdit: undefined, |
|
|
|
|
|
|
|
onAllCheckboxChange: undefined, |
|
|
|
|
|
|
|
onItemCheckboxChange: undefined, |
|
|
|
|
|
|
|
renderListItem: (key) => <BodyText>{key}</BodyText>, |
|
|
|
|
|
|
|
renderListItemCheckboxState: undefined, |
|
|
|
|
|
|
|
scroll: false, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
const LIST_ICON_MIN_WIDTH = '56px'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const CHECK_ALL_MIN_WIDTH = `calc(${LIST_ICON_MIN_WIDTH} - ${HEADER_SPACING})`; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const List = forwardRef( |
|
|
|
const List = forwardRef( |
|
|
|
<T,>( |
|
|
|
<T,>( |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
allowCheckAll: isAllowCheckAll = false, |
|
|
|
|
|
|
|
allowEdit: isAllowEdit = false, |
|
|
|
|
|
|
|
edit: isEdit = false, |
|
|
|
|
|
|
|
flexBoxProps, |
|
|
|
header, |
|
|
|
header, |
|
|
|
allowCheckAll: isAllowCheckAll = LIST_DEFAULT_PROPS.allowCheckAll, |
|
|
|
headerSpacing = '.3em', |
|
|
|
allowEdit: isAllowEdit = LIST_DEFAULT_PROPS.allowEdit, |
|
|
|
initialCheckAll = false, |
|
|
|
edit: isEdit = LIST_DEFAULT_PROPS.edit, |
|
|
|
insertHeader: isInsertHeader = true, |
|
|
|
initialCheckAll = LIST_DEFAULT_PROPS.initialCheckAll, |
|
|
|
listEmpty, |
|
|
|
insertHeader: isInsertHeader = LIST_DEFAULT_PROPS.insertHeader, |
|
|
|
listItemIconMinWidth = '56px', |
|
|
|
listEmpty = LIST_DEFAULT_PROPS.listEmpty, |
|
|
|
listItemKeyPrefix = uuidv4(), |
|
|
|
listItemKeyPrefix = LIST_DEFAULT_PROPS.listItemKeyPrefix, |
|
|
|
listItemProps: { sx: listItemSx, ...restListItemProps } = {}, |
|
|
|
listItemProps: { |
|
|
|
|
|
|
|
sx: listItemSx, |
|
|
|
|
|
|
|
...restListItemProps |
|
|
|
|
|
|
|
} = LIST_DEFAULT_PROPS.listItemProps, |
|
|
|
|
|
|
|
listItems, |
|
|
|
listItems, |
|
|
|
listProps: { |
|
|
|
listProps: { sx: listSx, ...restListProps } = {}, |
|
|
|
sx: listSx, |
|
|
|
|
|
|
|
...restListProps |
|
|
|
|
|
|
|
} = LIST_DEFAULT_PROPS.listProps, |
|
|
|
|
|
|
|
onAdd, |
|
|
|
onAdd, |
|
|
|
onDelete, |
|
|
|
onDelete, |
|
|
|
onEdit, |
|
|
|
onEdit, |
|
|
|
onAllCheckboxChange, |
|
|
|
onAllCheckboxChange, |
|
|
|
onItemCheckboxChange, |
|
|
|
onItemCheckboxChange, |
|
|
|
renderListItem = LIST_DEFAULT_PROPS.renderListItem, |
|
|
|
renderListItem = (key) => <BodyText>{key}</BodyText>, |
|
|
|
renderListItemCheckboxState, |
|
|
|
renderListItemCheckboxState, |
|
|
|
scroll: isScroll = LIST_DEFAULT_PROPS.scroll, |
|
|
|
scroll: isScroll = false, |
|
|
|
// Input props that depend on other input props.
|
|
|
|
// Input props that depend on other input props.
|
|
|
|
allowAddItem: isAllowAddItem = isAllowEdit, |
|
|
|
allowAddItem: isAllowAddItem = isAllowEdit, |
|
|
|
allowCheckItem: isAllowCheckItem = isAllowEdit, |
|
|
|
allowCheckItem: isAllowCheckItem = isAllowEdit, |
|
|
|
allowDelete: isAllowDelete = isAllowEdit, |
|
|
|
allowDelete: isAllowDelete = isAllowEdit, |
|
|
|
allowEditItem: isAllowEditItem = isAllowEdit, |
|
|
|
allowEditItem: isAllowEditItem = isAllowEdit, |
|
|
|
|
|
|
|
|
|
|
|
...rootProps |
|
|
|
|
|
|
|
}: ListProps<T>, |
|
|
|
}: ListProps<T>, |
|
|
|
ref: ForwardedRef<ListForwardedRefContent>, |
|
|
|
ref: ForwardedRef<ListForwardedRefContent>, |
|
|
|
) => { |
|
|
|
) => { |
|
|
|
const [isCheckAll, setIsCheckAll] = useState<boolean>(initialCheckAll); |
|
|
|
const [isCheckAll, setIsCheckAll] = useState<boolean>(initialCheckAll); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const checkAllMinWidth = useMemo( |
|
|
|
|
|
|
|
() => `calc(${listItemIconMinWidth} - ${headerSpacing})`, |
|
|
|
|
|
|
|
[headerSpacing, listItemIconMinWidth], |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const addItemButton = useMemo( |
|
|
|
const addItemButton = useMemo( |
|
|
|
() => |
|
|
|
() => |
|
|
|
isAllowAddItem ? ( |
|
|
|
isAllowAddItem ? ( |
|
|
@ -192,7 +114,7 @@ const List = forwardRef( |
|
|
|
|
|
|
|
|
|
|
|
if (isEdit && isAllowCheckItem) { |
|
|
|
if (isEdit && isAllowCheckItem) { |
|
|
|
element = isAllowCheckAll ? ( |
|
|
|
element = isAllowCheckAll ? ( |
|
|
|
<MUIBox sx={{ minWidth: CHECK_ALL_MIN_WIDTH }}> |
|
|
|
<MUIBox sx={{ minWidth: checkAllMinWidth }}> |
|
|
|
<Checkbox |
|
|
|
<Checkbox |
|
|
|
checked={isCheckAll} |
|
|
|
checked={isCheckAll} |
|
|
|
edge="start" |
|
|
|
edge="start" |
|
|
@ -205,12 +127,13 @@ const List = forwardRef( |
|
|
|
/> |
|
|
|
/> |
|
|
|
</MUIBox> |
|
|
|
</MUIBox> |
|
|
|
) : ( |
|
|
|
) : ( |
|
|
|
<Divider sx={{ minWidth: CHECK_ALL_MIN_WIDTH }} /> |
|
|
|
<Divider sx={{ minWidth: checkAllMinWidth }} /> |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return element; |
|
|
|
return element; |
|
|
|
}, [ |
|
|
|
}, [ |
|
|
|
|
|
|
|
checkAllMinWidth, |
|
|
|
isAllowCheckAll, |
|
|
|
isAllowCheckAll, |
|
|
|
isAllowCheckItem, |
|
|
|
isAllowCheckItem, |
|
|
|
isCheckAll, |
|
|
|
isCheckAll, |
|
|
@ -220,7 +143,7 @@ const List = forwardRef( |
|
|
|
const headerElement = useMemo( |
|
|
|
const headerElement = useMemo( |
|
|
|
() => |
|
|
|
() => |
|
|
|
isInsertHeader && header ? ( |
|
|
|
isInsertHeader && header ? ( |
|
|
|
<FlexBox row spacing={HEADER_SPACING} sx={{ height: '2.4em' }}> |
|
|
|
<FlexBox row spacing={headerSpacing} sx={{ height: '2.4em' }}> |
|
|
|
{checkAllElement} |
|
|
|
{checkAllElement} |
|
|
|
{typeof header === 'string' ? ( |
|
|
|
{typeof header === 'string' ? ( |
|
|
|
<> |
|
|
|
<> |
|
|
@ -243,6 +166,7 @@ const List = forwardRef( |
|
|
|
deleteItemButton, |
|
|
|
deleteItemButton, |
|
|
|
editItemButton, |
|
|
|
editItemButton, |
|
|
|
header, |
|
|
|
header, |
|
|
|
|
|
|
|
headerSpacing, |
|
|
|
isInsertHeader, |
|
|
|
isInsertHeader, |
|
|
|
], |
|
|
|
], |
|
|
|
); |
|
|
|
); |
|
|
@ -259,7 +183,7 @@ const List = forwardRef( |
|
|
|
const listItemCheckbox = useCallback( |
|
|
|
const listItemCheckbox = useCallback( |
|
|
|
(key: string, checked?: boolean) => |
|
|
|
(key: string, checked?: boolean) => |
|
|
|
isEdit && isAllowCheckItem ? ( |
|
|
|
isEdit && isAllowCheckItem ? ( |
|
|
|
<MUIListItemIcon sx={{ minWidth: LIST_ICON_MIN_WIDTH }}> |
|
|
|
<MUIListItemIcon sx={{ minWidth: listItemIconMinWidth }}> |
|
|
|
<Checkbox |
|
|
|
<Checkbox |
|
|
|
checked={checked} |
|
|
|
checked={checked} |
|
|
|
edge="start" |
|
|
|
edge="start" |
|
|
@ -269,14 +193,17 @@ const List = forwardRef( |
|
|
|
/> |
|
|
|
/> |
|
|
|
</MUIListItemIcon> |
|
|
|
</MUIListItemIcon> |
|
|
|
) : undefined, |
|
|
|
) : undefined, |
|
|
|
[isAllowCheckItem, isEdit, onItemCheckboxChange], |
|
|
|
[isAllowCheckItem, isEdit, listItemIconMinWidth, onItemCheckboxChange], |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const listItemElements = useMemo(() => { |
|
|
|
const listItemElements = useMemo(() => { |
|
|
|
const entries = Object.entries(listItems); |
|
|
|
let result = listEmptyElement; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (listItems) { |
|
|
|
|
|
|
|
const entries = Object.entries(listItems); |
|
|
|
|
|
|
|
|
|
|
|
return entries.length > 0 |
|
|
|
if (entries.length > 0) { |
|
|
|
? entries.map(([key, value]) => ( |
|
|
|
result = entries.map(([key, value]) => ( |
|
|
|
<MUIListItem |
|
|
|
<MUIListItem |
|
|
|
{...restListItemProps} |
|
|
|
{...restListItemProps} |
|
|
|
key={`${listItemKeyPrefix}-${key}`} |
|
|
|
key={`${listItemKeyPrefix}-${key}`} |
|
|
@ -288,8 +215,11 @@ const List = forwardRef( |
|
|
|
)} |
|
|
|
)} |
|
|
|
{renderListItem(key, value)} |
|
|
|
{renderListItem(key, value)} |
|
|
|
</MUIListItem> |
|
|
|
</MUIListItem> |
|
|
|
)) |
|
|
|
)); |
|
|
|
: listEmptyElement; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
}, [ |
|
|
|
}, [ |
|
|
|
listEmptyElement, |
|
|
|
listEmptyElement, |
|
|
|
listItemCheckbox, |
|
|
|
listItemCheckbox, |
|
|
@ -314,7 +244,7 @@ const List = forwardRef( |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
return ( |
|
|
|
<FlexBox spacing={0} {...rootProps}> |
|
|
|
<FlexBox spacing={0} {...flexBoxProps}> |
|
|
|
{headerElement} |
|
|
|
{headerElement} |
|
|
|
<MUIList |
|
|
|
<MUIList |
|
|
|
{...restListProps} |
|
|
|
{...restListProps} |
|
|
@ -327,11 +257,8 @@ const List = forwardRef( |
|
|
|
}, |
|
|
|
}, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
List.defaultProps = LIST_DEFAULT_PROPS; |
|
|
|
|
|
|
|
List.displayName = 'List'; |
|
|
|
List.displayName = 'List'; |
|
|
|
|
|
|
|
|
|
|
|
export type { ListForwardedRefContent, ListProps }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default List as <T>( |
|
|
|
export default List as <T>( |
|
|
|
props: ListProps<T> & { ref?: ForwardedRef<ListForwardedRefContent> }, |
|
|
|
props: ListProps<T> & { ref?: ForwardedRef<ListForwardedRefContent> }, |
|
|
|
) => ReturnType<FC<ListProps<T>>>; |
|
|
|
) => ReturnType<FC<ListProps<T>>>; |
|
|
|