import * as React from 'react'; import classNames from 'classnames'; import useMergedState from 'rc-util/lib/hooks/useMergedState'; import type { KeyWiseTransferItem } from '.'; import Pagination from '../pagination'; import type { PaginationType, TransferKey } from './interface'; import type { RenderedItem, TransferListProps } from './list'; import ListItem from './ListItem'; export const OmitProps = ['handleFilter', 'handleClear', 'checkedKeys'] as const; export type OmitProp = (typeof OmitProps)[number]; type PartialTransferListProps = Omit, OmitProp>; type ExistPagination = Exclude; export interface TransferListBodyProps extends PartialTransferListProps { filteredItems: RecordType[]; filteredRenderItems: RenderedItem[]; selectedKeys: TransferKey[]; } const parsePagination = (pagination?: ExistPagination) => { const defaultPagination: PaginationType = { simple: true, showSizeChanger: false, showLessItems: false, }; return { ...defaultPagination, ...pagination }; }; export interface ListBodyRef { items?: RenderedItem[]; } const TransferListBody: React.ForwardRefRenderFunction< ListBodyRef, TransferListBodyProps > = ( props: TransferListBodyProps, ref: React.ForwardedRef>, ) => { const { prefixCls, filteredRenderItems, selectedKeys, disabled: globalDisabled, showRemove, pagination, onScroll, onItemSelect, onItemRemove, } = props; const [current, setCurrent] = React.useState(1); const mergedPagination = React.useMemo(() => { if (!pagination) { return null; } const convertPagination = typeof pagination === 'object' ? pagination : {}; return parsePagination(convertPagination); }, [pagination]); const [pageSize, setPageSize] = useMergedState(10, { value: mergedPagination?.pageSize, }); React.useEffect(() => { if (mergedPagination) { const maxPageCount = Math.ceil(filteredRenderItems.length / pageSize!); setCurrent(Math.min(current, maxPageCount)); } }, [filteredRenderItems, mergedPagination, pageSize]); const onInternalClick = (item: RecordType, e: React.MouseEvent) => { onItemSelect(item.key, !selectedKeys.includes(item.key), e); }; const onRemove = (item: RecordType) => { onItemRemove?.([item.key]); }; const onPageChange = (cur: number) => { setCurrent(cur); }; const onSizeChange = (cur: number, size: number) => { setCurrent(cur); setPageSize(size); }; const memoizedItems = React.useMemo[]>(() => { const displayItems = mergedPagination ? filteredRenderItems.slice((current - 1) * pageSize!, current * pageSize!) : filteredRenderItems; return displayItems; }, [current, filteredRenderItems, mergedPagination, pageSize]); React.useImperativeHandle(ref, () => ({ items: memoizedItems })); const paginationNode: React.ReactNode = mergedPagination ? ( ) : null; const cls = classNames(`${prefixCls}-content`, { [`${prefixCls}-content-show-remove`]: showRemove, }); return ( <>
    {(memoizedItems || []).map(({ renderedEl, renderedText, item }) => ( ))}
{paginationNode} ); }; if (process.env.NODE_ENV !== 'production') { TransferListBody.displayName = 'TransferListBody'; } export default React.forwardRef< ListBodyRef, TransferListBodyProps >(TransferListBody);