2022-06-22 14:57:09 +08:00
|
|
|
import classNames from 'classnames';
|
2022-05-07 13:34:40 +08:00
|
|
|
import type {
|
|
|
|
CSSProperties,
|
|
|
|
FC,
|
|
|
|
ForwardRefExoticComponent,
|
|
|
|
ForwardRefRenderFunction,
|
|
|
|
HTMLAttributes,
|
|
|
|
ReactElement,
|
|
|
|
ReactNode,
|
|
|
|
} from 'react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import React, { Children, forwardRef, useContext } from 'react';
|
2023-06-05 09:56:19 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2020-04-26 21:23:25 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { Col } from '../grid';
|
2023-06-05 09:56:19 +08:00
|
|
|
import { ListContext } from './context';
|
2017-08-09 15:45:08 +08:00
|
|
|
|
2022-05-07 13:34:40 +08:00
|
|
|
export interface ListItemProps extends HTMLAttributes<HTMLDivElement> {
|
2017-08-09 15:45:08 +08:00
|
|
|
className?: string;
|
2022-05-07 13:34:40 +08:00
|
|
|
children?: ReactNode;
|
2017-08-09 15:45:08 +08:00
|
|
|
prefixCls?: string;
|
2022-05-07 13:34:40 +08:00
|
|
|
style?: CSSProperties;
|
|
|
|
extra?: ReactNode;
|
|
|
|
actions?: ReactNode[];
|
|
|
|
colStyle?: CSSProperties;
|
2017-08-09 15:45:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface ListItemMetaProps {
|
2022-05-07 13:34:40 +08:00
|
|
|
avatar?: ReactNode;
|
2017-08-09 15:45:08 +08:00
|
|
|
className?: string;
|
2022-05-07 13:34:40 +08:00
|
|
|
children?: ReactNode;
|
|
|
|
description?: ReactNode;
|
2017-08-09 15:45:08 +08:00
|
|
|
prefixCls?: string;
|
2022-05-07 13:34:40 +08:00
|
|
|
style?: CSSProperties;
|
|
|
|
title?: ReactNode;
|
2017-08-09 15:45:08 +08:00
|
|
|
}
|
|
|
|
|
2022-05-07 13:34:40 +08:00
|
|
|
export const Meta: FC<ListItemMetaProps> = ({
|
2020-04-26 21:23:25 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
avatar,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
...others
|
|
|
|
}) => {
|
2022-05-07 13:34:40 +08:00
|
|
|
const { getPrefixCls } = useContext(ConfigContext);
|
2020-04-26 21:23:25 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-05-07 13:34:40 +08:00
|
|
|
export interface ListItemTypeProps
|
2022-05-10 10:01:11 +08:00
|
|
|
extends ForwardRefExoticComponent<ListItemProps & React.RefAttributes<HTMLElement>> {
|
2020-04-26 21:23:25 +08:00
|
|
|
Meta: typeof Meta;
|
|
|
|
}
|
2017-08-14 11:08:19 +08:00
|
|
|
|
2022-05-07 13:34:40 +08:00
|
|
|
const InternalItem: ForwardRefRenderFunction<HTMLDivElement, ListItemProps> = (
|
|
|
|
{ prefixCls: customizePrefixCls, children, actions, extra, className, colStyle, ...others },
|
|
|
|
ref,
|
|
|
|
) => {
|
|
|
|
const { grid, itemLayout } = useContext(ListContext);
|
|
|
|
const { getPrefixCls } = 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;
|
2022-05-07 13:34:40 +08:00
|
|
|
Children.forEach(children, (element: ReactElement<any>) => {
|
2019-03-06 16:31:07 +08:00
|
|
|
if (typeof element === 'string') {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
});
|
2022-05-07 13:34:40 +08:00
|
|
|
return result && 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">
|
2022-05-07 13:34:40 +08:00
|
|
|
{actions.map((action: ReactNode, i: number) => (
|
2020-04-26 21:23:25 +08:00
|
|
|
// 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`
|
2022-05-07 13:34:40 +08:00
|
|
|
{...(!grid ? { ref } : {})}
|
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 ? (
|
2022-05-07 13:34:40 +08:00
|
|
|
<Col ref={ref} flex={1} style={colStyle}>
|
2020-04-26 21:23:25 +08:00
|
|
|
{itemChildren}
|
|
|
|
</Col>
|
|
|
|
) : (
|
|
|
|
itemChildren
|
|
|
|
);
|
|
|
|
};
|
2022-05-07 13:34:40 +08:00
|
|
|
const Item = forwardRef(InternalItem) as ListItemTypeProps;
|
2020-04-26 21:23:25 +08:00
|
|
|
|
|
|
|
Item.Meta = Meta;
|
|
|
|
|
|
|
|
export default Item;
|