import { Col, Row, SearchInput, SidePanel } from '../index'; import { ReactNode, useMemo, useState } from 'react'; import ListItem, { ListItemProps } from './ListItem'; import Button from './Button'; import { LeftOutlined, PlusCircleOutlined, RightOutlined, } from '@ant-design/icons'; const DEFAULT_ITEM_PER_PAGE = 10; const paginate = (array: any[], page_size: number, page_number: number) => { // human-readable page numbers usually start with 1, so we reduce 1 in the first argument return array.slice((page_number - 1) * page_size, page_number * page_size); }; interface ListItemWithId extends ListItemProps { id: string; title: string; } export interface LayoutSelectionProps { children: ReactNode; items: ListItemWithId[]; selected: string; onSelect: (s: string) => void; Header?: ReactNode; onAdd?: () => void; addLabel?: string; searchLabel?: string; itemPerPage?: number; leftPanelWidth?: number; } interface ListItemWithSelection extends ListItemWithId { selected: boolean; onSelect: (s: string) => void; id: string; } const ListItemWithSelection = ({ selected, onSelect, className, ...listItemProps }: ListItemWithSelection) => ( { listItemProps.onClick && listItemProps.onClick(e); onSelect(listItemProps.id); }} className={`!flex-initial ${className || ''} ${ selected ? '!text-blue-500 !border-blue-500' : '' }`} /> ); const LayoutSelection = ({ Header, children, items, selected, onSelect, onAdd, addLabel = 'add', searchLabel = '', leftPanelWidth = 8, itemPerPage = DEFAULT_ITEM_PER_PAGE, }: LayoutSelectionProps) => { const [searchValue, SetSearchValue] = useState(''); const [currentPage, setCurrentPage] = useState(1); const rightPanelWidth = 24 - leftPanelWidth; const filteredItems = useMemo( () => items.filter( (item) => item.title.toLowerCase().includes(searchValue.toLowerCase()) || (typeof item.content === 'string' && item.content.toLowerCase().includes(searchValue.toLowerCase())) ), [searchValue, items] ); const paginatedItems = useMemo( () => paginate(filteredItems, itemPerPage, currentPage), [searchValue, itemPerPage, currentPage, filteredItems] ); const totalPages = Math.ceil(filteredItems.length / itemPerPage); return ( {Header || null}
{onAdd && ( )} SetSearchValue(e.target.value)} className="flex-1" placeholder={searchLabel} />
{paginatedItems.map((item) => ( ))}
{totalPages > 1 && (
{currentPage} / {totalPages}
)} } /> {children}
); }; export default LayoutSelection;