ant-design/components/comment/index.tsx

95 lines
2.7 KiB
TypeScript
Raw Normal View History

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';
import { ConfigContext } from '../config-provider';
2018-10-22 22:28:58 +08:00
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 */
avatar?: React.ReactNode;
/** ClassName of comment */
2018-10-22 22:28:58 +08:00
className?: string;
2018-10-22 21:20:28 +08:00
/** The main content of the comment */
content: React.ReactNode;
/** Nested comments should be provided as children of the Comment */
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
}
const Comment: React.FC<CommentProps> = ({
actions,
author,
avatar,
children,
className,
content,
prefixCls: customizePrefixCls,
datetime,
...otherProps
}) => {
const { getPrefixCls, direction } = React.useContext(ConfigContext);
2018-10-29 01:17:00 +08:00
const renderNested = (prefixCls: string, nestedChildren: any) => (
<div className={classNames(`${prefixCls}-nested`)}>{nestedChildren}</div>
);
2018-10-22 21:20:28 +08:00
const prefixCls = getPrefixCls('comment', customizePrefixCls);
const avatarDom = avatar ? (
<div className={`${prefixCls}-avatar`}>
{typeof avatar === 'string' ? <img src={avatar} alt="comment-avatar" /> : avatar}
</div>
) : null;
2018-10-22 21:20:28 +08:00
const actionDom =
actions && actions.length ? (
<ul className={`${prefixCls}-actions`}>
{actions.map((action, index) => (
<li key={`action-${index}`}>{action}</li> // eslint-disable-line react/no-array-index-key
))}
</ul>
) : null;
2018-10-22 21:20:28 +08:00
const authorContent = (author || datetime) && (
<div className={`${prefixCls}-content-author`}>
{author && <span className={`${prefixCls}-content-author-name`}>{author}</span>}
{datetime && <span className={`${prefixCls}-content-author-time`}>{datetime}</span>}
</div>
);
const contentDom = (
<div className={`${prefixCls}-content`}>
{authorContent}
<div className={`${prefixCls}-content-detail`}>{content}</div>
{actionDom}
</div>
);
2018-10-22 21:20:28 +08:00
const cls = classNames(
prefixCls,
{
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);
return (
<div {...otherProps} className={cls}>
<div className={`${prefixCls}-inner`}>
{avatarDom}
{contentDom}
</div>
{children ? renderNested(prefixCls, children) : null}
</div>
);
};
export default Comment;