ant-design/components/comment/index.tsx

131 lines
3.5 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';
2018-10-22 21:20:28 +08:00
import Tooltip from '../tooltip';
2018-10-25 21:32:29 +08:00
import Nested from './Nested';
import Editor from './Editor';
2018-10-22 21:20:28 +08:00
2018-10-22 22:28:58 +08:00
export { NestedCommentProps } from './Nested';
2018-10-25 21:32:29 +08:00
export { CommentEditorProps } from './Editor';
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. */
author: string;
/** 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 */
children: React.ReactNode;
/** Additional style for the comment content */
contentStyle?: React.CSSProperties;
/** Additional style for the comment head */
headStyle?: React.CSSProperties;
/** Optional ID for the comment */
id?: string;
/** Additional style for the comment inner wrapper */
innerStyle?: React.CSSProperties;
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;
/** A time element containing the time to be displayed */
time?: React.ReactNode;
/** A time element to be displayed as the time tooltip */
tooltipTime?: React.ReactNode;
}
export default class Comment extends React.Component<CommentProps, {}> {
2018-10-22 22:28:58 +08:00
static Nested: typeof Nested = Nested;
2018-10-25 21:32:29 +08:00
static Editor: typeof Editor = Editor;
2018-10-22 22:28:58 +08:00
2018-10-22 21:20:28 +08:00
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;
}
render() {
const {
actions,
author,
avatar,
children,
2018-10-22 22:28:58 +08:00
className,
2018-10-22 21:20:28 +08:00
contentStyle = {},
headStyle = {},
innerStyle = {},
prefixCls = 'ant-comment',
style = {},
time,
tooltipTime,
...otherProps
} = this.props;
2018-10-24 16:24:20 +08:00
const classString = classNames(prefixCls, className);
2018-10-22 21:20:28 +08:00
const avatarDom = typeof avatar === 'string'
? <img src={avatar} />
: avatar;
let timeDom;
2018-10-22 22:28:58 +08:00
2018-10-22 21:20:28 +08:00
if (time) {
timeDom = <span className={`${prefixCls}-header-author-time`}>{time}</span>
}
if (time && tooltipTime) {
timeDom = (
<Tooltip title={tooltipTime}>
2018-10-22 22:28:58 +08:00
<span className={`${prefixCls}-header-author-time ${prefixCls}-header-author-time-tooltip`}>
{time}
</span>
2018-10-22 21:20:28 +08:00
</Tooltip>
)
}
const head = (
<div className={`${prefixCls}-header`} style={headStyle}>
<span className={`${prefixCls}-header-avatar`}>
{avatarDom}
</span>
<div className={`${prefixCls}-header-author`}>
<span className={`${prefixCls}-header-author-name`}>
{author}
</span>
{timeDom}
</div>
</div>
);
const actionDom = actions && actions.length
? <ul className={`${prefixCls}-actions`}>{this.getAction(actions)}</ul>
: null;
const content = (
<div className={`${prefixCls}-content`} style={contentStyle}>
<div className={`${prefixCls}-content-detail`}>
{children}
</div>
{actionDom}
</div>
);
return (
2018-10-22 22:28:58 +08:00
<div {...otherProps} className={classString} style={style}>
2018-10-22 21:20:28 +08:00
<div className={`${prefixCls}-inner`} style={innerStyle}>
{head}
{content}
</div>
</div>
);
}
}