mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-12 23:35:38 +08:00
01ca7c7821
* test: replace testing-lib * test: replace testing-lib * test: replace testing-lib * test: update snap * test: replace testing-lib * test: replace testing-lib * test: update for lint * merge: merge * test: testing-lib * test: replace testing-lib * test: testing-lib * test: testing-lib * test: replace testing-lib * test: replace testing-lib * test: Replace test aria replacment logic * test: Update snapshot * chore: hack for id * test: clean up * test: clean demo * chore: update * test: snapshot update * test: fix snapshot rep logic * test: fix snapshot rep logic * test: fix snapshot rep logic * chore: update demo * test: fix snapshot rep logic * test: Update snapshot * test: rest snapshot * test: update snapshot * test: Update test case * test: config env * chore: clean up * chore: Use renderServer instead * test: adjust testing logic * test: modify test logic * test: split ssr test * test: skip if need skip * chore: ignore test file covv Co-authored-by: 二货机器人 <smith3816@gmail.com>
2.4 KiB
2.4 KiB
order | title | ||||
---|---|---|---|---|---|
3 |
|
zh-CN
评论编辑器组件提供了相同样式的封装以支持自定义评论编辑器。
en-US
Comment can be used as an editor, so the user can customize the contents of the component.
import { Avatar, Button, Comment, Form, Input, List } from 'antd';
import moment from 'moment';
import React, { useState } from 'react';
const { TextArea } = Input;
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[] }) => (
<List
dataSource={comments}
header={`${comments.length} ${comments.length > 1 ? 'replies' : 'reply'}`}
itemLayout="horizontal"
renderItem={props => <Comment {...props} />}
/>
);
const Editor = ({ onChange, onSubmit, submitting, value }: EditorProps) => (
<>
<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>
</>
);
const App: React.FC = () => {
const [comments, setComments] = useState<CommentItem[]>([]);
const [submitting, setSubmitting] = useState(false);
const [value, setValue] = useState('');
const handleSubmit = () => {
if (!value) return;
setSubmitting(true);
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(),
},
]);
}, 1000);
};
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setValue(e.target.value);
};
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;