Last active
June 5, 2022 06:49
-
-
Save devmnj/a747a2f96bf7a874e877cfbdd4728a83 to your computer and use it in GitHub Desktop.
Quilljs Mention - React component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from 'react'; | |
| import ReactQuill from 'react-quill'; | |
| import 'react-quill/dist/quill.snow.css'; | |
| import 'quill-mention/dist/quill.mention.css'; | |
| import 'quill-mention'; | |
| const atValues = [ | |
| { id: 1, value: 'Fredrik Sundqvist' }, | |
| { id: 2, value: 'Patrik Sjölin' }, | |
| ]; | |
| export default class Editor extends Component { | |
| constructor(props) { | |
| super(props); | |
| } | |
| tagModeules = { | |
| toolbar: false, | |
| mention: { | |
| allowedChars: /^[A-Za-z\sÅÄÖåäö]*$/, | |
| mentionDenotationChars: ['@', '#'], | |
| source: function (searchTerm, renderItem, mentionChar) { | |
| let values; | |
| if (mentionChar === '@' || mentionChar === '#') { | |
| values = atValues; | |
| } | |
| if (searchTerm.length === 0) { | |
| renderItem(values, searchTerm); | |
| } else { | |
| const matches = []; | |
| for (let i = 0; i < values.length; i++) | |
| if ( | |
| ~values[i].value.toLowerCase().indexOf(searchTerm.toLowerCase()) | |
| ) | |
| matches.push(values[i]); | |
| renderItem(matches, searchTerm); | |
| } | |
| }, | |
| }, | |
| }; | |
| handleProcedureContentChange = (content, delta, source, editor) => { | |
| let has_attribues = delta.ops[1].attributes || ""; | |
| console.log(has_attribues); | |
| const cursorPosition = e.quill.getSelection().index; | |
| this.quill.insertText(cursorPosition, "★"); | |
| this.quill.setSelection(cursorPosition + 1); | |
| }; | |
| render() { | |
| return ( | |
| <div class="flex flex-row"> | |
| <div> | |
| <label className="" for="tags"> | |
| Post Tags | |
| </label> | |
| <ReactQuill | |
| id="tags" | |
| style={{ height: 150 }} | |
| placeholder="#tags here" | |
| modules={this.tagModeules} | |
| > | |
| <div className="my-editing-area" /> | |
| </ReactQuill> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment