Comment评论
对网站内容的反馈、评价和讨论。
何时使用#
评论组件可用于对事物的讨论,例如页面、博客文章、问题等等。
代码演示
We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.
- 0
- 0
- Reply to
import React, { createElement, useState } from 'react';
import { Comment, Tooltip, Avatar } from 'antd';
import moment from 'moment';
import { DislikeOutlined, LikeOutlined, DislikeFilled, LikeFilled } from '@ant-design/icons';
const Demo = () => {
const [likes, setLikes] = useState(0);
const [dislikes, setDislikes] = useState(0);
const [action, setAction] = useState(null);
const like = () => {
setLikes(1);
setDislikes(0);
setAction('liked');
};
const dislike = () => {
setLikes(0);
setDislikes(1);
setAction('disliked');
};
const actions = [
<Tooltip key="comment-basic-like" title="Like">
<span onClick={like}>
{createElement(action === 'liked' ? LikeFilled : LikeOutlined)}
<span className="comment-action">{likes}</span>
</span>
</Tooltip>,
<Tooltip key="comment-basic-dislike" title="Dislike">
<span onClick={dislike}>
{React.createElement(action === 'disliked' ? DislikeFilled : DislikeOutlined)}
<span className="comment-action">{dislikes}</span>
</span>
</Tooltip>,
<span key="comment-basic-reply-to">Reply to</span>,
];
return (
<Comment
actions={actions}
author={<a>Han Solo</a>}
avatar={
<Avatar
src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
alt="Han Solo"
/>
}
content={
<p>
We supply a series of design principles, practical patterns and high quality design
resources (Sketch and Axure), to help people create their product prototypes beautifully
and efficiently.
</p>
}
datetime={
<Tooltip title={moment().format('YYYY-MM-DD HH:mm:ss')}>
<span>{moment().fromNow()}</span>
</Tooltip>
}
/>
);
};
ReactDOM.render(<Demo />, mountNode);
/* tile uploaded pictures */
.comment-action {
padding-left: 8px;
cursor: 'auto';
}
[class*='-col-rtl'] .comment-action {
padding-right: 8px;
padding-left: 0;
}
import { Comment, Tooltip, List } from 'antd';
import moment from 'moment';
const data = [
{
actions: [<span key="comment-list-reply-to-0">Reply to</span>],
author: 'Han Solo',
avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
content: (
<p>
We supply a series of design principles, practical patterns and high quality design
resources (Sketch and Axure), to help people create their product prototypes beautifully and
efficiently.
</p>
),
datetime: (
<Tooltip
title={moment()
.subtract(1, 'days')
.format('YYYY-MM-DD HH:mm:ss')}
>
<span>
{moment()
.subtract(1, 'days')
.fromNow()}
</span>
</Tooltip>
),
},
{
actions: [<span key="comment-list-reply-to-0">Reply to</span>],
author: 'Han Solo',
avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
content: (
<p>
We supply a series of design principles, practical patterns and high quality design
resources (Sketch and Axure), to help people create their product prototypes beautifully and
efficiently.
</p>
),
datetime: (
<Tooltip
title={moment()
.subtract(2, 'days')
.format('YYYY-MM-DD HH:mm:ss')}
>
<span>
{moment()
.subtract(2, 'days')
.fromNow()}
</span>
</Tooltip>
),
},
];
ReactDOM.render(
<List
className="comment-list"
header={`${data.length} replies`}
itemLayout="horizontal"
dataSource={data}
renderItem={item => (
<li>
<Comment
actions={item.actions}
author={item.author}
avatar={item.avatar}
content={item.content}
datetime={item.datetime}
/>
</li>
)}
/>,
mountNode,
);
We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure).
- Reply to
We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure).
- Reply to
We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure).
- Reply to
We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure).
- Reply to
import { Comment, Avatar } from 'antd';
const ExampleComment = ({ children }) => (
<Comment
actions={[<span key="comment-nested-reply-to">Reply to</span>]}
author={<a>Han Solo</a>}
avatar={
<Avatar
src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
alt="Han Solo"
/>
}
content={
<p>
We supply a series of design principles, practical patterns and high quality design
resources (Sketch and Axure).
</p>
}
>
{children}
</Comment>
);
ReactDOM.render(
<ExampleComment>
<ExampleComment>
<ExampleComment />
<ExampleComment />
</ExampleComment>
</ExampleComment>,
mountNode,
);
import { Comment, Avatar, Form, Button, List, Input } from 'antd';
import moment from 'moment';
const { TextArea } = Input;
const CommentList = ({ comments }) => (
<List
dataSource={comments}
header={`${comments.length} ${comments.length > 1 ? 'replies' : 'reply'}`}
itemLayout="horizontal"
renderItem={props => <Comment {...props} />}
/>
);
const Editor = ({ onChange, onSubmit, submitting, value }) => (
<>
<Form.Item>
<TextArea rows={4} onChange={onChange} value={value} />
</Form.Item>
<Form.Item>
<Button htmlType="submit" loading={submitting} onClick={onSubmit} type="primary">
Add Comment
</Button>
</Form.Item>
</>
);
class App extends React.Component {
state = {
comments: [],
submitting: false,
value: '',
};
handleSubmit = () => {
if (!this.state.value) {
return;
}
this.setState({
submitting: true,
});
setTimeout(() => {
this.setState({
submitting: false,
value: '',
comments: [
{
author: 'Han Solo',
avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
content: <p>{this.state.value}</p>,
datetime: moment().fromNow(),
},
...this.state.comments,
],
});
}, 1000);
};
handleChange = e => {
this.setState({
value: e.target.value,
});
};
render() {
const { comments, submitting, value } = this.state;
return (
<>
{comments.length > 0 && <CommentList comments={comments} />}
<Comment
avatar={
<Avatar
src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
alt="Han Solo"
/>
}
content={
<Editor
onChange={this.handleChange}
onSubmit={this.handleSubmit}
submitting={submitting}
value={value}
/>
}
/>
</>
);
}
}
ReactDOM.render(<App />, mountNode);
API#
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
actions | 在评论内容下面呈现的操作项列表 | Array<ReactNode> | - | |
author | 要显示为注释作者的元素 | string | ReactNode | - | |
avatar | 要显示为评论头像的元素 - 通常是 antd Avatar 或者 src | string | ReactNode | - | |
children | 嵌套注释应作为注释的子项提供 | ReactNode | - | |
content | 评论的主要内容 | string | ReactNode | - | |
datetime | 展示时间描述 | string | ReactNode | - |
We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.
We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.