Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support matching suggestions on spacebar #1

Merged
merged 1 commit into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion draft-js-mention-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "draft-js-mention-plugin",
"version": "3.1.5",
"version": "3.1.5-withAutocomplete",
"description": "Mention Plugin for DraftJS",
"author": {
"name": "Nik Graf",
Expand Down
17 changes: 17 additions & 0 deletions draft-js-mention-plugin/src/MentionSuggestions/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import find from 'lodash/find';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { genKey } from 'draft-js';
Expand All @@ -12,6 +13,7 @@ export class MentionSuggestions extends Component {
static propTypes = {
entityMutability: PropTypes.oneOf(['SEGMENTED', 'IMMUTABLE', 'MUTABLE']),
entryComponent: PropTypes.func,
matchSuggestion: PropTypes.func,
onAddMention: PropTypes.func,
suggestions: PropTypes.array,
};
Expand Down Expand Up @@ -245,6 +247,16 @@ export class MentionSuggestions extends Component {
this.props.store.setEditorState(this.props.store.getEditorState());
};

onSpacebar = keyboardEvent => {
const matchingSuggestion = find(this.props.suggestions, suggestion =>
this.props.matchSuggestion(suggestion, this.lastSearchValue)
);
if (matchingSuggestion) {
keyboardEvent.preventDefault();
this.onMentionSelect(matchingSuggestion);
}
};

onMentionSelect = mention => {
// Note: This can happen in case a user typed @xxx (invalid mention) and
// then hit Enter. Then the mention will be undefined.
Expand Down Expand Up @@ -310,6 +322,10 @@ export class MentionSuggestions extends Component {
if (keyboardEvent.keyCode === 9) {
this.onTab(keyboardEvent);
}
// spacebar
if (keyboardEvent.keyCode === 32) {
this.onSpacebar(keyboardEvent);
}
};

const descendant = `mention-option-${this.key}-${this.state.focusedOptionIndex}`;
Expand Down Expand Up @@ -364,6 +380,7 @@ export class MentionSuggestions extends Component {
positionSuggestions, // eslint-disable-line no-unused-vars
mentionTrigger, // eslint-disable-line no-unused-vars
mentionPrefix, // eslint-disable-line no-unused-vars
matchSuggestion, // eslint-disable-line no-unused-vars
...elementProps
} = this.props;

Expand Down
2 changes: 2 additions & 0 deletions draft-js-mention-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default (config = {}) => {
mentionTrigger = '@',
mentionRegExp = defaultRegExp,
supportWhitespace = false,
matchSuggestion,
} = config;
const mentionSearchProps = {
ariaProps,
Expand All @@ -93,6 +94,7 @@ export default (config = {}) => {
positionSuggestions,
mentionTrigger,
mentionPrefix,
matchSuggestion,
};
const DecoratedMentionSuggestionsComponent = props => (
<MentionSuggestionsComponent {...props} {...mentionSearchProps} />
Expand Down