2018-10-22 21:20:28 +08:00
|
|
|
import * as React from 'react';
|
2018-10-22 22:28:58 +08:00
|
|
|
import classNames from 'classnames';
|
2020-04-23 20:52:15 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2018-10-22 22:28:58 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
function getAction(actions: React.ReactNode[]) {
|
|
|
|
if (!actions || !actions.length) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-06 15:02:05 +08:00
|
|
|
// eslint-disable-next-line react/no-array-index-key
|
2019-08-05 18:38:10 +08:00
|
|
|
const actionList = actions.map((action, index) => <li key={`action-${index}`}>{action}</li>);
|
|
|
|
return actionList;
|
|
|
|
}
|
|
|
|
|
2018-10-22 21:20:28 +08:00
|
|
|
export interface CommentProps {
|
|
|
|
/** List of action items rendered below the comment content */
|
|
|
|
actions?: Array<React.ReactNode>;
|
|
|
|
/** The element to display as the comment author. */
|
2018-12-18 03:03:37 +08:00
|
|
|
author?: React.ReactNode;
|
2018-10-22 21:20:28 +08:00
|
|
|
/** The element to display as the comment avatar - generally an antd Avatar */
|
2018-10-29 22:05:42 +08:00
|
|
|
avatar?: React.ReactNode;
|
2018-10-22 22:28:58 +08:00
|
|
|
/** className of comment */
|
|
|
|
className?: string;
|
2018-10-22 21:20:28 +08:00
|
|
|
/** The main content of the comment */
|
2018-10-29 00:26:13 +08:00
|
|
|
content: React.ReactNode;
|
|
|
|
/** Nested comments should be provided as children of the Comment */
|
2019-06-16 20:51:47 +08:00
|
|
|
children?: React.ReactNode;
|
2018-10-22 22:28:58 +08:00
|
|
|
/** Comment prefix defaults to '.ant-comment' */
|
2018-10-22 21:20:28 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
/** Additional style for the comment */
|
|
|
|
style?: React.CSSProperties;
|
2018-10-30 00:17:26 +08:00
|
|
|
/** A datetime element containing the time to be displayed */
|
|
|
|
datetime?: React.ReactNode;
|
2018-10-22 21:20:28 +08:00
|
|
|
}
|
|
|
|
|
2020-04-23 20:52:15 +08:00
|
|
|
const Comment: React.FC<CommentProps> = ({
|
|
|
|
actions,
|
|
|
|
author,
|
|
|
|
avatar,
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
content,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
style,
|
|
|
|
datetime,
|
|
|
|
...otherProps
|
|
|
|
}) => {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2018-10-29 01:17:00 +08:00
|
|
|
|
2020-04-23 20:52:15 +08:00
|
|
|
const renderNested = (prefixCls: string, nestedChildren: any) => {
|
|
|
|
return <div className={classNames(`${prefixCls}-nested`)}>{nestedChildren}</div>;
|
|
|
|
};
|
2018-10-22 21:20:28 +08:00
|
|
|
|
2020-04-23 20:52:15 +08:00
|
|
|
const prefixCls = getPrefixCls('comment', customizePrefixCls);
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-05-09 16:50:34 +08:00
|
|
|
const avatarDom = avatar ? (
|
2020-04-23 20:52:15 +08:00
|
|
|
<div className={`${prefixCls}-avatar`}>
|
|
|
|
{typeof avatar === 'string' ? <img src={avatar} alt="comment-avatar" /> : avatar}
|
|
|
|
</div>
|
2020-05-09 16:50:34 +08:00
|
|
|
) : null;
|
2018-10-22 21:20:28 +08:00
|
|
|
|
2020-04-23 20:52:15 +08:00
|
|
|
const actionDom =
|
|
|
|
actions && actions.length ? (
|
|
|
|
<ul className={`${prefixCls}-actions`}>{getAction(actions)}</ul>
|
|
|
|
) : null;
|
2018-10-22 21:20:28 +08:00
|
|
|
|
2020-04-23 20:52:15 +08:00
|
|
|
const authorContent = (
|
|
|
|
<div className={`${prefixCls}-content-author`}>
|
|
|
|
{author && <span className={`${prefixCls}-content-author-name`}>{author}</span>}
|
|
|
|
{datetime && <span className={`${prefixCls}-content-author-time`}>{datetime}</span>}
|
|
|
|
</div>
|
|
|
|
);
|
2018-10-29 22:05:42 +08:00
|
|
|
|
2020-04-23 20:52:15 +08:00
|
|
|
const contentDom = (
|
|
|
|
<div className={`${prefixCls}-content`}>
|
|
|
|
{authorContent}
|
|
|
|
<div className={`${prefixCls}-content-detail`}>{content}</div>
|
|
|
|
{actionDom}
|
|
|
|
</div>
|
|
|
|
);
|
2018-10-22 21:20:28 +08:00
|
|
|
|
2020-04-23 20:52:15 +08:00
|
|
|
const comment = (
|
|
|
|
<div className={`${prefixCls}-inner`}>
|
|
|
|
{avatarDom}
|
|
|
|
{contentDom}
|
|
|
|
</div>
|
|
|
|
);
|
2018-10-29 00:26:13 +08:00
|
|
|
|
2020-04-23 20:52:15 +08:00
|
|
|
const cls = classNames(prefixCls, className, {
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<div {...otherProps} className={cls} style={style}>
|
|
|
|
{comment}
|
|
|
|
{children ? renderNested(prefixCls, children) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-04-23 20:52:15 +08:00
|
|
|
export default Comment;
|