initial comment component

This commit is contained in:
Ilan 2018-10-22 15:20:28 +02:00 committed by 偏右
parent 553f5cfcfc
commit 53dd4731f1
11 changed files with 718 additions and 0 deletions

View File

@ -0,0 +1,268 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`renders ./components/comment/demo/basic.md correctly 1`] = `
<div
class="ant-comment"
>
<div
class="ant-comment-inner"
>
<div
class="ant-comment-header"
>
<span
class="ant-comment-header-avatar"
>
<span
class="ant-avatar ant-avatar-circle ant-avatar-image"
>
<img
alt="Han Solo"
src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
/>
</span>
</span>
<div
class="ant-comment-header-author"
>
<span
class="ant-comment-header-author-name"
>
<a>
Han Solo
</a>
</span>
<span
class="ant-comment-header-author-time"
>
a few seconds ago
</span>
</div>
</div>
<div
class="ant-comment-content"
>
<div
class="ant-comment-content-detail"
>
<p>
Sagittis id consectetur purus ut faucibus pulvinar elementum integer enim. Pellentesque massa placerat duis ultricies lacus sed turpis. Tempus urna et pharetra pharetra massa massa.
</p>
</div>
<ul
class="ant-comment-actions"
>
<li>
<span>
<span>
<i
class="anticon anticon-like"
>
<svg
aria-hidden="true"
class=""
data-icon="like"
fill="currentColor"
height="1em"
viewBox="64 64 896 896"
width="1em"
>
<path
d="M885.9 533.7c16.8-22.2 26.1-49.4 26.1-77.7 0-44.9-25.1-87.4-65.5-111.1a67.67 67.67 0 0 0-34.3-9.3H572.4l6-122.9c1.4-29.7-9.1-57.9-29.5-79.4A106.62 106.62 0 0 0 471 99.9c-52 0-98 35-111.8 85.1l-85.9 311h-.3v428h472.3c9.2 0 18.2-1.8 26.5-5.4 47.6-20.3 78.3-66.8 78.3-118.4 0-12.6-1.8-25-5.4-37 16.8-22.2 26.1-49.4 26.1-77.7 0-12.6-1.8-25-5.4-37 16.8-22.2 26.1-49.4 26.1-77.7-.2-12.6-2-25.1-5.6-37.1zM112 528v364c0 17.7 14.3 32 32 32h65V496h-65c-17.7 0-32 14.3-32 32z"
/>
</svg>
</i>
<span
style="padding-left:8px;cursor:auto"
>
0
</span>
</span>
</span>
</li>
<li>
<span>
<span>
<i
class="anticon anticon-dislike"
>
<svg
aria-hidden="true"
class=""
data-icon="dislike"
fill="currentColor"
height="1em"
viewBox="64 64 896 896"
width="1em"
>
<path
d="M885.9 490.3c3.6-12 5.4-24.4 5.4-37 0-28.3-9.3-55.5-26.1-77.7 3.6-12 5.4-24.4 5.4-37 0-28.3-9.3-55.5-26.1-77.7 3.6-12 5.4-24.4 5.4-37 0-51.6-30.7-98.1-78.3-118.4a66.1 66.1 0 0 0-26.5-5.4H273v428h.3l85.8 310.8C372.9 889 418.9 924 470.9 924c29.7 0 57.4-11.8 77.9-33.4 20.5-21.5 31-49.7 29.5-79.4l-6-122.9h239.9c12.1 0 23.9-3.2 34.3-9.3 40.4-23.5 65.5-66.1 65.5-111 0-28.3-9.3-55.5-26.1-77.7zM112 132v364c0 17.7 14.3 32 32 32h65V100h-65c-17.7 0-32 14.3-32 32z"
/>
</svg>
</i>
<span
style="padding-left:8px;cursor:auto"
>
0
</span>
</span>
</span>
</li>
<li>
<span>
<span>
Reply to
</span>
</span>
</li>
</ul>
</div>
</div>
</div>
`;
exports[`renders ./components/comment/demo/list.md correctly 1`] = `
<div
class="ant-list comment-list ant-list-split"
>
<div
class="ant-list-header"
>
2 replies
</div>
<div
class="ant-spin-nested-loading"
>
<div
class="ant-spin-container"
>
<div
class="ant-list-item"
>
<div
class="ant-list-item-content ant-list-item-content-single"
>
<div
class="ant-comment"
>
<div
class="ant-comment-inner"
>
<div
class="ant-comment-header"
>
<span
class="ant-comment-header-avatar"
>
<img
src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
/>
</span>
<div
class="ant-comment-header-author"
>
<span
class="ant-comment-header-author-name"
>
Han Solo
</span>
<span
class="ant-comment-header-author-time"
>
a day ago
</span>
</div>
</div>
<div
class="ant-comment-content"
>
<div
class="ant-comment-content-detail"
>
<p>
Nisl nisi scelerisque eu ultrices vitae auctor eu augue. Nulla at volutpat diam ut venenatis tellus in metus vulputate.
</p>
</div>
<ul
class="ant-comment-actions"
>
<li>
<span>
<span>
Reply to
</span>
</span>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div
class="ant-list-item"
>
<div
class="ant-list-item-content ant-list-item-content-single"
>
<div
class="ant-comment"
>
<div
class="ant-comment-inner"
>
<div
class="ant-comment-header"
>
<span
class="ant-comment-header-avatar"
>
<img
src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
/>
</span>
<div
class="ant-comment-header-author"
>
<span
class="ant-comment-header-author-name"
>
Stormtrooper
</span>
<span
class="ant-comment-header-author-time"
>
2 days ago
</span>
</div>
</div>
<div
class="ant-comment-content"
>
<div
class="ant-comment-content-detail"
>
<p>
Sed turpis tincidunt id aliquet risus feugiat in ante metus. Faucibus nisl tincidunt eget nullam non.
</p>
</div>
<ul
class="ant-comment-actions"
>
<li>
<span>
<span>
Reply to
</span>
</span>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
`;

