【问题标题】:Pass props to react-quill handler将 props 传递给 react-quill 处理程序
【发布时间】:2019-08-30 17:16:21
【问题描述】:

我已经建模了一个自定义工具栏,它使用以下代码笔在光标位置插入文本:

https://codepen.io/alexkrolick/pen/gmroPj?editors=0010

但是,我需要能够将 prop 值传递给 insertText 函数。我已经尝试重构,所以不能完全理解它。我将如何重构这个组件,以便我可以将 prop text 值传递给 insertText 函数?这是我现在的代码:

import React, { Component } from 'react'
import ReactQuill from 'react-quill'

function insertText() {
  const text = 'example123'
  // need this to be accessed from props.text
  const cursorPosition = this.quill.getSelection().index;

  this.quill.insertText(cursorPosition, text);
  this.quill.setSelection(cursorPosition + (text.length));
}

const CustomToolbar = () => (
  <div id="toolbar">
    <select className="ql-header" defaultValue={""} onChange={e => e.persist()}>
      <option value="1" />
      <option value="2" />
      <option selected />
    </select>
    <button className="ql-bold" />
    <button className="ql-italic" />
    <button className="ql-insertText">
      Insert Promo Code
    </button>
  </div>
);

class Editor extends Component {
  constructor(props) {
    super(props);
    // Note: text is passed in as a prop
  }

  render() {
    const { template, handleChange, onSave } = this.props

    return (
      <div className='modal fade' id='instruction-template-edit-modal' tabIndex='-1' role='dialog' aria-labelledby='myModalLabel'>
        <div className='modal-dialog modal-lg' role='document'>
          <div className='modal-content'>
            <div className='modal-header'>
              <button
                type='button'
                className='close'
                data-dismiss='modal'
                aria-label='Close'>
                <span aria-hidden='true'>&times;</span>
              </button>
              <h4
                className='modal-title general-header-text margin-left-15'
                id='myModalLabel'>
                Edit Instruction Template
              </h4>
            </div>
            <div className='modal-body'>
              <div className='instruction-template row text-editor'>
                <CustomToolbar />
                <ReactQuill value={template.content || ''} 
                  modules={Editor.modules}
                  formats={Editor.formats}
                  onChange={handleChange} />
              </div>
              <div className='row margin-top-20'>
                <a type='button'
                  className='cancel-link'
                  data-dismiss='modal'
                  aria-label='Close'>
                  Cancel
                </a>
                <button className='button-blue pull-right'
                  data-dismiss='modal'
                  aria-label='Save'
                  onClick={() => onSave(template) }>
                  SAVE
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    )
  }
}

Editor.modules = {
  toolbar: {
    container: "#toolbar",
    handlers: {
      insertText: insertText
    }
  },
  clipboard: {
    matchVisual: false,
  }
};

Editor.formats = [
  "header",
  "font",
  "size",
  "bold",
  "italic",
  "underline",
  "strike",
  "blockquote",
  "list",
  "bullet",
  "indent",
  "link",
  "image",
  "color"
];

export default Editor;

【问题讨论】:

    标签: javascript reactjs quill react-quill


    【解决方案1】:

    首先,将 Editor.modules 设为类属性,以便您可以访问 props,然后通过传递 callback ref,然后将 propsquill editor 作为参数传递给 insertText 函数。

    我已更新您的示例以使其正常工作,这是链接:

    https://codepen.io/jojo-tutor/pen/VwZzPdx?editors=0010
    
    /*
     * Custom "star" icon for the toolbar using an Octicon
     * https://octicons.github.io
     */
    const CustomButton = () => <span className="octicon octicon-star" />;
    
    /*
     * Event handler to be attached using Quill toolbar module (see line 73)
     * https://quilljs.com/docs/modules/toolbar/
     */
    function insertStar(quillEditor, props) {
      console.log({ quillEditor, props })
    
      const cursorPosition = quillEditor.getSelection().index;
      quillEditor.insertText(cursorPosition, props.character);
      quillEditor.setSelection(cursorPosition + 1);
    }
    
    /*
     * Custom toolbar component including insertStar button and dropdowns
     */
    const CustomToolbar = () => (
      <div id="toolbar">
        <select className="ql-header" defaultValue={""} onChange={e => e.persist()}>
          <option value="1" />
          <option value="2" />
          <option selected />
        </select>
        <button className="ql-bold" />
        <button className="ql-italic" />
        <select className="ql-color">
          <option value="red" />
          <option value="green" />
          <option value="blue" />
          <option value="orange" />
          <option value="violet" />
          <option value="#d0d1d2" />
          <option selected />
        </select>
        <button className="ql-insertStar">
          <CustomButton />
        </button>
      </div>
    );
    
    /* 
     * Editor component with custom toolbar and content containers
     */
    class Editor extends React.Component {
      constructor(props) {
        super(props);
        this.state = { editorHtml: "" };
        this.handleChange = this.handleChange.bind(this);
        this.reactQuillRef = null
      }
    
      handleChange(html) {
        this.setState({ editorHtml: html });
      }
    
      modules = {
        toolbar: {
          container: "#toolbar",
          handlers: {
              insertStar: () => insertStar(
                this.reactQuillRef.getEditor(),
                this.props
              )
            }
        },
    
        clipboard: {
          matchVisual: false,
        }
      }
    
      render() {
        return (
          <div className="text-editor">
            <CustomToolbar />
            <ReactQuill
              ref={(el) => { this.reactQuillRef = el }}
              onChange={this.handleChange}
              placeholder={this.props.placeholder}
              modules={this.modules}
              formats={Editor.formats}
              theme={"snow"} // pass false to use minimal theme
            />
          </div>
        );
      }
    }
    
    /* 
     * Quill modules to attach to editor
     * See https://quilljs.com/docs/modules/ for complete options
     */
    
    /* 
     * Quill editor formats
     * See https://quilljs.com/docs/formats/
     */
    Editor.formats = [
      "header",
      "font",
      "size",
      "bold",
      "italic",
      "underline",
      "strike",
      "blockquote",
      "list",
      "bullet",
      "indent",
      "link",
      "image",
      "color"
    ];
    
    /* 
     * PropType validation
     */
    Editor.propTypes = {
      placeholder: PropTypes.string
    };
    
    /* 
     * Render component on page
     */
    ReactDOM.render(
      <Editor placeholder={"Write something or insert a star ★"} character="[star]" />,
      document.querySelector(".app")
    );
    
    

    希望这会有所帮助。编码愉快!

    【讨论】:

      猜你喜欢
      • 2015-11-19
      • 2019-06-07
      • 2019-08-11
      • 2023-03-08
      • 2021-03-17
      • 2021-09-17
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      相关资源
      最近更新 更多