2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2017-08-09 15:45:08 +08:00
|
|
|
import classNames from 'classnames';
|
2020-04-27 16:59:33 +08:00
|
|
|
import { ListGridType, ListContext } from './index';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { Col } from '../grid';
|
2020-04-26 21:23:25 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2019-04-24 12:21:02 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2017-08-09 15:45:08 +08:00
|
|
|
|
2019-01-10 12:13:27 +08:00
|
|
|
export interface ListItemProps extends React.HTMLAttributes<HTMLDivElement> {
|
2017-08-09 15:45:08 +08:00
|
|
|
className?: string;
|
|
|
|
children?: React.ReactNode;
|
|
|
|
prefixCls?: string;
|
|
|
|
style?: React.CSSProperties;
|
2017-12-05 16:12:55 +08:00
|
|
|
extra?: React.ReactNode;
|
2018-11-01 14:49:51 +08:00
|
|
|
actions?: React.ReactNode[];
|
2017-08-21 21:14:20 +08:00
|
|
|
grid?: ListGridType;
|
2020-04-27 16:59:33 +08:00
|
|
|
colStyle?: React.CSSProperties;
|
2017-08-09 15:45:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface ListItemMetaProps {
|
|
|
|
avatar?: React.ReactNode;
|
|
|
|
className?: string;
|
|
|
|
children?: React.ReactNode;
|
2017-12-05 16:28:12 +08:00
|
|
|
description?: React.ReactNode;
|
2017-08-09 15:45:08 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
style?: React.CSSProperties;
|
2017-12-05 16:28:12 +08:00
|
|
|
title?: React.ReactNode;
|
2017-08-09 15:45:08 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 21:23:25 +08:00
|
|
|
export const Meta: React.FC<ListItemMetaProps> = ({
|
|
|
|
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 = (
|
|
|
|
<div className={`${prefixCls}-item-meta-content`}>
|
|
|
|
{title && <h4 className={`${prefixCls}-item-meta-title`}>{title}</h4>}
|
|
|
|
{description && <div className={`${prefixCls}-item-meta-description`}>{description}</div>}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div {...others} className={classString}>
|
|
|
|
{avatar && <div className={`${prefixCls}-item-meta-avatar`}>{avatar}</div>}
|
|
|
|
{(title || description) && content}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2017-08-09 15:45:08 +08:00
|
|
|
|
2020-04-26 21:23:25 +08:00
|
|
|
export interface ListItemTypeProps extends React.FC<ListItemProps> {
|
|
|
|
Meta: typeof Meta;
|
|
|
|
}
|
2017-08-14 11:08:19 +08:00
|
|
|
|
2020-05-29 15:27:08 +08:00
|
|
|
const Item: ListItemTypeProps = ({
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
children,
|
|
|
|
actions,
|
|
|
|
extra,
|
|
|
|
className,
|
|
|
|
colStyle,
|
|
|
|
...others
|
|
|
|
}) => {
|
2020-04-26 21:23:25 +08:00
|
|
|
const { grid, itemLayout } = React.useContext(ListContext);
|
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
2018-11-09 17:38:19 +08:00
|
|
|
|
2020-04-26 21:23:25 +08:00
|
|
|
const isItemContainsTextNodeAndNotSingular = () => {
|
2019-03-06 16:31:07 +08:00
|
|
|
let result;
|
|
|
|
React.Children.forEach(children, (element: React.ReactElement<any>) => {
|
|
|
|
if (typeof element === 'string') {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
});
|
2020-01-15 10:35:45 +08:00
|
|
|
return result && React.Children.count(children) > 1;
|
2020-04-26 21:23:25 +08:00
|
|
|
};
|
2019-03-06 16:31:07 +08:00
|
|
|
|
2020-04-26 21:23:25 +08:00
|
|
|
const isFlexMode = () => {
|
2019-03-11 23:34:05 +08:00
|
|
|
if (itemLayout === 'vertical') {
|
|
|
|
return !!extra;
|
|
|
|
}
|
2020-04-26 21:23:25 +08:00
|
|
|
return !isItemContainsTextNodeAndNotSingular();
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-04-26 21:23:25 +08:00
|
|
|
const prefixCls = getPrefixCls('list', customizePrefixCls);
|
|
|
|
const actionsContent = actions && actions.length > 0 && (
|
|
|
|
<ul className={`${prefixCls}-item-action`} key="actions">
|
|
|
|
{actions.map((action: React.ReactNode, i: number) => (
|
|
|
|
// eslint-disable-next-line react/no-array-index-key
|
|
|
|
<li key={`${prefixCls}-item-action-${i}`}>
|
|
|
|
{action}
|
|
|
|
{i !== actions.length - 1 && <em className={`${prefixCls}-item-action-split`} />}
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
);
|
2020-04-27 16:59:33 +08:00
|
|
|
const Element = grid ? 'div' : 'li';
|
2020-04-26 21:23:25 +08:00
|
|
|
const itemChildren = (
|
2020-04-27 16:59:33 +08:00
|
|
|
<Element
|
2020-04-26 21:23:25 +08:00
|
|
|
{...(others as any)} // `li` element `onCopy` prop args is not same as `div`
|
2020-09-06 13:07:39 +08:00
|
|
|
className={classNames(
|
|
|
|
`${prefixCls}-item`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-item-no-flex`]: !isFlexMode(),
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
2020-04-26 21:23:25 +08:00
|
|
|
>
|
|
|
|
{itemLayout === 'vertical' && extra
|
|
|
|
? [
|
|
|
|
<div className={`${prefixCls}-item-main`} key="content">
|
|
|
|
{children}
|
|
|
|
{actionsContent}
|
|
|
|
</div>,
|
|
|
|
<div className={`${prefixCls}-item-extra`} key="extra">
|
|
|
|
{extra}
|
|
|
|
</div>,
|
|
|
|
]
|
|
|
|
: [children, actionsContent, cloneElement(extra, { key: 'extra' })]}
|
2020-04-27 16:59:33 +08:00
|
|
|
</Element>
|
2020-04-26 21:23:25 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
return grid ? (
|
2020-04-27 16:59:33 +08:00
|
|
|
<Col flex={1} style={colStyle}>
|
2020-04-26 21:23:25 +08:00
|
|
|
{itemChildren}
|
|
|
|
</Col>
|
|
|
|
) : (
|
|
|
|
itemChildren
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Item.Meta = Meta;
|
|
|
|
|
|
|
|
export default Item;
|