【一】react-draft-wysiwyg富文本使用

5 篇文章 0 订阅

1.安装

npm install react-draft-wysiwyg
npm install draft-js
npm install draftjs-to-html
npm install html-to-draftjs

2.引用

import { Editor } from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import draftToHtml from 'draftjs-to-html';
import { EditorState, ContentState } from 'draft-js';
import htmlToDraft from 'html-to-draftjs';

3.使用

    const textEditor = (
      <Editor
        editorState={editorState}
        toolbarClassName="home-toolbar"
        wrapperClassName="home-wrapper"
        editorClassName="home-editor"
        onEditorStateChange={this.onEditorStateChange}
        toolbar={{
          history: { inDropdown: true },
          inline: { inDropdown: false },
          list: { inDropdown: true },
          textAlign: { inDropdown: true },
          image: {
            urlEnabled: false,
            uploadEnabled: true,
            alignmentEnabled: true,   // 是否显示排列按钮 相当于text-align
            uploadCallback: this.imageUploadCallBack,
            previewImage: true,
            inputAccept: 'image/*',
            alt: {present: false, mandatory: false}
          }
        }}
        onContentStateChange={this.onEditorContentChange}
        placeholder="请输文章内容"
        spellCheck
        localization={{ locale: 'zh', translations: { 'generic.add': '添加' } }}
      />

    );

下个文章主要记录下toorbar 上传图片本地预览的问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用React Hooks和Antd的Form组件进行表单校,并且配合React-Draft-Wysig富文本框时,输入内容提示立即消失的问题可以通过以下方式解决: 首先,确已经安装了相关的依赖包: ` npm install antd react-draft-wysiwy draft-js ``` 然后,创建一个新的组件,命名为DemoForm: ``` import React, { useState } from ''; import { Form, Input, Button } fromantd'; import { EditorState, ContentState from 'draft-js'; import { } from 'react-draft-wysiwyg'; import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css'; const DemoForm = () => { const [editorState, setEditorState] = useState(EditorState.createEmpty()); const [form] = Form.useForm(); const handleFormSubmit = (values) => { console.log(values); }; const handleEditorChange = (state) => { setEditorState(state); }; return ( <Form form={form} onFinish={handleFormSubmit}> <Form.Item name="content" rules={[ { required: true, message: '请输入内容', }, ]} > <Editor editorState={editorState} onEditorStateChange={handleEditorChange} /> </Form.Item> <Form.Item> <Button type="primary" htmlType="submit"> 提交 </Button> </Form.Item> </Form> ); }; export default DemoForm; ``` 在上面的代码中,我们使用useState钩子来管理富文本框的编辑状态(editorState)和Form组件的表单状态(form)。onEditorStateChange函数用于更新编辑状态,handleFormSubmit函数用于处理表单提交。 在Form.Item组件中,我们使用了rules属性来进行表单校验。在这个例子中,我们要求content字段是必填的,如果没有输入内容,会显示"请输入内容"的错误提示。 最后,通过使用Editor组件来展示富文本框,并且将editorState和handleEditorChange函数传递给它。 这样,在输入内容时,如果没有满足校验规则,会显示错误提示,直到满足规则或者手动清空输入内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值