searchbar.js 1.6 KB

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