searchbar.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import Agent from '../../../renderer/Agent'
  8. import classnames from 'classnames'
  9. import './searchbar.less'
  10. export default class SearchBar extends React.Component {
  11. constructor (props) {
  12. super(props)
  13. this.state = {
  14. show: false,
  15. keyword: ''
  16. }
  17. this._t = null
  18. Agent.on('search_on', () => {
  19. this.setState({
  20. show: true
  21. }, () => {
  22. setTimeout(() => {
  23. this.refs.keyword.focus()
  24. }, 100)
  25. })
  26. })
  27. Agent.on('search_off', () => {
  28. this.clearSearch()
  29. })
  30. }
  31. clearSearch () {
  32. this.setState({
  33. show: false,
  34. keyword: ''
  35. })
  36. Agent.emit('search', '')
  37. }
  38. doSearch (kw) {
  39. this.setState({
  40. keyword: kw
  41. })
  42. clearTimeout(this._t)
  43. this._t = setTimeout(() => {
  44. Agent.emit('search', kw)
  45. }, 300)
  46. }
  47. onCancel () {
  48. Agent.emit('cancel_search')
  49. }
  50. render () {
  51. if (!this.state.show) {
  52. return null
  53. }
  54. return (
  55. <div id="sh-searchbar">
  56. <input
  57. ref="keyword"
  58. type="text"
  59. placeholder="keyword"
  60. value={this.state.keyword}
  61. onChange={(e) => this.doSearch(e.target.value)}
  62. onKeyDown={(e) => (e.keyCode === 27 && this.onCancel())}
  63. />
  64. </div>
  65. )
  66. }
  67. }