reacthooks和antd form.submit校验react-draft-wysiwyg富文本框,输入内容提示立马消失。写一个小demo
时间: 2024-04-08 13:30:33 浏览: 221
当使用React Hooks和Antd的Form组件进行表单提交校验时,React Draft Wysiwyg富文本框的输入内容提示立即消失的问题可以通过以下小demo进行解决:
首先,确保你已经安装了React、Antd、React Draft Wysiwyg和相关依赖。
然后,创建一个新的React组件,命名为FormDemo.js,并添加以下代码:
```jsx
import React, { useState } from 'react';
import { Form, Input, Button } from 'antd';
import { EditorState, ContentState, convertToRaw } from 'draft-js';
import { Editor } from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
const FormDemo = () => {
const [form] = Form.useForm();
const [editorState, setEditorState] = useState(EditorState.createEmpty());
const handleSubmit = (values) => {
const contentState = editorState.getCurrentContent();
const rawContent = convertToRaw(contentState);
values.editorContent = JSON.stringify(rawContent);
console.log(values);
form.resetFields();
setEditorState(EditorState.createEmpty());
};
const handleEditorStateChange = (state) => {
setEditorState(state);
};
return (
<Form form={form} onFinish={handleSubmit}>
<Form.Item
name="name"
label="Name"
rules={[
{ required: true, message: 'Please input your name' },
]}
>
<Input />
</Form.Item>
<Form.Item
name="editorContent"
label="Content"
rules={[
{ required: true, message: 'Please input content' },
]}
>
<Editor
editorState={editorState}
onEditorStateChange={handleEditorStateChange}
/>
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Submit
</Button>
</Form.Item>
</Form>
);
};
export default FormDemo;
```
在上述代码中,我们创建了一个包含名字和内容的表单。内容字段使用React Draft Wysiwyg富文本编辑器。当提交表单时,我们将富文本内容转换为JSON字符串,并在控制台中打印表单值。然后,我们重置表单字段并清空富文本编辑器的内容。
最后,你可以在你的应用程序中使用FormDemo组件。确保你已经正确导入并渲染了该组件。
这样,当你在富文本框中输入内容时,输入内容提示不会立即消失,而是会根据校验规则进行校验。只有当表单提交时,才会清空输入内容并重置表单字段。
希望这个小demo对你有所帮助!如果你有任何进一步的问题,请随时提问。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)