ant-design/components/comment/index.tsx

100 lines
2.9 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 { ConfigConsumer, ConfigConsumerProps } 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;
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 */
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
}
export default class Comment extends React.Component<CommentProps, {}> {
getAction(actions: React.ReactNode[]) {
if (!actions || !actions.length) {
return null;
}
2018-12-07 20:02:01 +08:00
const actionList = actions.map((action, index) => <li key={`action-${index}`}>{action}</li>);
2018-10-22 21:20:28 +08:00
return actionList;
}
renderNested = (prefixCls: string, children: any) => {
2018-12-07 20:02:01 +08:00
return <div className={classNames(`${prefixCls}-nested`)}>{children}</div>;
};
2018-10-29 01:17:00 +08:00
renderComment = ({ getPrefixCls }: ConfigConsumerProps) => {
2018-10-22 21:20:28 +08:00
const {
actions,
author,
avatar,
children,
2018-10-22 22:28:58 +08:00
className,
content,
prefixCls: customizePrefixCls,
2018-11-02 02:28:21 +08:00
style,
2018-10-30 00:17:26 +08:00
datetime,
2018-10-22 21:20:28 +08:00
...otherProps
} = this.props;
const prefixCls = getPrefixCls('comment', customizePrefixCls);
const avatarDom = (
2018-10-30 00:26:12 +08:00
<div className={`${prefixCls}-avatar`}>
{typeof avatar === 'string' ? <img src={avatar} /> : avatar}
2018-10-22 21:20:28 +08:00
</div>
);
2018-12-07 20:02:01 +08:00
const actionDom =
actions && actions.length ? (
<ul className={`${prefixCls}-actions`}>{this.getAction(actions)}</ul>
) : null;
2018-10-22 21:20:28 +08:00
const authorContent = (
<div className={`${prefixCls}-content-author`}>
2018-12-07 20:02:01 +08:00
{author && <span className={`${prefixCls}-content-author-name`}>{author}</span>}
{datetime && <span className={`${prefixCls}-content-author-time`}>{datetime}</span>}
</div>
);
const contentDom = (
2018-10-30 00:26:12 +08:00
<div className={`${prefixCls}-content`}>
{authorContent}
2018-12-07 20:02:01 +08:00
<div className={`${prefixCls}-content-detail`}>{content}</div>
2018-10-22 21:20:28 +08:00
{actionDom}
</div>
);
const comment = (
2018-10-31 20:53:10 +08:00
<div className={`${prefixCls}-inner`}>
{avatarDom}
{contentDom}
2018-10-22 21:20:28 +08:00
</div>
2018-10-30 19:25:54 +08:00
);
return (
2018-11-02 02:28:21 +08:00
<div {...otherProps} className={classNames(prefixCls, className)} style={style}>
{comment}
{children ? this.renderNested(prefixCls, children) : null}
</div>
);
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderComment}</ConfigConsumer>;
}
2018-10-22 21:20:28 +08:00
}