Implement search
parent
21bb9eb63e
commit
c3f1c6c661
|
@ -27,7 +27,7 @@
|
||||||
|
|
||||||
.emoji-picker-scroll {
|
.emoji-picker-scroll {
|
||||||
overflow: scroll;
|
overflow: scroll;
|
||||||
max-height: 270px;
|
height: 270px;
|
||||||
padding: 0 6px 6px 6px;
|
padding: 0 6px 6px 6px;
|
||||||
border: solid #d9d9d9;
|
border: solid #d9d9d9;
|
||||||
border-width: 1px 0;
|
border-width: 1px 0;
|
||||||
|
|
|
@ -18,7 +18,8 @@
|
||||||
"url": "https://github.com/missive/emoji-picker/issues"
|
"url": "https://github.com/missive/emoji-picker/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/missive/emoji-picker",
|
"homepage": "https://github.com/missive/emoji-picker",
|
||||||
"dependencies": {},
|
"dependencies": {
|
||||||
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"react": "^0.14.0 || ^15.0.0-0"
|
"react": "^0.14.0 || ^15.0.0-0"
|
||||||
},
|
},
|
||||||
|
@ -29,6 +30,7 @@
|
||||||
"babel-preset-react": "6.5.0",
|
"babel-preset-react": "6.5.0",
|
||||||
"emoji-data": "git://github.com/missive/emoji-data.git#01aeccb3adf6cdfaab6e13892f7688740047ff32",
|
"emoji-data": "git://github.com/missive/emoji-data.git#01aeccb3adf6cdfaab6e13892f7688740047ff32",
|
||||||
"inflection": "1.10.0",
|
"inflection": "1.10.0",
|
||||||
|
"lunr": "0.7.1",
|
||||||
"mkdirp": "0.5.1",
|
"mkdirp": "0.5.1",
|
||||||
"react": "0.14.7",
|
"react": "0.14.7",
|
||||||
"react-dom": "0.14.7",
|
"react-dom": "0.14.7",
|
||||||
|
|
|
@ -3,9 +3,23 @@ import React from 'react'
|
||||||
import data from '../../data'
|
import data from '../../data'
|
||||||
import Preview from './preview'
|
import Preview from './preview'
|
||||||
import Category from './category'
|
import Category from './category'
|
||||||
|
import Search from './search'
|
||||||
|
|
||||||
export default class Picker extends React.Component {
|
export default class Picker extends React.Component {
|
||||||
componentWillMount() {
|
constructor(props) {
|
||||||
|
super(props)
|
||||||
|
this.testStickyPosition()
|
||||||
|
|
||||||
|
this.state = {
|
||||||
|
categories: data.categories,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidUpdate() {
|
||||||
|
this.handleScroll()
|
||||||
|
}
|
||||||
|
|
||||||
|
testStickyPosition() {
|
||||||
var stickyTestElement = document.createElement('div')
|
var stickyTestElement = document.createElement('div')
|
||||||
for (let prefix of ['', '-webkit-', '-ms-', '-moz-', '-o-']) {
|
for (let prefix of ['', '-webkit-', '-ms-', '-moz-', '-o-']) {
|
||||||
stickyTestElement.style.position = `${prefix}sticky`
|
stickyTestElement.style.position = `${prefix}sticky`
|
||||||
|
@ -14,10 +28,6 @@ export default class Picker extends React.Component {
|
||||||
this.hasStickyPosition = !!stickyTestElement.style.position.length
|
this.hasStickyPosition = !!stickyTestElement.style.position.length
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate() {
|
|
||||||
this.handleScroll()
|
|
||||||
}
|
|
||||||
|
|
||||||
handleEmojiOver(emoji) {
|
handleEmojiOver(emoji) {
|
||||||
var { preview } = this.refs
|
var { preview } = this.refs
|
||||||
preview.setState({ emoji: emoji })
|
preview.setState({ emoji: emoji })
|
||||||
|
@ -28,7 +38,7 @@ export default class Picker extends React.Component {
|
||||||
scrollTop = target.scrollTop
|
scrollTop = target.scrollTop
|
||||||
|
|
||||||
if (!this.hasStickyPosition) {
|
if (!this.hasStickyPosition) {
|
||||||
Array(data.categories.length).fill().forEach((_, i) => {
|
Array(this.state.categories.length).fill().forEach((_, i) => {
|
||||||
var category = this.refs[`category-${i}`]
|
var category = this.refs[`category-${i}`]
|
||||||
if (category) {
|
if (category) {
|
||||||
category.handleScroll(scrollTop)
|
category.handleScroll(scrollTop)
|
||||||
|
@ -37,6 +47,17 @@ export default class Picker extends React.Component {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleSearch(emojis) {
|
||||||
|
if (emojis == null) {
|
||||||
|
this.setState({ categories: data.categories })
|
||||||
|
} else {
|
||||||
|
this.setState({ categories: [{
|
||||||
|
name: 'Search Results',
|
||||||
|
emojis: emojis,
|
||||||
|
}]})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
var { perLine, emojiSize, sheetURL } = this.props
|
var { perLine, emojiSize, sheetURL } = this.props
|
||||||
|
|
||||||
|
@ -46,13 +67,11 @@ export default class Picker extends React.Component {
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div ref="scroll" className='emoji-picker-scroll' onScroll={this.handleScroll.bind(this)}>
|
<div ref="scroll" className='emoji-picker-scroll' onScroll={this.handleScroll.bind(this)}>
|
||||||
<input
|
<Search
|
||||||
type='text'
|
onSearch={this.handleSearch.bind(this)}
|
||||||
placeholder='Search'
|
|
||||||
className='emoji-picker-search'
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{data.categories.map((category, i) => {
|
{this.state.categories.map((category, i) => {
|
||||||
if (category.name == 'Skins') return null
|
if (category.name == 'Skins') return null
|
||||||
return <Category
|
return <Category
|
||||||
ref={`category-${i}`}
|
ref={`category-${i}`}
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
import React from 'react'
|
||||||
|
import lunr from 'lunr'
|
||||||
|
|
||||||
|
import data from '../../data'
|
||||||
|
|
||||||
|
export default class Search extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props)
|
||||||
|
this.buildIndex()
|
||||||
|
}
|
||||||
|
|
||||||
|
buildIndex() {
|
||||||
|
this.index = lunr(function() {
|
||||||
|
this.field('short_name', { boost: 2 })
|
||||||
|
this.field('name')
|
||||||
|
|
||||||
|
this.ref('short_name')
|
||||||
|
})
|
||||||
|
|
||||||
|
for (let emoji in data.emojis) {
|
||||||
|
let emojiData = data.emojis[emoji],
|
||||||
|
{ short_name, name } = emojiData
|
||||||
|
|
||||||
|
this.index.add({ short_name, name })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleChange() {
|
||||||
|
var { input } = this.refs,
|
||||||
|
value = input.value,
|
||||||
|
results = null
|
||||||
|
|
||||||
|
if (value.length) {
|
||||||
|
results = this.index.search(value).map((result) =>
|
||||||
|
result.ref
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.props.onSearch(results)
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return <input
|
||||||
|
ref='input'
|
||||||
|
type='text'
|
||||||
|
onChange={this.handleChange.bind(this)}
|
||||||
|
placeholder='Search'
|
||||||
|
className='emoji-picker-search'
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Search.propTypes = {
|
||||||
|
onSearch: React.PropTypes.func,
|
||||||
|
}
|
||||||
|
|
||||||
|
Search.defaultProps = {
|
||||||
|
onSearch: (() => {}),
|
||||||
|
}
|
Loading…
Reference in New Issue