ant-design/components/comment/demo/editor.md

108 lines
2.4 KiB
Markdown
Raw Normal View History

2018-10-25 21:32:29 +08:00
---
order: 3
title:
2018-12-04 16:51:13 +08:00
zh-CN: 回复框
en-US: Reply Editor
2018-10-25 21:32:29 +08:00
---
## zh-CN
2018-10-28 22:09:38 +08:00
评论编辑器组件提供了相同样式的封装以支持自定义评论编辑器。
2018-10-25 21:32:29 +08:00
## en-US
2019-04-20 06:35:29 +08:00
Comment can be used as an editor, so the user can customize the contents of the component.
2018-10-25 21:32:29 +08:00
```tsx
2022-05-23 14:37:16 +08:00
import { Avatar, Button, Comment, Form, Input, List } from 'antd';
2018-10-25 21:32:29 +08:00
import moment from 'moment';
2022-05-23 14:37:16 +08:00
import React, { useState } from 'react';
2018-10-25 21:32:29 +08:00
const { TextArea } = Input;
2018-10-25 21:32:29 +08:00
interface CommentItem {
author: string;
avatar: string;
content: React.ReactNode;
datetime: string;
}
interface EditorProps {
onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
onSubmit: () => void;
submitting: boolean;
value: string;
}
const CommentList = ({ comments }: { comments: CommentItem[] }) => (
2018-12-01 00:24:16 +08:00
<List
dataSource={comments}
header={`${comments.length} ${comments.length > 1 ? 'replies' : 'reply'}`}
itemLayout="horizontal"
renderItem={props => <Comment {...props} />}
/>
);
2018-10-25 21:32:29 +08:00
const Editor = ({ onChange, onSubmit, submitting, value }: EditorProps) => (
<>
2018-12-04 16:51:13 +08:00
<Form.Item>
2018-12-01 00:24:16 +08:00
<TextArea rows={4} onChange={onChange} value={value} />
2018-12-04 16:51:13 +08:00
</Form.Item>
<Form.Item>
2019-05-07 14:57:32 +08:00
<Button htmlType="submit" loading={submitting} onClick={onSubmit} type="primary">
2018-12-01 00:24:16 +08:00
Add Comment
</Button>
2018-12-04 16:51:13 +08:00
</Form.Item>
</>
2018-12-01 00:24:16 +08:00
);
2018-10-25 21:32:29 +08:00
const App: React.FC = () => {
const [comments, setComments] = useState<CommentItem[]>([]);
const [submitting, setSubmitting] = useState(false);
const [value, setValue] = useState('');
2018-10-25 21:32:29 +08:00
const handleSubmit = () => {
if (!value) return;
2018-11-02 22:52:35 +08:00
setSubmitting(true);
2018-10-25 21:32:29 +08:00
setTimeout(() => {
setSubmitting(false);
setValue('');
setComments([
...comments,
{
author: 'Han Solo',
avatar: 'https://joeschmoe.io/api/v1/random',
content: <p>{value}</p>,
datetime: moment('2016-11-22').fromNow(),
},
]);
2018-10-25 21:32:29 +08:00
}, 1000);
2019-05-07 14:57:32 +08:00
};
2018-10-25 21:32:29 +08:00
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setValue(e.target.value);
2019-05-07 14:57:32 +08:00
};
2018-11-02 18:17:28 +08:00
return (
<>
{comments.length > 0 && <CommentList comments={comments} />}
<Comment
avatar={<Avatar src="https://joeschmoe.io/api/v1/random" alt="Han Solo" />}
content={
<Editor
onChange={handleChange}
onSubmit={handleSubmit}
submitting={submitting}
value={value}
/>
}
/>
</>
);
};
export default App;
2019-05-07 14:57:32 +08:00
```