View File

@ -0,0 +1,3 @@
import demoTest from '../../../tests/shared/demoTest';
demoTest('comment');

View File

@ -0,0 +1,94 @@
---
order: 0
title:
zh-CN: 基本评论
en-US: Basic comment
---
## zh-CN
一个基本的评论作者,头像,时间和行动。
## en-US
A basic comment with author, avatar, time and actions.
````jsx
import { Comment, Icon, Tooltip, Avatar } from 'antd';
import moment from 'moment';
class App extends React.Component {
state = {
likes: 0,
dislikes: 0,
action: null,
}
like = () => {
this.setState({
likes: 1,
dislikes: 0,
action: 'liked',
});
}
dislike = () => {
this.setState({
likes: 0,
dislikes: 1,
action: 'disliked',
})
}
render() {
const { likes, dislikes, action } = this.state;
const actions = [
<span>
<Tooltip title="Like">
<Icon
type="like"
theme={action === 'liked' ? 'twoTone' : 'filled'}
onClick={this.like}
/>
</Tooltip>
<span style={{ paddingLeft: 8, cursor: 'auto' }}>
{likes}
</span>
</span>,
<span>
<Tooltip title="Dislike">
<Icon
type="dislike"
theme={action === 'disliked' ? 'twoTone' : 'filled'}
onClick={this.dislike}
/>
</Tooltip>
<span style={{ paddingLeft: 8, cursor: 'auto' }}>
{dislikes}
</span>
</span>,
<span>Reply to</span>,
];
return (
<Comment
author={<a>Han Solo</a>}
avatar={
<Avatar
src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
alt="Han Solo"
/>
}
time={moment().fromNow()}
tooltipTime={moment().format('YYYY-MM-DD HH:mm:ss')}
actions={actions}
>
<p>Sagittis id consectetur purus ut faucibus pulvinar elementum integer enim. Pellentesque massa placerat duis ultricies lacus sed turpis. Tempus urna et pharetra pharetra massa massa.</p>
</Comment>
);
}
}
ReactDOM.render(<App />, mountNode);
````

View File

@ -0,0 +1,83 @@
---
order: 1
title:
zh-CN: 用法用名单
en-US: Usage With List
---
## zh-CN
基本评论使用`antd` List组件来呈现一系列注释。
## en-US
A basic comment used with `antd` List component to render a series of comments.
````jsx
import { Comment, Icon, Tooltip, List } from 'antd';
import moment from 'moment';
const data = [
{
author: 'Han Solo',
avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
time: moment().subtract(1, 'days').fromNow(),
tooltipTime: moment().subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss'),
actions: [
<span>Reply to</span>
],
children: (
<p>Nisl nisi scelerisque eu ultrices vitae auctor eu augue. Nulla at volutpat diam ut venenatis tellus in metus vulputate.</p>
)
},
{
author: 'Stormtrooper',
avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
time: moment().subtract(2, 'days').fromNow(),
tooltipTime: moment().subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss'),
actions: [
<span>Reply to</span>
],
children: (
<p>Sed turpis tincidunt id aliquet risus feugiat in ante metus. Faucibus nisl tincidunt eget nullam non.</p>
)
},
];
ReactDOM.render(
<List
className="comment-list"
header={`${data.length} replies`}
itemLayout="horizontal"
dataSource={data}
renderItem={item => (
<List.Item>
<Comment
actions={item.actions}
author={item.author}
avatar={item.avatar}
time={item.time}
tooltipTime={item.tooltipTime}
>
{item.children}
</Comment>
</List.Item>
)}
/>,
mountNode);
````
<style>
.comment-list .ant-card-head {
padding: 0;
margin-bottom: 0;
}
.comment-list.ant-list .ant-list-item {
padding: 0;
border-bottom: 0;
}
.comment-list.ant-list .ant-list-item .ant-list-item-content {
display: block;
}
</style>

View File

