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

Add emojisToShow filter #43

Merged
merged 2 commits into from
Feb 17, 2017
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 src/components/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default class Category extends React.Component {
updateDisplay(display) {
var emojis = this.getEmojis()

if (!display && !emojis) {
if (!emojis) {
return
}

Expand Down
36 changes: 31 additions & 5 deletions src/components/picker.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}
13 changes: 9 additions & 4 deletions src/utils/emoji-index.js
Original file line number Diff line number Diff line change
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,16 @@ function search(value, maxResults = 75) {
}
}

if (results && results.length) {
results = results.slice(0, maxResults)
let filtered_results = null;
if (results) {
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 }