missive / emoji-mart

🏪 One component to pick them all
https://missiveapp.com/open/emoji-mart
MIT License
8.67k stars 835 forks source link

[Question] Input Match support #336

Closed li6834300 closed 5 years ago

li6834300 commented 5 years ago

Hi, Does emoji-mart support input matching? for instance, if I key in ' :sometest: ' in my input area, I expect there is a selector that gives me recomandation of related emoji that related to keyword. Just like something in this input box image

EtienneLem commented 5 years ago

The headless search is probably what you’re looking for, that’s what we’re using at Missive to autocomplete the emojis in the chat input.

Here’s the function called on keyup to extract the text that can be used as a search term, which of course will probably need some tweaking to fit your codebase:

getCurrentlyTypedEmoji() {
  let match, text;

  if (!(match = this.input.value.substring(0, this.input.selectionStart).match(/(^|\W):((:?\w|\+|\-)[^:]*)?$/))) {
    return null
  }

  text = match[0].match(/:(.*)/)[1];
  if ((text.match(RegExp(' ', 'g')) || []).length > 1) {
    return null
  }

  return text
}

// Snippet of code in `keyup` event
let emojiSearch;
if (emojiSearch = getCurrentlyTypedEmoji()) {
  let emojiSearchResults = emojiIndex.search(emojiSearch)
  console.log(emojiSearchResults)
}
li6834300 commented 5 years ago

Thanks, that's what I am looking for!