/
banner-search.jsx
53 lines (46 loc) · 1.61 KB
/
banner-search.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React from 'react'
import PropTypes from 'prop-types'
import './banner-search-style'
export default class BannerSearch extends React.Component {
static propTypes = {
blockName: PropTypes.string.isRequired,
active: PropTypes.bool.isRequired,
placeholder: PropTypes.string,
icons: PropTypes.shape({
clear: PropTypes.node.isRequired,
search: PropTypes.node.isRequired
}),
onToggle: PropTypes.func.isRequired
}
static defaultProps = {
placeholder: 'Search this site...'
}
render() {
let { blockName, active, placeholder } = this.props,
activeMod = active ? `${blockName}--active` : ''
return (
<div className={ `${blockName} ${activeMod}` }>
<input
ref={ ref => this._input = ref }
className={ `${blockName}__input` }
type="text"
placeholder={ placeholder } />
<button
className={ `${blockName}__icon ${blockName}__clear` }
onClick={ this.props.onToggle }>
{ this.props.icons.clear }
</button>
<button
className={ `${blockName}__icon ${blockName}__search` }
onClick={ this.props.onToggle }>
{ this.props.icons.search }
</button>
</div>
)
}
componentDidUpdate(prevProps, prevState) {
if ( !prevProps.active && this.props.active ) {
this._input.focus()
}
}
}