Skip to content

Commit

Permalink
add emojisToShow filter
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Osugi <tosugi@pivotal.io>
  • Loading branch information
Devin Brown authored and Thomas Osugi committed Feb 2, 2017
1 parent 626d5b1 commit 1a6e0e0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
36 changes: 31 additions & 5 deletions src/components/picker.js
Expand Up @@ -12,10 +12,7 @@ import { Anchors, Category, Emoji, Preview, Search } from '.'
const RECENT_CATEGORY = { name: 'Recent', emojis: null }
const SEARCH_CATEGORY = { name: 'Search', emojis: null, anchor: RECENT_CATEGORY }

const CATEGORIES = [
SEARCH_CATEGORY,
RECENT_CATEGORY,
].concat(data.categories)
let CATEGORIES = [];

const I18N = {
search: 'Search',
Expand All @@ -42,6 +39,32 @@ export default class Picker extends React.Component {
skin: store.get('skin') || props.skin,
firstRender: true,
}

let filteredCategories = [];

for (let hash of data.categories) {
let new_emojis = [];
for (let emoji of hash.emojis) {
let unified = data.emojis[emoji].unified;
if (props.emojisToShowFilter(unified)) {
new_emojis.push(emoji)
}
}

if (new_emojis.length) {
let new_hash = {
emojis: new_emojis,
name: hash.name
}
filteredCategories.push(new_hash);
}
}
CATEGORIES = [
SEARCH_CATEGORY,
RECENT_CATEGORY,
].concat(filteredCategories);

this.categories = CATEGORIES;
}

componentWillReceiveProps(props) {
Expand Down Expand Up @@ -238,7 +261,7 @@ export default class Picker extends React.Component {
}

render() {
var { perLine, emojiSize, set, sheetSize, style, title, emoji, color, backgroundImageFn } = this.props,
var { perLine, emojiSize, set, sheetSize, style, title, emoji, color, backgroundImageFn, emojisToShowFilter } = this.props,
{ skin } = this.state,
width = (perLine * (emojiSize + 12)) + 12 + 2

Expand All @@ -258,6 +281,7 @@ export default class Picker extends React.Component {
ref='search'
onSearch={this.handleSearch.bind(this)}
i18n={this.i18n}
emojisToShowFilter={emojisToShowFilter}
/>

{this.getCategories().map((category, i) => {
Expand Down Expand Up @@ -318,6 +342,7 @@ Picker.propTypes = {
backgroundImageFn: Emoji.propTypes.backgroundImageFn,
skin: Emoji.propTypes.skin,
sheetSize: Emoji.propTypes.sheetSize,
emojisToShowFilter: React.PropTypes.func,
}

Picker.defaultProps = {
Expand All @@ -333,4 +358,5 @@ Picker.defaultProps = {
skin: Emoji.defaultProps.skin,
sheetSize: Emoji.defaultProps.sheetSize,
backgroundImageFn: Emoji.defaultProps.backgroundImageFn,
emojisToShowFilter: (codePoint) => true,
}
4 changes: 3 additions & 1 deletion src/components/search.js
Expand Up @@ -6,7 +6,7 @@ export default class Search extends React.Component {
var { input } = this.refs,
value = input.value

this.props.onSearch(emojiIndex.search(value))
this.props.onSearch(emojiIndex.search(value, this.props.emojisToShowFilter, this.props.maxResults))
}

clear() {
Expand All @@ -29,9 +29,11 @@ export default class Search extends React.Component {
Search.propTypes = {
onSearch: React.PropTypes.func,
maxResults: React.PropTypes.number,
emojisToShowFilter: React.PropTypes.func
}

Search.defaultProps = {
onSearch: (() => {}),
maxResults: 75,
emojisToShowFilter: () => true
}
10 changes: 6 additions & 4 deletions src/utils/emoji-index.js
Expand Up @@ -20,7 +20,7 @@ for (let emoji in data.emojis) {
emojisList[id] = getSanitizedData(id)
}

function search(value, maxResults = 75) {
function search(value, emojisToShowFilter = () => true, maxResults = 75) {
var results = null

if (value.length) {
Expand Down Expand Up @@ -92,11 +92,13 @@ function search(value, maxResults = 75) {
}
}

if (results && results.length) {
results = results.slice(0, maxResults)
let filtered_results = (results || []).filter(
(result) => emojisToShowFilter(data.emojis[result.id].unified));
if (filtered_results && filtered_results.length) {
filtered_results = filtered_results.slice(0, maxResults)
}

return results
return filtered_results
}

export default { search, emojis: emojisList, emoticons: emoticonsList }

0 comments on commit 1a6e0e0

Please sign in to comment.