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

158 lines
3.8 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
import {frequently} from '../utils'
2016-06-02 17:21:31 +00:00
import {Emoji} from '.'
2016-05-31 14:36:52 +00:00
2016-07-07 18:28:15 +00:00
const LABELS = {
'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',
}
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, emojis, emojiProps } = this.props,
{ skin, size, sheetURL } = emojiProps,
{ perLine: nextPerLine, emojis: nextEmojis, emojiProps: nextEmojiProps } = nextProps,
{ skin: nextSkin, size: nextSize, sheetURL: nextSheetURL } = nextEmojiProps,
shouldUpdate = false
if (name == 'Recent' && perLine != nextPerLine) {
shouldUpdate = true
}
if (name == 'Search') {
shouldUpdate = !(emojis == nextEmojis)
}
if (skin != nextSkin || size != nextSize || sheetURL != nextSheetURL) {
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-06-02 15:26:48 +00:00
if (height > labelHeight) {
this.maxMargin = height - labelHeight
} else {
this.maxMargin = 1
}
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
}
render() {
2016-07-07 18:22:02 +00:00
var { name, emojis, perLine, hasStickyPosition, emojiProps } = this.props,
2016-05-31 16:35:08 +00:00
labelStyles = {},
2016-06-02 15:26:48 +00:00
labelSpanStyles = {},
containerStyles = {}
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)
} else {
2016-06-02 15:26:48 +00:00
containerStyles = {
height: 1,
overflow: 'hidden',
2016-07-08 17:56:29 +00:00
marginBottom: '-1px',
2016-06-02 15:26:48 +00:00
}
}
2016-07-07 18:22:02 +00:00
if (!hasStickyPosition) {
labelStyles = {
height: 28,
}
labelSpanStyles = {
position: 'absolute',
}
}
2016-06-02 15:26:48 +00:00
return <div ref='container' className='emoji-picker-category' style={containerStyles}>
2016-05-31 16:35:08 +00:00
<div style={labelStyles} data-name={name} className='emoji-picker-category-label'>
2016-07-07 18:28:15 +00:00
<span style={labelSpanStyles} ref='label'>{LABELS[name]}</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
key={emoji}
emoji={emoji}
{...emojiProps}
/>
2016-05-31 14:36:52 +00:00
)}
2016-06-02 15:26:48 +00:00
{emojis && !emojis.length &&
<div className='emoji-picker-no-results'>
<Emoji
{...emojiProps}
size={22}
emoji='sleuth_or_spy'
/>
<span className='emoji-picker-no-results-label'>
No emoji found
</span>
</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,
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
}