HeaderSearchBox.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // This is the root component for #search-top
  2. import React from 'react';
  3. import SearchForm from './HeaderSearchBox/SearchForm';
  4. import SearchSuggest from './HeaderSearchBox/SearchSuggest';
  5. import axios from 'axios'
  6. export default class SearchBox extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.crowi = window.crowi; // FIXME
  10. this.state = {
  11. searchingKeyword: '',
  12. searchedPages: [],
  13. searchError: null,
  14. searching: false,
  15. focused: false,
  16. }
  17. this.search = this.search.bind(this);
  18. }
  19. search(data) {
  20. const keyword = data.keyword;
  21. if (keyword === '') {
  22. this.setState({
  23. searchingKeyword: '',
  24. searchedPages: [],
  25. });
  26. return true;
  27. }
  28. this.setState({
  29. searchingKeyword: keyword,
  30. searching: true,
  31. });
  32. this.crowi.apiGet('/search', {q: keyword})
  33. .then(res => {
  34. this.setState({
  35. searchingKeyword: keyword,
  36. searchedPages: res.data,
  37. searching: false,
  38. searchError: null,
  39. });
  40. }).catch(err => {
  41. this.setState({
  42. searchError: err,
  43. searching: false,
  44. });
  45. });
  46. }
  47. render() {
  48. return (
  49. <div className="search-box">
  50. <SearchForm
  51. onSearchFormChanged={this.search}
  52. searchedPages={this.state.searchedPages}
  53. searchError={this.state.searchError}
  54. />
  55. {/* omit since using react-bootstrap-typeahead in SearchForm
  56. <SearchSuggest
  57. searchingKeyword={this.state.searchingKeyword}
  58. searchedPages={this.state.searchedPages}
  59. searchError={this.state.searchError}
  60. searching={this.state.searching}
  61. focused={this.state.focused}
  62. />
  63. */}
  64. </div>
  65. );
  66. }
  67. }
  68. SearchBox.propTypes = {
  69. //pollInterval: React.PropTypes.number,
  70. };
  71. SearchBox.defaultProps = {
  72. //pollInterval: 1000,
  73. };