Skip to content

Commit

Permalink
Functional Emoji component
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneLem committed May 27, 2017
1 parent 2ace579 commit d2e37ca
Showing 1 changed file with 67 additions and 77 deletions.
144 changes: 67 additions & 77 deletions src/components/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,102 +6,90 @@ import { getData, getSanitizedData, unifiedToNative } from '../utils'

const SHEET_COLUMNS = 49

export default class Emoji extends React.Component {
constructor(props) {
super(props)
const _getPosition = (props) => {
var { sheet_x, sheet_y } = _getData(props),
multiply = 100 / (SHEET_COLUMNS - 1)

this.hasSkinVariations = !!this.getData().skin_variations
}
return `${multiply * sheet_x}% ${multiply * sheet_y}%`
}

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
)
}
const _getData = (props) => {
var { emoji, skin, set } = props
return getData(emoji, skin, set)
}

getPosition() {
var { sheet_x, sheet_y } = this.getData(),
multiply = 100 / (SHEET_COLUMNS - 1)
const _getSanitizedData = (props) => {
var { emoji, skin, set } = props
return getSanitizedData(emoji, skin, set)
}

return `${multiply * sheet_x}% ${multiply * sheet_y}%`
}
const _handleClick = (e, props) => {
if (!props.onClick) { return }
var { onClick } = props,
emoji = _getSanitizedData(props)

getData() {
var { emoji, skin, set } = this.props
return getData(emoji, skin, set)
}
onClick(emoji, e)
}

getSanitizedData() {
var { emoji, skin, set } = this.props
return getSanitizedData(emoji, skin, set)
}
const _handleOver = (e, props) => {
if (!props.onOver) { return }
var { onOver } = props,
emoji = _getSanitizedData(props)

handleClick(e) {
if (!this.props.onClick) { return }
var { onClick } = this.props,
emoji = this.getSanitizedData()
onOver(emoji, e)
}

onClick(emoji, e)
}
const _handleLeave = (e, props) => {
if (!props.onLeave) { return }
var { onLeave } = props,
emoji = _getSanitizedData(props)

handleOver(e) {
if (!this.props.onOver) { return }
var { onOver } = this.props,
emoji = this.getSanitizedData()
onLeave(emoji, e)
}

onOver(emoji, e)
const Emoji = (props) => {
for (let k in Emoji.defaultProps) {
if (props[k] == undefined && Emoji.defaultProps[k] != undefined) {
props[k] = Emoji.defaultProps[k]
}
}

handleLeave(e) {
if (!this.props.onLeave) { return }
var { onLeave } = this.props,
emoji = this.getSanitizedData()
var { unified } = _getData(props),
style = {},
children = props.children

onLeave(emoji, e)
if (!unified) {
return null
}

render() {
var { set, size, sheetSize, native, forceSize, onOver, onLeave, backgroundImageFn } = this.props,
{ unified } = this.getData(),
style = {},
children = this.props.children
if (props.native && unified) {
style = { fontSize: props.size }
children = unifiedToNative(unified)

if (!unified) {
return null
if (props.forceSize) {
style.display = 'inline-block'
style.width = props.size
style.height = props.size
}

if (native && unified) {
style = { fontSize: size }
children = unifiedToNative(unified)

if (forceSize) {
style.display = 'inline-block'
style.width = size
style.height = size
}
} else {
style = {
width: size,
height: size,
display: 'inline-block',
backgroundImage: `url(${backgroundImageFn(set, sheetSize)})`,
backgroundSize: `${100 * SHEET_COLUMNS}%`,
backgroundPosition: this.getPosition(),
}
} else {
style = {
width: props.size,
height: props.size,
display: 'inline-block',
backgroundImage: `url(${props.backgroundImageFn(props.set, props.sheetSize)})`,
backgroundSize: `${100 * SHEET_COLUMNS}%`,
backgroundPosition: _getPosition(props),
}

return <span
onClick={this.handleClick.bind(this)}
onMouseEnter={this.handleOver.bind(this)}
onMouseLeave={this.handleLeave.bind(this)}
className='emoji-mart-emoji'>
<span style={style}>{children}</span>
</span>
}

return <span
key={props.emoji.id || props.emoji}
onClick={(e) => _handleClick(e, props)}
onMouseEnter={(e) => _handleOver(e, props)}
onMouseLeave={(e) => _handleLeave(e, props)}
className='emoji-mart-emoji'>
<span style={style}>{children}</span>
</span>
}

Emoji.propTypes = {
Expand Down Expand Up @@ -132,3 +120,5 @@ Emoji.defaultProps = {
onLeave: (() => {}),
onClick: (() => {}),
}

export default Emoji

0 comments on commit d2e37ca

Please sign in to comment.