import * as React from 'react'; import * as PropTypes from 'prop-types'; import classNames from 'classnames'; import { ListGridType, ListContext } from './index'; import { Col } from '../grid'; import { ConfigContext } from '../config-provider'; import { cloneElement } from '../_util/reactNode'; export interface ListItemProps extends React.HTMLAttributes { className?: string; children?: React.ReactNode; prefixCls?: string; style?: React.CSSProperties; extra?: React.ReactNode; actions?: React.ReactNode[]; grid?: ListGridType; colStyle?: React.CSSProperties; } export interface ListItemMetaProps { avatar?: React.ReactNode; className?: string; children?: React.ReactNode; description?: React.ReactNode; prefixCls?: string; style?: React.CSSProperties; title?: React.ReactNode; } export const Meta: React.FC = ({ prefixCls: customizePrefixCls, className, avatar, title, description, ...others }) => { const { getPrefixCls } = React.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 React.FC { Meta: typeof Meta; } const Item: ListItemTypeProps = props => { const { grid, itemLayout } = React.useContext(ListContext); const { getPrefixCls } = React.useContext(ConfigContext); const isItemContainsTextNodeAndNotSingular = () => { const { children } = props; let result; React.Children.forEach(children, (element: React.ReactElement) => { if (typeof element === 'string') { result = true; } }); return result && React.Children.count(children) > 1; }; const isFlexMode = () => { const { extra } = props; if (itemLayout === 'vertical') { return !!extra; } return !isItemContainsTextNodeAndNotSingular(); }; const { prefixCls: customizePrefixCls, children, actions, extra, className, colStyle, ...others } = props; const prefixCls = getPrefixCls('list', customizePrefixCls); const actionsContent = actions && actions.length > 0 && (
    {actions.map((action: React.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 ); }; Item.Meta = Meta; Item.contextTypes = { grid: PropTypes.any, itemLayout: PropTypes.string, }; export default Item;