我已经建模了一个自定义工具栏,它使用以下代码笔在光标位置插入文本:
https://codepen.io/alexkrolick/pen/gmroPj?editors=0010
但是,我需要能够将道具值传递给insertText
函数。我已经尝试过重构,所以不能完全理解它。我将如何重构这个组件,以便我可以将 proptext
值传递给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'>×</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;