2024-04-09 16:49:47 +08:00
|
|
|
import type { CSSProperties, FC, HTMLAttributes, ReactElement, ReactNode } from 'react';
|
|
|
|
import React, { Children, useContext } from 'react';
|
2024-04-01 15:49:45 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
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;
|
2024-06-03 11:24:39 +08:00
|
|
|
classNames?: {
|
|
|
|
actions?: string;
|
|
|
|
extra?: 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;
|
2024-06-03 11:24:39 +08:00
|
|
|
styles?: {
|
|
|
|
actions?: CSSProperties;
|
|
|
|
extra?: CSSProperties;
|
|
|
|
};
|
2022-05-07 13:34:40 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-03 11:24:39 +08:00
|
|
|
type ListItemClassNamesModule = keyof Exclude<ListItemProps['classNames'], undefined>;
|
|
|
|
type ListItemStylesModule = keyof Exclude<ListItemProps['styles'], undefined>;
|
|
|
|
|
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
|
|
|
|
2024-04-09 16:49:47 +08:00
|
|
|
const InternalItem = React.forwardRef<HTMLDivElement, ListItemProps>((props, ref) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
children,
|
|
|
|
actions,
|
|
|
|
extra,
|
2024-06-03 11:24:39 +08:00
|
|
|
styles,
|
2024-04-09 16:49:47 +08:00
|
|
|
className,
|
2024-06-03 11:24:39 +08:00
|
|
|
classNames: customizeClassNames,
|
2024-04-09 16:49:47 +08:00
|
|
|
colStyle,
|
|
|
|
...others
|
|
|
|
} = props;
|
2022-05-07 13:34:40 +08:00
|
|
|
const { grid, itemLayout } = useContext(ListContext);
|
2024-06-03 11:24:39 +08:00
|
|
|
const { getPrefixCls, list } = useContext(ConfigContext);
|
|
|
|
|
|
|
|
const moduleClass = (moduleName: ListItemClassNamesModule) =>
|
|
|
|
classNames(list?.item?.classNames?.[moduleName], customizeClassNames?.[moduleName]);
|
|
|
|
|
|
|
|
const moduleStyle = (moduleName: ListItemStylesModule): React.CSSProperties => ({
|
|
|
|
...list?.item?.styles?.[moduleName],
|
|
|
|
...styles?.[moduleName],
|
|
|
|
});
|
2018-11-09 17:38:19 +08:00
|
|
|
|
2020-04-26 21:23:25 +08:00
|
|
|
const isItemContainsTextNodeAndNotSingular = () => {
|
2024-06-22 21:59:12 +08:00
|
|
|
let result = false;
|
2024-04-01 15:49:45 +08:00
|
|
|
Children.forEach(children as ReactElement, (element) => {
|
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);
|
2024-06-22 21:59:12 +08:00
|
|
|
const actionsContent = actions?.length && (
|
2024-06-03 11:24:39 +08:00
|
|
|
<ul
|
|
|
|
className={classNames(`${prefixCls}-item-action`, moduleClass('actions'))}
|
|
|
|
key="actions"
|
|
|
|
style={moduleStyle('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>,
|
2024-06-03 11:24:39 +08:00
|
|
|
<div
|
|
|
|
className={classNames(`${prefixCls}-item-extra`, moduleClass('extra'))}
|
|
|
|
key="extra"
|
|
|
|
style={moduleStyle('extra')}
|
|
|
|
>
|
2020-04-26 21:23:25 +08:00
|
|
|
{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
|
|
|
|
);
|
2024-04-09 16:49:47 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
export type ListItemTypeProps = typeof InternalItem & {
|
|
|
|
Meta: typeof Meta;
|
2020-04-26 21:23:25 +08:00
|
|
|
};
|
2024-04-09 16:49:47 +08:00
|
|
|
|
|
|
|
const Item = InternalItem as ListItemTypeProps;
|
2020-04-26 21:23:25 +08:00
|
|
|
|
|
|
|
Item.Meta = Meta;
|
|
|
|
|
|
|
|
export default Item;
|