import classNames from 'classnames'; import type { CSSProperties, FC, ForwardRefExoticComponent, ForwardRefRenderFunction, HTMLAttributes, ReactElement, ReactNode, } from 'react'; import React, { Children, forwardRef, useContext } from 'react'; import { ConfigContext } from '../config-provider'; import { Col } from '../grid'; import { cloneElement } from '../_util/reactNode'; import { ListContext } from './index'; export interface ListItemProps extends HTMLAttributes { className?: string; children?: ReactNode; prefixCls?: string; style?: CSSProperties; extra?: ReactNode; actions?: ReactNode[]; colStyle?: CSSProperties; } export interface ListItemMetaProps { avatar?: ReactNode; className?: string; children?: ReactNode; description?: ReactNode; prefixCls?: string; style?: CSSProperties; title?: ReactNode; } export const Meta: FC = ({ prefixCls: customizePrefixCls, className, avatar, title, description, ...others }) => { const { getPrefixCls } = useContext(ConfigContext); const prefixCls = getPrefixCls('list', customizePrefixCls); const classString = classNames(`${prefixCls}-item-meta`, className); const content = (
{title &&

{title}

} {description &&
{description}
}
); return (
{avatar &&
{avatar}
} {(title || description) && content}
); }; export interface ListItemTypeProps extends ForwardRefExoticComponent> { Meta: typeof Meta; } const InternalItem: ForwardRefRenderFunction = ( { prefixCls: customizePrefixCls, children, actions, extra, className, colStyle, ...others }, ref, ) => { const { grid, itemLayout } = useContext(ListContext); const { getPrefixCls } = useContext(ConfigContext); const isItemContainsTextNodeAndNotSingular = () => { let result; Children.forEach(children, (element: ReactElement) => { if (typeof element === 'string') { result = true; } }); return result && Children.count(children) > 1; }; const isFlexMode = () => { if (itemLayout === 'vertical') { return !!extra; } return !isItemContainsTextNodeAndNotSingular(); }; const prefixCls = getPrefixCls('list', customizePrefixCls); const actionsContent = actions && actions.length > 0 && (
    {actions.map((action: ReactNode, i: number) => ( // eslint-disable-next-line react/no-array-index-key
  • {action} {i !== actions.length - 1 && }
  • ))}
); const Element = grid ? 'div' : 'li'; const itemChildren = ( {itemLayout === 'vertical' && extra ? [
{children} {actionsContent}
,
{extra}
, ] : [children, actionsContent, cloneElement(extra, { key: 'extra' })]}
); return grid ? ( {itemChildren} ) : ( itemChildren ); }; const Item = forwardRef(InternalItem) as ListItemTypeProps; Item.Meta = Meta; export default Item;