2018-10-25 21:32:29 +08:00
|
|
|
---
|
|
|
|
order: 3
|
|
|
|
title:
|
|
|
|
zh-CN: 编辑模式
|
|
|
|
en-US: Editor mode
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
2018-10-28 22:09:38 +08:00
|
|
|
评论编辑器组件提供了相同样式的封装以支持自定义评论编辑器。
|
2018-10-25 21:32:29 +08:00
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Comment can be used as editor, user can customize the editor component.
|
|
|
|
|
|
|
|
````jsx
|
2018-10-29 22:05:42 +08:00
|
|
|
import { Comment, Icon, Tooltip, Avatar, Form, Button, List, Input, Radio } from 'antd';
|
2018-10-25 21:32:29 +08:00
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
const FormItem = Form.Item;
|
|
|
|
const TextArea = Input.TextArea;
|
|
|
|
|
|
|
|
function hasErrors(fieldsError) {
|
|
|
|
return Object.keys(fieldsError).some(field => fieldsError[field]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function CommentList({ comments }) {
|
|
|
|
return (
|
|
|
|
<List
|
|
|
|
header={`${comments.length} ${comments.length > 1 ? 'replies' : 'reply'}`}
|
|
|
|
itemLayout="horizontal"
|
|
|
|
dataSource={comments}
|
2018-10-29 00:26:13 +08:00
|
|
|
renderItem={props => <Comment {...props} />}
|
2018-10-25 21:32:29 +08:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
class Editor extends React.Component {
|
|
|
|
handleSubmit = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.props.form.validateFields((err, values) => {
|
|
|
|
if (!err) {
|
|
|
|
this.props.onSubmit(
|
|
|
|
values,
|
|
|
|
() => this.props.form.resetFields()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { getFieldDecorator, getFieldsError, getFieldError, isFieldTouched } = this.props.form;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form onSubmit={this.handleSubmit}>
|
|
|
|
<FormItem>
|
|
|
|
{getFieldDecorator('message', {
|
|
|
|
rules: [{ required: true, message: 'Please input your message!' }],
|
|
|
|
})(
|
|
|
|
<TextArea rows={4} />
|
|
|
|
)}
|
|
|
|
</FormItem>
|
|
|
|
<FormItem>
|
|
|
|
<Button
|
|
|
|
disabled={hasErrors(getFieldsError())}
|
|
|
|
htmlType="submit"
|
|
|
|
loading={this.props.submitting}
|
|
|
|
type="primary"
|
|
|
|
>
|
|
|
|
Add Comment
|
|
|
|
</Button>
|
|
|
|
</FormItem>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const EditorForm = Form.create()(Editor);
|
|
|
|
|
|
|
|
class App extends React.Component {
|
|
|
|
state = {
|
|
|
|
comments: [],
|
|
|
|
submitting: false,
|
2018-10-29 22:05:42 +08:00
|
|
|
direction: 'left',
|
2018-10-25 21:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit = (values, cb) => {
|
|
|
|
this.setState({
|
|
|
|
submitting: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
this.setState({
|
|
|
|
submitting: false,
|
|
|
|
comments: [
|
|
|
|
{
|
|
|
|
author: 'Han Solo',
|
|
|
|
avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
|
2018-10-29 00:26:13 +08:00
|
|
|
content: values.message,
|
2018-10-25 21:32:29 +08:00
|
|
|
time: moment().fromNow(),
|
|
|
|
},
|
|
|
|
...this.state.comments,
|
|
|
|
]
|
|
|
|
})
|
|
|
|
if (cb) cb();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
2018-10-29 22:05:42 +08:00
|
|
|
handleChange = (e) => {
|
|
|
|
this.setState({ direction: e.target.value });
|
|
|
|
}
|
|
|
|
|
2018-10-25 21:32:29 +08:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
{this.state.comments.length > 0 && (
|
|
|
|
<CommentList
|
|
|
|
comments={this.state.comments}
|
|
|
|
/>
|
|
|
|
)}
|
2018-10-29 22:05:42 +08:00
|
|
|
<Form layout="inline">
|
|
|
|
<FormItem label="Editor direction">
|
|
|
|
<Radio.Group size="default" value={this.state.direction} onChange={this.handleChange}>
|
|
|
|
<Radio.Button value="left">Left</Radio.Button>
|
|
|
|
<Radio.Button value="right">Right</Radio.Button>
|
|
|
|
</Radio.Group>
|
|
|
|
</FormItem>
|
|
|
|
</Form>
|
|
|
|
<Comment
|
|
|
|
direction={this.state.direction}
|
2018-10-25 21:32:29 +08:00
|
|
|
avatar={
|
|
|
|
<Avatar
|
|
|
|
src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"
|
|
|
|
alt="Han Solo"
|
|
|
|
/>
|
|
|
|
}
|
2018-10-29 00:26:13 +08:00
|
|
|
content={
|
|
|
|
<EditorForm
|
|
|
|
submitting={this.state.submitting}
|
|
|
|
onSubmit={this.handleSubmit}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
2018-10-25 21:32:29 +08:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(<App />, mountNode);
|
|
|
|
````
|