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

169 lines
4.1 KiB
JavaScript
Raw Normal View History

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) {
var { name, perLine, native, hasStickyPosition, emojis, emojiProps } = this.props,
{ skin, size, set } = emojiProps,
{ perLine: nextPerLine, native: nextNative, hasStickyPosition: nextHasStickyPosition, emojis: nextEmojis, emojiProps: nextEmojiProps } = nextProps,
{ 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)
}
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
if (height == 0) {
this.maxMargin = 0
2016-06-02 15:26:48 +00:00
} else {
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
}
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)
}
return emojis
}
updateDisplay(display) {
var emojis = this.getEmojis()
if (!emojis) {
return
}
this.container.style.display = display
}
render() {
2016-10-27 03:22:59 +00:00
var { name, hasStickyPosition, emojiProps, i18n } = this.props,
emojis = this.getEmojis(),
labelStyles = {},
labelSpanStyles = {},
containerStyles = {}
if (!emojis) {
2016-06-02 15:26:48 +00:00
containerStyles = {
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-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'>
{i18n.notfound}
2017-04-18 15:20:17 +00:00
</div>
</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,
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
}