Functional Emoji component

nolan/hinaloe-test
Etienne Lemay 2017-04-23 17:46:00 +02:00
parent 2ace579c7d
commit d2e37cad6d
1 changed files with 74 additions and 84 deletions

View File

@ -6,102 +6,90 @@ import { getData, getSanitizedData, unifiedToNative } from '../utils'
const SHEET_COLUMNS = 49 const SHEET_COLUMNS = 49
export default class Emoji extends React.Component { const _getPosition = (props) => {
constructor(props) { var { sheet_x, sheet_y } = _getData(props),
super(props)
this.hasSkinVariations = !!this.getData().skin_variations
}
shouldComponentUpdate(nextProps) {
return (
this.hasSkinVariations && nextProps.skin != this.props.skin ||
nextProps.size != this.props.size ||
nextProps.native != this.props.native ||
nextProps.set != this.props.set ||
nextProps.emoji != this.props.emoji
)
}
getPosition() {
var { sheet_x, sheet_y } = this.getData(),
multiply = 100 / (SHEET_COLUMNS - 1) multiply = 100 / (SHEET_COLUMNS - 1)
return `${multiply * sheet_x}% ${multiply * sheet_y}%` return `${multiply * sheet_x}% ${multiply * sheet_y}%`
} }
getData() { const _getData = (props) => {
var { emoji, skin, set } = this.props var { emoji, skin, set } = props
return getData(emoji, skin, set) return getData(emoji, skin, set)
} }
getSanitizedData() { const _getSanitizedData = (props) => {
var { emoji, skin, set } = this.props var { emoji, skin, set } = props
return getSanitizedData(emoji, skin, set) return getSanitizedData(emoji, skin, set)
} }
handleClick(e) { const _handleClick = (e, props) => {
if (!this.props.onClick) { return } if (!props.onClick) { return }
var { onClick } = this.props, var { onClick } = props,
emoji = this.getSanitizedData() emoji = _getSanitizedData(props)
onClick(emoji, e) onClick(emoji, e)
} }
handleOver(e) { const _handleOver = (e, props) => {
if (!this.props.onOver) { return } if (!props.onOver) { return }
var { onOver } = this.props, var { onOver } = props,
emoji = this.getSanitizedData() emoji = _getSanitizedData(props)
onOver(emoji, e) onOver(emoji, e)
} }
handleLeave(e) { const _handleLeave = (e, props) => {
if (!this.props.onLeave) { return } if (!props.onLeave) { return }
var { onLeave } = this.props, var { onLeave } = props,
emoji = this.getSanitizedData() emoji = _getSanitizedData(props)
onLeave(emoji, e) onLeave(emoji, e)
}
const Emoji = (props) => {
for (let k in Emoji.defaultProps) {
if (props[k] == undefined && Emoji.defaultProps[k] != undefined) {
props[k] = Emoji.defaultProps[k]
}
} }
render() { var { unified } = _getData(props),
var { set, size, sheetSize, native, forceSize, onOver, onLeave, backgroundImageFn } = this.props,
{ unified } = this.getData(),
style = {}, style = {},
children = this.props.children children = props.children
if (!unified) { if (!unified) {
return null return null
} }
if (native && unified) { if (props.native && unified) {
style = { fontSize: size } style = { fontSize: props.size }
children = unifiedToNative(unified) children = unifiedToNative(unified)
if (forceSize) { if (props.forceSize) {
style.display = 'inline-block' style.display = 'inline-block'
style.width = size style.width = props.size
style.height = size style.height = props.size
} }
} else { } else {
style = { style = {
width: size, width: props.size,
height: size, height: props.size,
display: 'inline-block', display: 'inline-block',
backgroundImage: `url(${backgroundImageFn(set, sheetSize)})`, backgroundImage: `url(${props.backgroundImageFn(props.set, props.sheetSize)})`,
backgroundSize: `${100 * SHEET_COLUMNS}%`, backgroundSize: `${100 * SHEET_COLUMNS}%`,
backgroundPosition: this.getPosition(), backgroundPosition: _getPosition(props),
} }
} }
return <span return <span
onClick={this.handleClick.bind(this)} key={props.emoji.id || props.emoji}
onMouseEnter={this.handleOver.bind(this)} onClick={(e) => _handleClick(e, props)}
onMouseLeave={this.handleLeave.bind(this)} onMouseEnter={(e) => _handleOver(e, props)}
onMouseLeave={(e) => _handleLeave(e, props)}
className='emoji-mart-emoji'> className='emoji-mart-emoji'>
<span style={style}>{children}</span> <span style={style}>{children}</span>
</span> </span>
}
} }
Emoji.propTypes = { Emoji.propTypes = {
@ -132,3 +120,5 @@ Emoji.defaultProps = {
onLeave: (() => {}), onLeave: (() => {}),
onClick: (() => {}), onClick: (() => {}),
} }
export default Emoji