Skip to content

Commit

Permalink
Support matching suggestions on spacebar
Browse files Browse the repository at this point in the history
  • Loading branch information
ndrjkvc committed Sep 16, 2019
1 parent 95ded7f commit 7fa4455
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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

0 comments on commit 7fa4455

Please sign in to comment.