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';
|
|
|
|
|
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-10-29 22:05:42 +08:00
|
|
|
author?: string;
|
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 */
|
2018-10-29 22:05:42 +08:00
|
|
|
children?: any;
|
2018-10-22 21:20:28 +08:00
|
|
|
/** Optional ID for the comment */
|
|
|
|
id?: string;
|
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-29 22:05:42 +08:00
|
|
|
/** Direction of the comment left or right */
|
|
|
|
direction?: 'left' | 'right';
|
2018-10-22 21:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Comment extends React.Component<CommentProps, {}> {
|
|
|
|
getAction(actions: React.ReactNode[]) {
|
|
|
|
if (!actions || !actions.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const actionList = actions.map((action, index) => (
|
|
|
|
<li key={`action-${index}`}>
|
|
|
|
<span>{action}</span>
|
|
|
|
</li>
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return actionList;
|
|
|
|
}
|
|
|
|
|
2018-10-29 22:05:42 +08:00
|
|
|
renderNested = (child: any) => {
|
2018-10-29 01:17:00 +08:00
|
|
|
const { prefixCls = 'ant-comment' } = this.props;
|
|
|
|
const classString = classNames(`${prefixCls}-nested`);
|
|
|
|
|
|
|
|
return (
|
2018-10-29 22:05:42 +08:00
|
|
|
<div key={child.key} className={classString}>
|
2018-10-29 01:17:00 +08:00
|
|
|
{child}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-10-22 21:20:28 +08:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
actions,
|
|
|
|
author,
|
|
|
|
avatar,
|
|
|
|
children,
|
2018-10-22 22:28:58 +08:00
|
|
|
className,
|
2018-10-29 00:26:13 +08:00
|
|
|
content,
|
2018-10-29 22:05:42 +08:00
|
|
|
direction = 'left',
|
2018-10-22 21:20:28 +08:00
|
|
|
prefixCls = 'ant-comment',
|
|
|
|
style = {},
|
2018-10-30 00:17:26 +08:00
|
|
|
datetime,
|
2018-10-22 21:20:28 +08:00
|
|
|
...otherProps
|
|
|
|
} = this.props;
|
|
|
|
|
2018-10-29 22:05:42 +08:00
|
|
|
const classString = classNames(prefixCls, className, {
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'left',
|
|
|
|
[`${prefixCls}-ltr`]: direction === 'right',
|
|
|
|
});
|
2018-10-22 21:20:28 +08:00
|
|
|
|
2018-10-29 22:05:42 +08:00
|
|
|
const authorElements = [];
|
2018-10-22 22:28:58 +08:00
|
|
|
|
2018-10-29 22:05:42 +08:00
|
|
|
if (author) {
|
|
|
|
authorElements.push(
|
|
|
|
<span key="name" className={`${prefixCls}-content-author-name`}>
|
|
|
|
{author}
|
|
|
|
</span>,
|
|
|
|
);
|
2018-10-22 21:20:28 +08:00
|
|
|
}
|
|
|
|
|
2018-10-30 00:17:26 +08:00
|
|
|
if (datetime) {
|
2018-10-29 22:05:42 +08:00
|
|
|
authorElements.push(
|
2018-10-30 00:17:26 +08:00
|
|
|
<span key="time" className={`${prefixCls}-content-author-time`}>{datetime}</span>,
|
2018-10-29 22:05:42 +08:00
|
|
|
);
|
2018-10-22 21:20:28 +08:00
|
|
|
}
|
|
|
|
|
2018-10-29 22:05:42 +08:00
|
|
|
const avatarDom = (
|
2018-10-30 00:17:26 +08:00
|
|
|
<div key="avatar" className={`${prefixCls}-avatar`}>
|
2018-10-29 22:05:42 +08:00
|
|
|
{typeof avatar === 'string' ? <img src={avatar} /> : avatar}
|
2018-10-22 21:20:28 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
const actionDom = actions && actions.length
|
|
|
|
? <ul className={`${prefixCls}-actions`}>{this.getAction(actions)}</ul>
|
|
|
|
: null;
|
|
|
|
|
2018-10-29 22:05:42 +08:00
|
|
|
const authorContent = (
|
|
|
|
<div className={`${prefixCls}-content-author`}>
|
|
|
|
{direction === 'left' ? authorElements : authorElements.reverse()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2018-10-29 00:26:13 +08:00
|
|
|
const contentDom = (
|
2018-10-30 00:17:26 +08:00
|
|
|
<div key="content" className={`${prefixCls}-content`}>
|
2018-10-29 22:05:42 +08:00
|
|
|
{authorContent}
|
2018-10-22 21:20:28 +08:00
|
|
|
<div className={`${prefixCls}-content-detail`}>
|
2018-10-29 00:26:13 +08:00
|
|
|
{content}
|
2018-10-22 21:20:28 +08:00
|
|
|
</div>
|
|
|
|
{actionDom}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2018-10-29 00:26:13 +08:00
|
|
|
const comment = (
|
2018-10-22 22:28:58 +08:00
|
|
|
<div {...otherProps} className={classString} style={style}>
|
2018-10-30 00:17:26 +08:00
|
|
|
<div className={`${prefixCls}-inner`}>
|
2018-10-29 22:05:42 +08:00
|
|
|
{direction === 'left'
|
|
|
|
? [avatarDom, contentDom]
|
|
|
|
: [contentDom, avatarDom]
|
|
|
|
}
|
2018-10-22 21:20:28 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2018-10-29 00:26:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const nestedComments =
|
2018-10-29 22:05:42 +08:00
|
|
|
React.Children
|
|
|
|
.toArray(children)
|
|
|
|
.map((child: React.ReactElement<any>) => this.renderNested(child))
|
2018-10-29 00:26:13 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{comment}
|
|
|
|
{nestedComments}
|
|
|
|
</div>
|
2018-10-29 22:05:42 +08:00
|
|
|
);
|
2018-10-22 21:20:28 +08:00
|
|
|
}
|
|
|
|
}
|