2016-05-31 14:36:52 +00:00
|
|
|
import React from 'react'
|
2016-07-07 18:22:02 +00:00
|
|
|
|
2016-07-20 18:45:28 +00:00
|
|
|
import frequently from '../utils/frequently'
|
2016-10-27 03:27:55 +00:00
|
|
|
import { Emoji } from '.'
|
2016-05-31 14:36:52 +00:00
|
|
|
|
|
|
|
export default class Category extends React.Component {
|
2016-05-31 16:35:08 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.container = this.refs.container
|
|
|
|
this.label = this.refs.label
|
|
|
|
this.parent = this.container.parentNode
|
2016-05-31 14:36:52 +00:00
|
|
|
|
2016-05-31 16:35:08 +00:00
|
|
|
this.margin = 0
|
|
|
|
this.minMargin = 0
|
|
|
|
|
|
|
|
this.memoizeSize()
|
|
|
|
}
|
2016-05-31 14:36:52 +00:00
|
|
|
|
2016-07-08 17:56:29 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2017-02-17 15:48:57 +00:00
|
|
|
var { name, perLine, native, hasStickyPosition, emojis, emojiProps } = this.props,
|
2016-12-01 01:31:02 +00:00
|
|
|
{ skin, size, set } = emojiProps,
|
2017-02-17 15:48:57 +00:00
|
|
|
{ perLine: nextPerLine, native: nextNative, hasStickyPosition: nextHasStickyPosition, emojis: nextEmojis, emojiProps: nextEmojiProps } = nextProps,
|
2016-12-01 01:31:02 +00:00
|
|
|
{ skin: nextSkin, size: nextSize, set: nextSet } = nextEmojiProps,
|
2016-07-08 17:56:29 +00:00
|
|
|
shouldUpdate = false
|
|
|
|
|
|
|
|
if (name == 'Recent' && perLine != nextPerLine) {
|
|
|
|
shouldUpdate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == 'Search') {
|
|
|
|
shouldUpdate = !(emojis == nextEmojis)
|
|
|
|
}
|
|
|
|
|
2017-02-17 15:48:57 +00:00
|
|
|
if (skin != nextSkin || size != nextSize || native != nextNative || set != nextSet || hasStickyPosition != nextHasStickyPosition) {
|
2016-07-08 17:56:29 +00:00
|
|
|
shouldUpdate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return shouldUpdate
|
2016-05-31 16:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memoizeSize() {
|
|
|
|
var { top, height } = this.container.getBoundingClientRect()
|
|
|
|
var { top: parentTop } = this.parent.getBoundingClientRect()
|
|
|
|
var { height: labelHeight } = this.label.getBoundingClientRect()
|
|
|
|
|
|
|
|
this.top = top - parentTop + this.parent.scrollTop
|
2016-07-08 21:03:41 +00:00
|
|
|
|
2016-07-08 20:28:43 +00:00
|
|
|
if (height == 0) {
|
|
|
|
this.maxMargin = 0
|
2016-06-02 15:26:48 +00:00
|
|
|
} else {
|
2016-07-08 21:03:41 +00:00
|
|
|
this.maxMargin = height - labelHeight
|
2016-06-02 15:26:48 +00:00
|
|
|
}
|
2016-05-31 16:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleScroll(scrollTop) {
|
|
|
|
var margin = scrollTop - this.top
|
|
|
|
margin = margin < this.minMargin ? this.minMargin : margin
|
|
|
|
margin = margin > this.maxMargin ? this.maxMargin : margin
|
|
|
|
|
|
|
|
if (margin == this.margin) return
|
2016-06-02 15:26:48 +00:00
|
|
|
var { name } = this.props
|
|
|
|
|
|
|
|
if (!this.props.hasStickyPosition) {
|
|
|
|
this.label.style.top = `${margin}px`
|
|
|
|
}
|
|
|
|
|
2016-05-31 16:35:08 +00:00
|
|
|
this.margin = margin
|
2016-06-02 15:26:48 +00:00
|
|
|
return true
|
2016-05-31 14:36:52 +00:00
|
|
|
}
|
|
|
|
|
2016-07-08 21:03:41 +00:00
|
|
|
getEmojis() {
|
|
|
|
var { name, emojis, perLine } = this.props
|
2016-05-31 14:36:52 +00:00
|
|
|
|
2016-07-07 18:22:02 +00:00
|
|
|
if (name == 'Recent') {
|
|
|
|
let frequentlyUsed = frequently.get(perLine * 4)
|
2016-05-31 16:35:08 +00:00
|
|
|
|
2016-07-07 18:22:02 +00:00
|
|
|
if (frequentlyUsed.length) {
|
|
|
|
emojis = frequentlyUsed
|
2016-05-31 16:35:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 18:22:02 +00:00
|
|
|
if (emojis) {
|
|
|
|
emojis = emojis.slice(0)
|
2016-07-08 21:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return emojis
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDisplay(display) {
|
|
|
|
var emojis = this.getEmojis()
|
|
|
|
|
2017-02-16 17:07:54 +00:00
|
|
|
if (!emojis) {
|
2016-07-08 21:03:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.container.style.display = display
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-10-27 03:22:59 +00:00
|
|
|
var { name, hasStickyPosition, emojiProps, i18n } = this.props,
|
2016-07-08 21:03:41 +00:00
|
|
|
emojis = this.getEmojis(),
|
|
|
|
labelStyles = {},
|
|
|
|
labelSpanStyles = {},
|
|
|
|
containerStyles = {}
|
|
|
|
|
|
|
|
if (!emojis) {
|
2016-06-02 15:26:48 +00:00
|
|
|
containerStyles = {
|
2016-07-08 21:03:41 +00:00
|
|
|
display: 'none',
|
2016-06-02 15:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 18:22:02 +00:00
|
|
|
if (!hasStickyPosition) {
|
|
|
|
labelStyles = {
|
|
|
|
height: 28,
|
|
|
|
}
|
|
|
|
|
|
|
|
labelSpanStyles = {
|
|
|
|
position: 'absolute',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 15:20:17 +00:00
|
|
|
return <div ref='container' className={`emoji-mart-category ${emojis && !emojis.length ? 'emoji-mart-no-results' : ''}`} style={containerStyles}>
|
2016-07-27 15:35:12 +00:00
|
|
|
<div style={labelStyles} data-name={name} className='emoji-mart-category-label'>
|
2016-10-27 03:22:59 +00:00
|
|
|
<span style={labelSpanStyles} ref='label'>{i18n.categories[name.toLowerCase()]}</span>
|
2016-05-31 16:35:08 +00:00
|
|
|
</div>
|
|
|
|
|
2016-06-02 15:26:48 +00:00
|
|
|
{emojis && emojis.map((emoji) =>
|
2016-05-31 18:48:15 +00:00
|
|
|
<Emoji
|
2016-07-19 16:27:24 +00:00
|
|
|
key={emoji.id || emoji}
|
2016-05-31 18:48:15 +00:00
|
|
|
emoji={emoji}
|
|
|
|
{...emojiProps}
|
|
|
|
/>
|
2016-05-31 14:36:52 +00:00
|
|
|
)}
|
2016-06-01 15:38:11 +00:00
|
|
|
|
2016-06-02 15:26:48 +00:00
|
|
|
{emojis && !emojis.length &&
|
2017-04-18 15:20:17 +00:00
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
<Emoji
|
|
|
|
{...emojiProps}
|
|
|
|
size={38}
|
|
|
|
emoji='sleuth_or_spy'
|
|
|
|
onOver={null}
|
|
|
|
onLeave={null}
|
|
|
|
onClick={null}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='emoji-mart-no-results-label'>
|
2017-04-18 13:50:38 +00:00
|
|
|
{i18n.notfound}
|
2017-04-18 15:20:17 +00:00
|
|
|
</div>
|
2016-06-01 15:38:11 +00:00
|
|
|
</div>
|
|
|
|
}
|
2016-05-31 14:36:52 +00:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Category.propTypes = {
|
|
|
|
emojis: React.PropTypes.array,
|
2016-05-31 16:35:08 +00:00
|
|
|
hasStickyPosition: React.PropTypes.bool,
|
2016-05-31 14:36:52 +00:00
|
|
|
name: React.PropTypes.string.isRequired,
|
2017-02-17 15:48:57 +00:00
|
|
|
native: React.PropTypes.bool.isRequired,
|
2016-07-07 18:22:02 +00:00
|
|
|
perLine: React.PropTypes.number.isRequired,
|
2016-05-31 14:36:52 +00:00
|
|
|
emojiProps: React.PropTypes.object.isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
Category.defaultProps = {
|
|
|
|
emojis: [],
|
2016-05-31 16:35:08 +00:00
|
|
|
hasStickyPosition: true,
|
2016-05-31 14:36:52 +00:00
|
|
|
}
|