Created
November 10, 2017 21:17
-
-
Save levvsha/7cbcd6c4197c54dc27ba5dc374d920fe to your computer and use it in GitHub Desktop.
Draft.js mentions plugin with suggestions list that shown above the cursor
This file contains 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 { EditorState } from 'draft-js'; | |
import Editor from 'draft-js-plugins-editor'; | |
import createMentionPlugin, { defaultSuggestionsFilter } from 'draft-js-mention-plugin'; | |
import editorStyles from './editorStyles.css'; | |
import 'draft-js-mention-plugin/lib/plugin.css'; | |
const mentions = [ | |
{ | |
name: 'Matthew Russell', | |
link: 'https://twitter.com/mrussell247', | |
avatar: 'https://pbs.twimg.com/profile_images/517863945/mattsailing_400x400.jpg', | |
}, | |
{ | |
name: 'Julian Krispel-Samsel', | |
link: 'https://twitter.com/juliandoesstuff', | |
avatar: 'https://avatars2.githubusercontent.com/u/1188186?v=3&s=400', | |
}, | |
{ | |
name: 'Jyoti Puri', | |
link: 'https://twitter.com/jyopur', | |
avatar: 'https://avatars0.githubusercontent.com/u/2182307?v=3&s=400', | |
}, | |
{ | |
name: 'Max Stoiber', | |
link: 'https://twitter.com/mxstbr', | |
avatar: 'https://pbs.twimg.com/profile_images/763033229993574400/6frGyDyA_400x400.jpg', | |
}, | |
{ | |
name: 'Nik Graf', | |
link: 'https://twitter.com/nikgraf', | |
avatar: 'https://avatars0.githubusercontent.com/u/223045?v=3&s=400', | |
}, | |
{ | |
name: 'Pascal Brandt', | |
link: 'https://twitter.com/psbrandt', | |
avatar: 'https://pbs.twimg.com/profile_images/688487813025640448/E6O6I011_400x400.png', | |
}, | |
]; | |
export default class SimpleMentionEditor extends Component { | |
constructor(props) { | |
super(props); | |
this.mentionPlugin = createMentionPlugin({ | |
positionSuggestions: (settings) => { | |
return { | |
left: settings.decoratorRect.left + 'px', | |
top: settings.decoratorRect.top - 40 + 'px', | |
display: 'block', | |
transform: 'scale(1) translateY(-100%)', | |
transformOrigin: '1em 0% 0px', | |
transition: 'all 0.25s cubic-bezier(0.3, 1.2, 0.2, 1)' | |
}; | |
} | |
}); | |
} | |
state = { | |
editorState: EditorState.createEmpty(), | |
suggestions: mentions, | |
}; | |
onChange = (editorState) => { | |
this.setState({ | |
editorState, | |
}); | |
}; | |
onSearchChange = ({ value }) => { | |
this.setState({ | |
suggestions: defaultSuggestionsFilter(value, mentions), | |
}); | |
}; | |
onAddMention = () => { | |
// get the mention object selected | |
} | |
focus = () => { | |
this.editor.focus(); | |
}; | |
render() { | |
const { MentionSuggestions } = this.mentionPlugin; | |
const plugins = [this.mentionPlugin]; | |
return ( | |
<div className={editorStyles.editor} onClick={this.focus}> | |
<Editor | |
editorState={this.state.editorState} | |
onChange={this.onChange} | |
plugins={plugins} | |
ref={(element) => { this.editor = element; }} | |
/> | |
<MentionSuggestions | |
onSearchChange={this.onSearchChange} | |
suggestions={this.state.suggestions} | |
onAddMention={this.onAddMention} | |
/> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment