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

90 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-06-02 15:26:48 +00:00
import React from 'react'
import PropTypes from 'prop-types'
2016-06-02 15:26:48 +00:00
2017-09-29 18:33:08 +00:00
import SVGs from '../svgs'
2016-06-02 15:26:48 +00:00
export default class Anchors extends React.PureComponent {
2016-06-02 15:26:48 +00:00
constructor(props) {
super(props)
2018-03-27 18:51:26 +00:00
let defaultCategory = props.categories.filter(
(category) => category.first,
)[0]
2016-07-08 17:56:29 +00:00
2016-06-02 15:26:48 +00:00
this.state = {
2017-10-07 04:02:02 +00:00
selected: defaultCategory.name,
2016-06-02 15:26:48 +00:00
}
this.handleClick = this.handleClick.bind(this)
}
2017-12-15 20:30:58 +00:00
getSVG(id) {
2017-11-09 06:34:35 +00:00
this.SVGs || (this.SVGs = {})
2017-12-15 20:30:58 +00:00
if (this.SVGs[id]) {
return this.SVGs[id]
2017-11-09 06:34:35 +00:00
} else {
let svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
2017-12-15 20:30:58 +00:00
${SVGs[id]}
2017-11-09 06:34:35 +00:00
</svg>`
2017-12-15 20:30:58 +00:00
this.SVGs[id] = svg
2017-11-09 06:34:35 +00:00
return svg
}
}
2017-10-07 04:02:02 +00:00
handleClick(e) {
var index = e.currentTarget.getAttribute('data-index')
var { categories, onAnchorClick } = this.props
onAnchorClick(categories[index], index)
2016-06-02 15:26:48 +00:00
}
render() {
2016-10-27 03:22:59 +00:00
var { categories, onAnchorClick, color, i18n } = this.props,
2017-10-07 04:02:02 +00:00
{ selected } = this.state
2016-06-02 15:26:48 +00:00
2017-10-07 04:02:02 +00:00
return (
<div className="emoji-mart-anchors">
{categories.map((category, i) => {
2017-12-15 20:30:58 +00:00
var { id, name, anchor } = category,
2017-09-29 18:33:08 +00:00
isSelected = name == selected
2016-07-08 17:56:29 +00:00
2017-10-07 04:02:02 +00:00
if (anchor === false) {
return null
}
2016-06-02 15:26:48 +00:00
2017-10-07 04:02:02 +00:00
return (
<span
2017-12-15 20:30:58 +00:00
key={id}
title={i18n.categories[id]}
2017-10-07 04:02:02 +00:00
data-index={i}
onClick={this.handleClick}
2018-03-27 18:51:26 +00:00
className={`emoji-mart-anchor ${
isSelected ? 'emoji-mart-anchor-selected' : ''
}`}
2017-10-07 04:02:02 +00:00
style={{ color: isSelected ? color : null }}
>
2017-12-15 20:30:58 +00:00
<div dangerouslySetInnerHTML={{ __html: this.getSVG(id) }} />
2017-10-07 04:02:02 +00:00
<span
className="emoji-mart-anchor-bar"
style={{ backgroundColor: color }}
/>
</span>
)
})}
</div>
)
2016-06-02 15:26:48 +00:00
}
}
Anchors.propTypes = {
categories: PropTypes.array,
onAnchorClick: PropTypes.func,
2016-06-02 15:26:48 +00:00
}
Anchors.defaultProps = {
categories: [],
2017-10-07 04:02:02 +00:00
onAnchorClick: () => {},
2016-06-02 15:26:48 +00:00
}