react 使用 富文本编辑器并支持MD,同时支持<Form.Item/>的可控输入

1.分析

<Form.Item label="id" name="id" hidden>
     <Input />
</Form.Item>

在react ant 使用中,我们可以看到,Input 输入变化后,值会自动绑定到form实例上,同时,form set值以后,Input 也会跟着变化。这里面Input 默认挂载了两个属性value和onChange,从而做到了双向绑定,所以我们也可以实现一个这个的自定义可控组件

2.实现

2.1引入插件

npm install react-markdown-editor-lite markdown-it

2.2 封装组件

import React, { useEffect, useState } from 'react';
import MdEditor from 'react-markdown-editor-lite';
import MarkdownIt from 'markdown-it';
import 'react-markdown-editor-lite/lib/index.css';

// 初始化 markdown 解析器
const mdParser = new MarkdownIt();

export default ({ value, view, readOnly, onChange, ...resetProps }: PropsType) => {
  const [content, setContent] = useState(value);

  useEffect(() => {
    setContent(value);
  }, [value]);

  const handleEditorChange = ({ html, text }) => {
    setContent(text);
    onChange?.(text);
  };
  if (!view) {
    view = {
      menu: false,
      md: false,
      html: true,
    };
  }

  return (
    <MdEditor
      value={content}
      style={{ height: '500px' }}
      renderHTML={(text) => mdParser.render(text)}
      onChange={handleEditorChange}
      readOnly={readOnly ?? false}
      view={view}
      {...resetProps}
    />
  );
};

interface PropsType {
  value?: string;
  readOnly?: boolean;

2.2 使用

 <Form.Item 
 	label="公共内容"
    name="noticeContent"
    rules={[{ required: true }]}
    style={{ marginBottom: '8px' }}
>
     <WebEditor />
</Form.Item>

相关推荐

  1. Taro(React使用文本编辑器Editor

    2024-06-06 22:06:05       7 阅读
  2. React文本编辑器wangEditor

    2024-06-06 22:06:05       39 阅读
  3. react-draft-wysiwyg文本编辑器使用常见问题解答

    2024-06-06 22:06:05       11 阅读
  4. react集成tinymce文本编辑器

    2024-06-06 22:06:05       21 阅读
  5. React文本编辑器开发(十)变换

    2024-06-06 22:06:05       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-06 22:06:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-06 22:06:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-06 22:06:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-06 22:06:05       20 阅读

热门阅读

  1. iOS swift5 加载网络图片的第三方框架

    2024-06-06 22:06:05       8 阅读
  2. C++ 智能指针

    2024-06-06 22:06:05       11 阅读
  3. Docker命令大全

    2024-06-06 22:06:05       11 阅读
  4. WPS超级会员4年,2024年到手值得!

    2024-06-06 22:06:05       29 阅读
  5. 【python】匿名函数

    2024-06-06 22:06:05       10 阅读
  6. 东方博宜1542 - 小X算排名

    2024-06-06 22:06:05       10 阅读
  7. CSS中绝对定位和百分比问题(CSS中的小细节)

    2024-06-06 22:06:05       9 阅读