emoji-mart-lazyload/src/components/picker.js

624 lines
16 KiB
JavaScript
Raw Normal View History

2016-07-06 19:45:34 +00:00
import '../vendor/raf-polyfill'
2016-06-02 15:26:48 +00:00
2016-05-31 14:36:52 +00:00
import React from 'react'
import PropTypes from 'prop-types'
import data from '../data'
2016-06-02 17:21:31 +00:00
2016-07-20 18:45:28 +00:00
import store from '../utils/store'
import frequently from '../utils/frequently'
import { deepMerge, measureScrollbar } from '../utils'
2016-07-20 18:45:28 +00:00
2016-10-27 03:27:55 +00:00
import { Anchors, Category, Emoji, Preview, Search } from '.'
2016-06-02 15:26:48 +00:00
2016-10-27 03:22:59 +00:00
const I18N = {
search: 'Search',
2017-04-18 15:20:17 +00:00
notfound: 'No Emoji Found',
2016-10-27 03:22:59 +00:00
categories: {
search: 'Search Results',
recent: 'Frequently Used',
people: 'Smileys & People',
nature: 'Animals & Nature',
foods: 'Food & Drink',
activity: 'Activity',
places: 'Travel & Places',
objects: 'Objects',
symbols: 'Symbols',
flags: 'Flags',
custom: 'Custom',
2016-10-27 03:22:59 +00:00
},
}
export default class Picker extends React.PureComponent {
2016-05-31 18:39:30 +00:00
constructor(props) {
super(props)
this.RECENT_CATEGORY = { id: 'recent', name: 'Recent', emojis: null }
this.CUSTOM_CATEGORY = { id: 'custom', name: 'Custom', emojis: [] }
this.SEARCH_CATEGORY = {
id: 'search',
name: 'Search',
emojis: null,
anchor: false,
}
2016-10-27 03:22:59 +00:00
this.i18n = deepMerge(I18N, props.i18n)
2016-05-31 18:39:30 +00:00
this.state = {
skin: props.skin || store.get('skin') || props.defaultSkin,
firstRender: true,
2016-05-31 18:39:30 +00:00
}
this.categories = []
let allCategories = [].concat(data.categories)
if (props.custom.length > 0) {
2018-03-27 18:51:26 +00:00
this.CUSTOM_CATEGORY.emojis = props.custom.map((emoji) => {
return {
...emoji,
// `<Category />` expects emoji to have an `id`.
id: emoji.short_names[0],
custom: true,
}
})
allCategories.push(this.CUSTOM_CATEGORY)
}
2017-05-29 11:36:49 +00:00
this.hideRecent = true
if (props.include != undefined) {
2017-09-29 23:45:52 +00:00
allCategories.sort((a, b) => {
if (props.include.indexOf(a.id) > props.include.indexOf(b.id)) {
return 1
}
return -1
})
}
2017-10-07 04:02:02 +00:00
for (
let categoryIndex = 0;
categoryIndex < allCategories.length;
categoryIndex++
) {
2017-09-17 08:54:22 +00:00
const category = allCategories[categoryIndex]
2017-10-07 04:02:02 +00:00
let isIncluded =
props.include && props.include.length
? props.include.indexOf(category.id) > -1
2017-10-07 04:02:02 +00:00
: true
let isExcluded =
props.exclude && props.exclude.length
? props.exclude.indexOf(category.id) > -1
2017-10-07 04:02:02 +00:00
: false
if (!isIncluded || isExcluded) {
continue
}
2017-04-04 16:00:32 +00:00
if (props.emojisToShowFilter) {
let newEmojis = []
2017-10-07 04:02:02 +00:00
const { emojis } = category
2017-09-17 08:54:22 +00:00
for (let emojiIndex = 0; emojiIndex < emojis.length; emojiIndex++) {
const emoji = emojis[emojiIndex]
if (props.emojisToShowFilter(data.emojis[emoji] || emoji)) {
newEmojis.push(emoji)
}
}
if (newEmojis.length) {
let newCategory = {
emojis: newEmojis,
name: category.name,
2018-01-09 20:20:18 +00:00
id: category.id,
}
2017-04-04 16:00:32 +00:00
this.categories.push(newCategory)
}
} else {
this.categories.push(category)
}
}
2017-04-04 16:00:32 +00:00
2017-10-07 04:02:02 +00:00
let includeRecent =
props.include && props.include.length
? props.include.indexOf(this.RECENT_CATEGORY.id) > -1
2017-10-07 04:02:02 +00:00
: true
let excludeRecent =
props.exclude && props.exclude.length
? props.exclude.indexOf(this.RECENT_CATEGORY.id) > -1
2017-10-07 04:02:02 +00:00
: false
2017-05-29 11:36:49 +00:00
if (includeRecent && !excludeRecent) {
this.hideRecent = false
this.categories.unshift(this.RECENT_CATEGORY)
}
if (this.categories[0]) {
this.categories[0].first = true
}
this.categories.unshift(this.SEARCH_CATEGORY)
2017-10-07 04:02:02 +00:00
this.setAnchorsRef = this.setAnchorsRef.bind(this)
this.handleAnchorClick = this.handleAnchorClick.bind(this)
2017-10-07 04:02:02 +00:00
this.setSearchRef = this.setSearchRef.bind(this)
this.handleSearch = this.handleSearch.bind(this)
this.setScrollRef = this.setScrollRef.bind(this)
this.handleScroll = this.handleScroll.bind(this)
this.handleScrollPaint = this.handleScrollPaint.bind(this)
2017-10-07 04:02:02 +00:00
this.handleEmojiOver = this.handleEmojiOver.bind(this)
this.handleEmojiLeave = this.handleEmojiLeave.bind(this)
this.handleEmojiClick = this.handleEmojiClick.bind(this)
2018-03-15 19:36:07 +00:00
this.handleEmojiSelect = this.handleEmojiSelect.bind(this)
2017-10-07 04:02:02 +00:00
this.setPreviewRef = this.setPreviewRef.bind(this)
this.handleSkinChange = this.handleSkinChange.bind(this)
2018-03-15 19:36:07 +00:00
this.handleKeyDown = this.handleKeyDown.bind(this)
2016-05-31 18:39:30 +00:00
}
2016-06-09 00:22:06 +00:00
componentWillReceiveProps(props) {
if (props.skin) {
this.setState({ skin: props.skin })
} else if (props.defaultSkin && !store.get('skin')) {
this.setState({ skin: props.defaultSkin })
}
2016-06-09 00:22:06 +00:00
}
componentDidMount() {
if (this.state.firstRender) {
this.testStickyPosition()
2016-07-15 16:31:22 +00:00
this.firstRenderTimeout = setTimeout(() => {
this.setState({ firstRender: false })
}, 60)
}
}
2016-05-31 18:39:30 +00:00
componentDidUpdate() {
2016-07-08 17:56:29 +00:00
this.updateCategoriesSize()
2016-05-31 18:39:30 +00:00
this.handleScroll()
}
2016-07-15 16:31:22 +00:00
componentWillUnmount() {
this.SEARCH_CATEGORY.emojis = null
2016-07-15 16:31:22 +00:00
clearTimeout(this.leaveTimeout)
clearTimeout(this.firstRenderTimeout)
}
2016-05-31 18:39:30 +00:00
testStickyPosition() {
2017-09-17 08:54:22 +00:00
const stickyTestElement = document.createElement('div')
const prefixes = ['', '-webkit-', '-ms-', '-moz-', '-o-']
2017-10-07 04:02:02 +00:00
prefixes.forEach(
2018-03-27 18:51:26 +00:00
(prefix) => (stickyTestElement.style.position = `${prefix}sticky`),
2017-10-07 04:02:02 +00:00
)
2016-05-31 16:35:08 +00:00
this.hasStickyPosition = !!stickyTestElement.style.position.length
}
2016-05-31 14:36:52 +00:00
handleEmojiOver(emoji) {
var { preview } = this
2017-10-07 04:02:02 +00:00
if (!preview) {
return
}
2017-06-23 09:32:26 +00:00
// Use Array.prototype.find() when it is more widely supported.
const emojiData = this.CUSTOM_CATEGORY.emojis.filter(
2018-03-27 18:51:26 +00:00
(customEmoji) => customEmoji.id === emoji.id,
2017-10-07 04:02:02 +00:00
)[0]
2017-09-17 08:54:22 +00:00
for (let key in emojiData) {
if (emojiData.hasOwnProperty(key)) {
2017-10-07 04:02:02 +00:00
emoji[key] = emojiData[key]
2017-09-17 08:54:22 +00:00
}
}
2017-09-17 08:54:22 +00:00
preview.setState({ emoji })
clearTimeout(this.leaveTimeout)
}
handleEmojiLeave(emoji) {
var { preview } = this
2017-10-07 04:02:02 +00:00
if (!preview) {
return
}
this.leaveTimeout = setTimeout(() => {
preview.setState({ emoji: null })
}, 16)
2016-05-31 14:36:52 +00:00
}
2016-07-21 19:10:33 +00:00
handleEmojiClick(emoji, e) {
this.props.onClick(emoji, e)
2018-03-15 19:36:07 +00:00
this.handleEmojiSelect(emoji)
}
handleEmojiSelect(emoji) {
this.props.onSelect(emoji)
if (!this.hideRecent && !this.props.recent) frequently.add(emoji)
2016-07-07 18:22:02 +00:00
var component = this.categoryRefs['category-1']
2016-07-08 17:56:29 +00:00
if (component) {
2016-07-07 18:22:02 +00:00
let maxMargin = component.maxMargin
component.forceUpdate()
window.requestAnimationFrame(() => {
if (!this.scroll) return
2016-07-08 17:56:29 +00:00
component.memoizeSize()
2016-07-07 18:22:02 +00:00
if (maxMargin == component.maxMargin) return
2016-07-08 17:56:29 +00:00
this.updateCategoriesSize()
2016-07-07 18:22:02 +00:00
this.handleScrollPaint()
if (this.SEARCH_CATEGORY.emojis) {
component.updateDisplay('none')
}
2016-07-07 18:22:02 +00:00
})
}
}
2016-05-31 16:35:08 +00:00
handleScroll() {
2016-06-02 15:26:48 +00:00
if (!this.waitingForPaint) {
this.waitingForPaint = true
window.requestAnimationFrame(this.handleScrollPaint)
2016-06-02 15:26:48 +00:00
}
}
handleScrollPaint() {
this.waitingForPaint = false
if (!this.scroll) {
return
}
2017-05-27 17:36:41 +00:00
let activeCategory = null
if (this.SEARCH_CATEGORY.emojis) {
activeCategory = this.SEARCH_CATEGORY
2017-05-27 17:36:41 +00:00
} else {
var target = this.scroll,
2017-10-07 04:02:02 +00:00
scrollTop = target.scrollTop,
scrollingDown = scrollTop > (this.scrollTop || 0),
minTop = 0
2017-05-27 17:36:41 +00:00
for (let i = 0, l = this.categories.length; i < l; i++) {
2017-10-07 04:02:02 +00:00
let ii = scrollingDown ? this.categories.length - 1 - i : i,
category = this.categories[ii],
component = this.categoryRefs[`category-${ii}`]
2017-05-27 17:36:41 +00:00
if (component) {
let active = component.handleScroll(scrollTop)
if (!minTop || component.top < minTop) {
if (component.top > 0) {
minTop = component.top
}
}
2017-05-27 17:36:41 +00:00
if (active && !activeCategory) {
activeCategory = category
}
2016-05-31 16:35:08 +00:00
}
2016-06-02 15:26:48 +00:00
}
2017-05-27 17:36:41 +00:00
if (scrollTop < minTop) {
2017-10-07 04:02:02 +00:00
activeCategory = this.categories.filter(
2018-03-27 18:51:26 +00:00
(category) => !(category.anchor === false),
2017-10-07 04:02:02 +00:00
)[0]
2017-05-27 17:36:41 +00:00
} else if (scrollTop + this.clientHeight >= this.scrollHeight) {
activeCategory = this.categories[this.categories.length - 1]
}
}
2016-06-02 15:26:48 +00:00
if (activeCategory) {
let { anchors } = this,
2017-10-07 04:02:02 +00:00
{ name: categoryName } = activeCategory
2016-06-02 15:26:48 +00:00
if (anchors.state.selected != categoryName) {
anchors.setState({ selected: categoryName })
}
2016-05-31 16:35:08 +00:00
}
2016-06-02 15:26:48 +00:00
this.scrollTop = scrollTop
2016-05-31 16:35:08 +00:00
}
2016-05-31 18:39:30 +00:00
handleSearch(emojis) {
this.SEARCH_CATEGORY.emojis = emojis
2016-07-08 17:56:29 +00:00
for (let i = 0, l = this.categories.length; i < l; i++) {
let component = this.categoryRefs[`category-${i}`]
2016-07-08 17:56:29 +00:00
if (component && component.props.name != 'Search') {
let display = emojis ? 'none' : 'inherit'
component.updateDisplay(display)
2016-07-08 17:56:29 +00:00
}
2016-05-31 18:39:30 +00:00
}
2016-07-08 17:56:29 +00:00
this.forceUpdate()
this.scroll.scrollTop = 0
this.handleScroll()
2016-05-31 18:39:30 +00:00
}
2016-06-02 18:58:19 +00:00
handleAnchorClick(category, i) {
var component = this.categoryRefs[`category-${i}`],
2017-10-07 04:02:02 +00:00
{ scroll, anchors } = this,
scrollToComponent = null
2016-06-02 18:58:19 +00:00
scrollToComponent = () => {
if (component) {
let { top } = component
if (category.first) {
top = 0
} else {
top += 1
}
2016-06-02 18:58:19 +00:00
scroll.scrollTop = top
2016-06-02 18:58:19 +00:00
}
}
if (this.SEARCH_CATEGORY.emojis) {
this.handleSearch(null)
this.search.clear()
2016-06-02 18:58:19 +00:00
window.requestAnimationFrame(scrollToComponent)
} else {
scrollToComponent()
2016-06-02 18:58:19 +00:00
}
}
2016-06-09 00:22:06 +00:00
handleSkinChange(skin) {
var newState = { skin: skin },
{ onSkinChange } = this.props
this.setState(newState)
store.update(newState)
onSkinChange(skin)
2016-06-09 00:22:06 +00:00
}
2018-03-15 19:36:07 +00:00
handleKeyDown(e) {
let handled = false
2018-03-15 19:36:07 +00:00
switch (e.keyCode) {
case 13:
let emoji
2018-03-27 18:51:26 +00:00
if (
this.SEARCH_CATEGORY.emojis &&
(emoji = this.SEARCH_CATEGORY.emojis[0])
) {
this.handleEmojiSelect(emoji)
2018-03-15 19:36:07 +00:00
}
handled = true
2018-03-27 18:51:26 +00:00
break
2018-03-15 19:36:07 +00:00
}
2018-03-15 19:36:07 +00:00
if (handled) {
e.preventDefault()
}
}
2016-07-08 17:56:29 +00:00
updateCategoriesSize() {
for (let i = 0, l = this.categories.length; i < l; i++) {
let component = this.categoryRefs[`category-${i}`]
2016-07-08 17:56:29 +00:00
if (component) component.memoizeSize()
}
if (this.scroll) {
let target = this.scroll
this.scrollHeight = target.scrollHeight
this.clientHeight = target.clientHeight
}
2016-07-08 17:56:29 +00:00
}
getCategories() {
2017-10-07 04:02:02 +00:00
return this.state.firstRender
? this.categories.slice(0, 3)
: this.categories
}
setAnchorsRef(c) {
this.anchors = c
}
setSearchRef(c) {
this.search = c
}
setPreviewRef(c) {
this.preview = c
}
setScrollRef(c) {
this.scroll = c
}
setCategoryRef(name, c) {
if (!this.categoryRefs) {
this.categoryRefs = {}
}
this.categoryRefs[name] = c
}
2016-05-31 14:36:52 +00:00
render() {
2017-10-07 04:02:02 +00:00
var {
perLine,
emojiSize,
set,
sheetSize,
style,
title,
emoji,
color,
native,
backgroundImageFn,
emojisToShowFilter,
showPreview,
showSkinTones,
2017-10-07 04:02:02 +00:00
emojiTooltip,
include,
exclude,
recent,
2017-10-07 04:02:02 +00:00
autoFocus,
} = this.props,
{ skin } = this.state,
width = perLine * (emojiSize + 12) + 12 + 2 + measureScrollbar()
return (
2018-03-27 18:51:26 +00:00
<div
style={{ width: width, ...style }}
className="emoji-mart"
onKeyDown={this.handleKeyDown}
>
2017-10-07 04:02:02 +00:00
<div className="emoji-mart-bar">
<Anchors
ref={this.setAnchorsRef}
2016-10-27 03:22:59 +00:00
i18n={this.i18n}
2017-10-07 04:02:02 +00:00
color={color}
categories={this.categories}
onAnchorClick={this.handleAnchorClick}
2016-05-31 14:36:52 +00:00
/>
2017-10-07 04:02:02 +00:00
</div>
2016-05-31 14:36:52 +00:00
2017-10-07 04:02:02 +00:00
<Search
ref={this.setSearchRef}
onSearch={this.handleSearch}
i18n={this.i18n}
emojisToShowFilter={emojisToShowFilter}
include={include}
exclude={exclude}
custom={this.CUSTOM_CATEGORY.emojis}
2017-10-07 04:02:02 +00:00
autoFocus={autoFocus}
2016-05-31 14:36:52 +00:00
/>
2017-10-07 04:02:02 +00:00
<div
ref={this.setScrollRef}
className="emoji-mart-scroll"
onScroll={this.handleScroll}
>
{this.getCategories().map((category, i) => {
return (
<Category
ref={this.setCategoryRef.bind(this, `category-${i}`)}
key={category.name}
id={category.id}
2017-10-07 04:02:02 +00:00
name={category.name}
emojis={category.emojis}
perLine={perLine}
native={native}
hasStickyPosition={this.hasStickyPosition}
i18n={this.i18n}
2018-03-02 20:10:04 +00:00
recent={
category.id == this.RECENT_CATEGORY.id ? recent : undefined
}
2017-10-07 04:02:02 +00:00
custom={
category.id == this.RECENT_CATEGORY.id
? this.CUSTOM_CATEGORY.emojis
2017-12-15 20:31:49 +00:00
: undefined
2017-10-07 04:02:02 +00:00
}
emojiProps={{
native: native,
skin: skin,
size: emojiSize,
set: set,
sheetSize: sheetSize,
forceSize: native,
tooltip: emojiTooltip,
backgroundImageFn: backgroundImageFn,
onOver: this.handleEmojiOver,
onLeave: this.handleEmojiLeave,
onClick: this.handleEmojiClick,
}}
/>
)
})}
</div>
{showPreview && (
<div className="emoji-mart-bar">
<Preview
ref={this.setPreviewRef}
title={title}
emoji={emoji}
showSkinTones={showSkinTones}
2017-10-07 04:02:02 +00:00
emojiProps={{
native: native,
size: 38,
skin: skin,
set: set,
sheetSize: sheetSize,
backgroundImageFn: backgroundImageFn,
}}
skinsProps={{
skin: skin,
onChange: this.handleSkinChange,
}}
/>
</div>
)}
</div>
)
2016-05-31 14:36:52 +00:00
}
}
Picker.propTypes = {
onClick: PropTypes.func,
2018-03-15 19:36:07 +00:00
onSelect: PropTypes.func,
onSkinChange: PropTypes.func,
perLine: PropTypes.number,
emojiSize: PropTypes.number,
i18n: PropTypes.object,
style: PropTypes.object,
title: PropTypes.string,
emoji: PropTypes.string,
color: PropTypes.string,
2016-10-27 00:55:44 +00:00
set: Emoji.propTypes.set,
2016-10-27 01:04:05 +00:00
skin: Emoji.propTypes.skin,
native: PropTypes.bool,
backgroundImageFn: Emoji.propTypes.backgroundImageFn,
sheetSize: Emoji.propTypes.sheetSize,
emojisToShowFilter: PropTypes.func,
showPreview: PropTypes.bool,
showSkinTones: PropTypes.bool,
emojiTooltip: Emoji.propTypes.tooltip,
include: PropTypes.arrayOf(PropTypes.string),
exclude: PropTypes.arrayOf(PropTypes.string),
recent: PropTypes.arrayOf(PropTypes.string),
autoFocus: PropTypes.bool,
2017-10-07 04:02:02 +00:00
custom: PropTypes.arrayOf(
PropTypes.shape({
name: PropTypes.string.isRequired,
short_names: PropTypes.arrayOf(PropTypes.string).isRequired,
emoticons: PropTypes.arrayOf(PropTypes.string),
keywords: PropTypes.arrayOf(PropTypes.string),
imageUrl: PropTypes.string.isRequired,
2018-03-27 18:51:26 +00:00
}),
2017-10-07 04:02:02 +00:00
),
2016-05-31 14:36:52 +00:00
}
Picker.defaultProps = {
2017-10-07 04:02:02 +00:00
onClick: () => {},
2018-03-15 19:36:07 +00:00
onSelect: () => {},
onSkinChange: () => {},
2016-05-31 21:23:51 +00:00
emojiSize: 24,
2016-05-31 14:36:52 +00:00
perLine: 9,
2016-10-27 03:22:59 +00:00
i18n: {},
2016-07-11 18:08:41 +00:00
style: {},
2016-07-27 15:35:12 +00:00
title: 'Emoji Mart™',
emoji: 'department_store',
2016-07-21 19:33:18 +00:00
color: '#ae65c5',
2016-10-27 00:55:44 +00:00
set: Emoji.defaultProps.set,
skin: null,
defaultSkin: Emoji.defaultProps.skin,
native: Emoji.defaultProps.native,
sheetSize: Emoji.defaultProps.sheetSize,
backgroundImageFn: Emoji.defaultProps.backgroundImageFn,
emojisToShowFilter: null,
showPreview: true,
showSkinTones: true,
emojiTooltip: Emoji.defaultProps.tooltip,
2017-04-04 15:07:48 +00:00
autoFocus: false,
custom: [],
2016-05-31 14:36:52 +00:00
}