@ -0,0 +1,29 @@
---
category: Components
type: Data Display
title: Comment
cols: 1
---
Single comment component.
## When To Use
Comments can be used to enable discussions on an entity for example page, blog post, issue or other.
## API
| Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- |
actions | List of action items rendered below the comment content | Array<ReactNode> | - |
author | The element to display as the comment author | ReactNode | - |
avatar | The element to display as the comment avatar - generally an antd `Avatar` | ReactNode | - |
children | The main content of the comment | ReactNode | - |
contentStyle | Inline style to apply to the comment content | object | - |
headStyle | Inline style to apply to the comment head | object | - |
id | Optional ID for the comment | string | - |
innerStyle | Additional style for the inner content | object | - |
prefixCls | Comment prefix className defaults to `.ant-comment` | string | .ant-comment |
style | Additional style for the comment | object | - |
time | A time element containing the time to be displayed | ReactNode | - |
tooltipTime | A time element to be displayed as the time tooltip | ReactNode | - |

View File

@ -0,0 +1,114 @@
import * as React from 'react';
import Tooltip from '../tooltip';
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;
/** 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;
/** Comment prefix className defaults to '.ant-comment' */
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, {}> {
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,
contentStyle = {},
headStyle = {},
innerStyle = {},
prefixCls = 'ant-comment',
style = {},
time,
tooltipTime,
...otherProps
} = this.props;
const avatarDom = typeof avatar === 'string'
? <img src={avatar} />
: avatar;
let timeDom;
if (time) {
timeDom = <span className={`${prefixCls}-header-author-time`}>{time}</span>
}
if (time && tooltipTime) {
timeDom = (
<Tooltip title={tooltipTime}>
<span className={`${prefixCls}-header-author-time`}>{time}</span>
</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 (
<div {...otherProps} className={prefixCls} style={style}>
<div className={`${prefixCls}-inner`} style={innerStyle}>
{head}
{content}
</div>
</div>
);
}
}

View File

@ -0,0 +1,30 @@
---
category: Components
type: Data Display
title: 评论
subtitle: 分割线
cols: 1
---
单一评论组件。
## When To Use
评论可用于启用对实体的讨论,例如页面,博客文章,问题或其他。
## API
| Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- |
actions | 在评论内容下面呈现的操作项列表 | Array<ReactNode> | - |
author | 要显示为注释作者的元素 | ReactNode | - |
avatar | 要显示为评论头像的元素 - 通常是antd `Avatar` | ReactNode | - |
children | 评论的主要内容 | ReactNode | - |
contentStyle | 要应用于评论内容的内联样式 | object | - |
headStyle | 要应用于注释头的内联样式 | object | - |
id | 评论的可选ID | string | - |
innerStyle | 内容的附加风格 | object | - |
prefixCls | 注释前缀className默认为`.ant-comment` | string | .ant-comment |
style | 评论的其他风格 | object | - |
time | 包含要显示的时间的时间元素 | ReactNode | - |
tooltipTime | 要显示为时间工具提示的时间元素 | ReactNode | - |

View File

@ -0,0 +1,83 @@
@import "../../style/themes/default";
@import "../../style/mixins/index";
@comment-prefix-cls: ~"@{ant-prefix}-comment";
.@{comment-prefix-cls} {
position: relative;
&-inner {
padding: @comment-padding-base;
}
&-header {
margin-bottom: @comment-header-margin;
padding-bottom: @comment-header-padding;
&-avatar {
position: relative;
float: left;
margin-right: 12px;
cursor: pointer;
img {
width: 32px;
height: 32px;
border-radius: 16px;
}
}
&-author {
padding-left: 12px;
flex: auto;
font-size: 14px;
& > a,
& > span {
display: inline-block;
height: 18px;
font-size: 12px;
line-height: 18px;
padding-right: 8px;
cursor: pointer;
}
&-name {
transition: color 0.3s ease;
font-size: 14px;
color: @comment-author-name-color;
* > {
color: @comment-author-name-color;
&:hover {
color: @comment-author-name-color;
}
}
}
&-time {
cursor: pointer;
color: @comment-author-time-color;
white-space: nowrap;
&:hover {
text-decoration: underline;
}
}
}
}
&-content {
position: relative;
padding-left: 44px;
font-size: 14px;
}
&-actions {
margin-top: 12px;
& > li {
float: left;
color: @comment-action-color;
& > span {
padding-right: 10px;
-webkit-transition: color 0.3s ease;
transition: color 0.3s ease;
font-size: 12px;
color: @comment-action-color;
cursor: pointer;
user-select: none;
&:hover {
color: @comment-action-hover-color;
}
}
}
}
}

View File

@ -0,0 +1,2 @@
import '../../style/index.less';
import './index.less';

View File

@ -45,6 +45,8 @@ export { default as Checkbox } from './checkbox';
export { default as Col } from './col';
export { default as Comment } from './comment';
export { default as DatePicker } from './date-picker';
export { default as Divider } from './divider';

View File

@ -394,6 +394,16 @@
@card-actions-background: @background-color-light;
@card-shadow: 0 2px 8px rgba(0, 0, 0, .09);
// Comment
// ---
@comment-padding-base: 16px 0;
@comment-header-margin: 4px;
@comment-header-padding: 8px;
@comment-author-name-color: #8c8c8c;
@comment-author-time-color: #ccc;
@comment-action-color: #8c8c8c;
@comment-action-hover-color: #595959;
// Tabs
// ---
@tabs-card-head-background: @background-color-light;