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

125 lines
2.8 KiB
JavaScript

import React from 'react'
import PropTypes from 'prop-types'
import { search as icons } from '../svgs'
import NimbleEmojiIndex from '../utils/emoji-index/nimble-emoji-index'
let id = 0
export default class Search extends React.PureComponent {
constructor(props) {
super(props)
this.state = {
icon: icons.search,
isSearching: false,
id: ++id
}
this.data = props.data
this.emojiIndex = new NimbleEmojiIndex(this.data)
this.setRef = this.setRef.bind(this)
this.handleChange = this.handleChange.bind(this)
this.clear = this.clear.bind(this)
this.handleKeyUp = this.handleKeyUp.bind(this)
}
search(value) {
if (value == '')
this.setState({
icon: icons.search,
isSearching: false,
})
else
this.setState({
icon: icons.delete,
isSearching: true,
})
this.props.onSearch(
this.emojiIndex.search(value, {
emojisToShowFilter: this.props.emojisToShowFilter,
maxResults: this.props.maxResults,
include: this.props.include,
exclude: this.props.exclude,
custom: this.props.custom,
}),
)
}
clear() {
if (this.input.value == '') return
this.input.value = ''
this.search('')
}
handleChange() {
this.search(this.input.value)
}
handleKeyUp(e) {
if (e.keyCode === 13) {
this.clear()
}
}
clearIfNotSearching () {
const { isSearching } = this.state
if (!isSearching) {
this.clear()
}
}
setRef(c) {
this.input = c
}
render() {
const { i18n, autoFocus } = this.props
const { icon, id } = this.state
const inputId = `emoji-mart-search-${id}`
return (
<div className="emoji-mart-search">
<input
id={inputId}
ref={this.setRef}
type="search"
onChange={this.handleChange}
placeholder={i18n.search}
autoFocus={autoFocus}
/>
{/*
* Use a <label> in addition to the placeholder for accessibility, but place it off-screen
* http://www.maxability.co.in/2016/01/placeholder-attribute-and-why-it-is-not-accessible/
*/}
<label
className="emoji-mart-sr-only"
htmlFor={inputId}
>{i18n.search}</label>
<button
className="emoji-mart-search-icon"
onClick={this.clearIfNotSearching}
onKeyUp={this.handleKeyUp}
aria-label={i18n.clear}
>
{icon()}
</button>
</div>
)
}
}
Search.propTypes /* remove-proptypes */ = {
onSearch: PropTypes.func,
maxResults: PropTypes.number,
emojisToShowFilter: PropTypes.func,
autoFocus: PropTypes.bool,
}
Search.defaultProps = {
onSearch: () => {},
maxResults: 75,
emojisToShowFilter: null,
autoFocus: false,
}