import React, { useEffect } from "react"; import Button from "../Button/Button"; export interface ListProps { items: T[]; /** Renders the item. */ itemRenderer: (item: T) => React.ReactNode; /** The ID/key of an item. */ itemId: (item: T) => string; /** Limit the numbers of shown (and rendered) components. */ limitOptions?: { initialMax: number; stepSize: number; }; moreLabel?: string; } /** A generic list component with support of various performance/scalability improvements. */ export function List({ items, itemRenderer, itemId, limitOptions, moreLabel = "Show more..." }: ListProps) { // The number of items to show in the list, undefined means unlimited const [nrOfItemsToShow, setNrOfItemsToShow] = React.useState( limitOptions?.initialMax ?? undefined, ); useEffect(() => { setNrOfItemsToShow(limitOptions?.initialMax ?? undefined); }, [items.length, limitOptions?.initialMax]); const onShowMore = () => { if (limitOptions) { setNrOfItemsToShow((current) => (current ?? 0) + limitOptions.stepSize); } }; return (
    {(nrOfItemsToShow ? items.slice(0, nrOfItemsToShow) : items).map((item) => { return
  1. {itemRenderer(item)}
  2. ; })} {nrOfItemsToShow != null && nrOfItemsToShow < items.length && ( )}
); } export default List;