import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; import { useState, type ReactNode, type ReactElement, Fragment } from 'react'; import { List as MuiList,ListItem as MuiListItem,ListItemButton as MuiListItemButton,ListItemText as MuiListItemText,ListItemIcon as MuiListItemIcon,ListItemAvatar as MuiListItemAvatar,ListSubheader as MuiListSubheader,type ListProps, Divider } from '@mui/material'; import clsx from 'clsx'; import './rds-list.scss'; import {Paper } from '@mui/material'; import RdsCheckbox from '../rds-checkbox/rds-checkbox'; export interface RdsListItem { id: string | number; primary: string; secondary?: string; icon?: ReactNode; avatar?: ReactNode; secondaryAction?: ReactNode; onClick?: () => void; selected?: boolean; disabled?: boolean; children?: RdsListItem[]; } export interface RdsListSection { title: ReactNode; items: RdsListItem[]; } export interface RdsListProps extends Omit { items?: RdsListItem[]; /** Grouped sections with sticky subheaders — use instead of multiple lists in one scroll container. */ sections?: RdsListSection[]; variant?: 'simple' | 'button' | 'icon' | 'avatar' | 'firebase'; alignItems?: 'flex-start' | 'center'; disableGutters?: boolean; withDividers?: boolean; withCheckboxes?: boolean; onCheckboxChange?: (id: string | number, checked: boolean) => void; checkedItems?: (string | number)[]; } const ExpandIcon = ({ open }: { open: boolean }) => ( ); const RdsList = ({ items = [], sections, variant = 'simple', alignItems, disableGutters, withDividers, withCheckboxes, onCheckboxChange, checkedItems, className, dense, ...props }: RdsListProps) => { const [openMap, setOpenMap] = useState>({}); const [internalChecked, setInternalChecked] = useState<(string | number)[]>([]); const isControlledChecked = checkedItems !== undefined; const effectiveCheckedItems = isControlledChecked ? (Array.isArray(checkedItems) ? checkedItems : []) : internalChecked; const showCheckboxes = withCheckboxes === true; const variantClass = variant === 'firebase' ? 'rds-list--firebase' : ''; const denseClass = dense ? 'rds-list--dense' : ''; const rootClass = clsx('rds-list', variantClass, denseClass, className); const getItemClass = (item: RdsListItem) => clsx( 'rds-list__item', (item.onClick || variant === 'button') && 'rds-list__item--clickable', item.selected && 'rds-list__item--selected', item.disabled && 'rds-list__item--disabled', disableGutters && 'rds-list__item--no-gutters', ); const handleCheckboxChange = (id: string | number) => () => { const isChecked = !effectiveCheckedItems.includes(id); if (onCheckboxChange) { onCheckboxChange(id, isChecked); } else { const newChecked = isChecked ? [...effectiveCheckedItems, id] : effectiveCheckedItems.filter(item => item !== id); setInternalChecked(newChecked); } }; const handleToggle = (id: string | number) => { setOpenMap((prev) => ({ ...prev, [id]: !prev[id] })); }; const renderListItem = (item: RdsListItem): ReactElement => { const hasChildren = Array.isArray(item.children) && item.children.length > 0; const isOpen = openMap[item.id] || false; const itemProps = { selected: item.selected, disabled: item.disabled, alignItems: alignItems, disableGutters: disableGutters, className: getItemClass(item), }; if (hasChildren) { return ( handleToggle(item.id)}> {item.icon && ( {item.icon} )} {item.avatar && ( {item.avatar} )} {isOpen && ( {(item.children ?? []).map((child) => renderListItem(child) )} )} ); } if (variant === 'button' || item.onClick || showCheckboxes) { const checkbox = showCheckboxes && !item.icon ? ( handleCheckboxChange(item.id)()} style="square" /> ) : null; const icon = item.icon ? ( {item.icon} ) : checkbox; return ( {icon} {item.avatar && ( {item.avatar} )} {item.secondaryAction && ( {item.secondaryAction} )} ); } return ( {item.icon && !item.secondaryAction && ( {item.icon} )} {item.avatar && ( {item.avatar} )} {item.secondaryAction && ( {item.secondaryAction} )} ); }; const renderItems = (sectionItems: RdsListItem[]): ReactNode[] => { if (withDividers) { const sectionChildren: ReactNode[] = []; sectionItems.forEach((item, idx) => { const hasChildren = Array.isArray(item.children) && item.children.length > 0; sectionChildren.push(renderListItem(item)); if (idx < sectionItems.length - 1 && !hasChildren) { sectionChildren.push( ); } }); return sectionChildren; } return sectionItems.map((item) => renderListItem(item)); }; if (sections?.length) { const { subheader: _subheader, ...listProps } = props; return ( } {...listProps} > {sections.map((section, sectionIdx) => (
    • {section.title} {renderItems(section.items)}
  • ))}
    ); } const children = renderItems(items); return ( {children} ); }; RdsList.displayName = 'RdsList'; export default RdsList;