Skip to content

Commit

Permalink
Use prefix filter to match search query
Browse files Browse the repository at this point in the history
  • Loading branch information
arisgk committed Sep 16, 2017
1 parent 3422b11 commit 1ea98ac
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/components/Home/AutocompleteEditor/AutocompleteEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ import './styles/autocomplete.css';
const mentionPlugin = createAutocompletePlugin({
mentionPrefix: '@',
mentionTrigger: '@',
entityMutability: 'IMMUTABLE',
});
const MentionSuggestions = mentionPlugin.MentionSuggestions;

const hashtagPlugin = createAutocompletePlugin({
mentionPrefix: '#',
mentionTrigger: '#',
entityMutability: 'IMMUTABLE',
});
const HashtagSuggestions = hashtagPlugin.MentionSuggestions;

const relationPlugin = createAutocompletePlugin({
mentionPrefix: '<>',
mentionTrigger: '<>',
entityMutability: 'IMMUTABLE',
});
const RelationSuggestions = relationPlugin.MentionSuggestions;

Expand Down
4 changes: 2 additions & 2 deletions src/selectors/hashtags/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createSelector } from 'reselect';
import { defaultSuggestionsFilter } from 'draft-js-mention-plugin';
import prefixFilter from '../../utils/suggestions/prefixFilter';

const getEntities = state => state.home.entities.hashtags;
const getSearch = state => state.home.ui.hashtags.search;

export const getSuggestions = createSelector(
[getEntities, getSearch],
(entities, search) => defaultSuggestionsFilter(search, entities),
(entities, search) => prefixFilter(search, entities, 7),
);
4 changes: 2 additions & 2 deletions src/selectors/mentions/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createSelector } from 'reselect';
import { defaultSuggestionsFilter } from 'draft-js-mention-plugin';
import prefixFilter from '../../utils/suggestions/prefixFilter';

const getEntities = state => state.home.entities.mentions;
const getSearch = state => state.home.ui.mentions.search;

export const getSuggestions = createSelector(
[getEntities, getSearch],
(entities, search) => defaultSuggestionsFilter(search, entities),
(entities, search) => prefixFilter(search, entities, 7),
);
4 changes: 2 additions & 2 deletions src/selectors/relations/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createSelector } from 'reselect';
import { defaultSuggestionsFilter } from 'draft-js-mention-plugin';
import prefixFilter from '../../utils/suggestions/prefixFilter';

const getEntities = state => state.home.entities.relations;
const getSearch = state => state.home.ui.relations.search;

export const getSuggestions = createSelector(
[getEntities, getSearch],
(entities, search) => defaultSuggestionsFilter(search, entities),
(entities, search) => prefixFilter(search, entities, 7),
);
18 changes: 18 additions & 0 deletions src/utils/suggestions/prefixFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const size = list => (list.constructor.name === 'List'
? list.size
: list.length);

const get = (obj, attr) => (obj.get
? obj.get(attr)
: obj[attr]);

const filter = (searchValue, suggestions, maxResults) => {
const value = searchValue.toLowerCase();
const filteredSuggestions = suggestions.filter(suggestion => (
!value || get(suggestion, 'name').toLowerCase().startsWith(value)
));
const length = size(filteredSuggestions) < maxResults ? size(filteredSuggestions) : maxResults;
return filteredSuggestions.slice(0, length);
};

export default filter;

0 comments on commit 1ea98ac

Please sign in to comment.