import React, { CSSProperties, ReactElement, useCallback, useMemo } from "react"; import cx from "classnames"; import ListItem, { ListItemProps } from "../../../components/ListItem/ListItem"; import ListTitle, { ListTitleProps } from "../../ListTitle/ListTitle"; import VirtualizedList from "../../../components/VirtualizedList/VirtualizedList"; import VibeComponentProps from "../../../types/VibeComponentProps"; import { ListItemIconProps } from "../../ListItemIcon/ListItemIcon"; import styles from "./VirtualizedListItems.module.scss"; const ITEM_CHILDREN_TYPES = { TITLE: "title", ITEM: "item" }; const LIST_TITLE_HEIGHT = 48; const LIST_ITEM_HEIGHT = 32; export interface VirtualizedListItemsProps extends VibeComponentProps { children?: React.ReactElement | React.ReactElement[]; } export const VirtualizedListItems: React.FC = ({ children }) => { const items = useMemo(() => { const childrenArr = Array.isArray(children) ? children : [children]; return childrenArr .map((child, index) => { // @ts-ignore displayName is coming from Component assigned field: ListTitle, ListItem const childTypeDisplayName = child.type.displayName; if (childTypeDisplayName === ListTitle.displayName) { return { type: ITEM_CHILDREN_TYPES.TITLE, id: `list-title-${index}`, props: child.props, // avoid add spacing to the first category on the list height: LIST_TITLE_HEIGHT }; } else if (childTypeDisplayName === ListItem.displayName) { const { id } = child.props; return { type: ITEM_CHILDREN_TYPES.ITEM, id: id || `list-item-${index}`, props: child.props, height: LIST_ITEM_HEIGHT }; } else { return undefined; } }) .filter(item => item !== undefined); }, [children]); const itemRenderer = useCallback( (item: ReactElement, index: number, style: CSSProperties) => { const { type, props } = item; let element; switch (type) { case ITEM_CHILDREN_TYPES.TITLE: { element = ; break; } case ITEM_CHILDREN_TYPES.ITEM: { element = ; break; } } return
{element}
; }, [] ); return ( // @ts-ignore TODO TS-migration fix when VirtualizedList is converted to TS ); };