SearchBox.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // This is the root component for #search-top
  2. import React from 'react';
  3. import SearchForm from './SearchForm';
  4. import SearchSuggest from './SearchSuggest';
  5. import axios from 'axios'
  6. export default class SearchBox extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. searchingKeyword: '',
  11. searchedPages: [],
  12. searchError: null,
  13. }
  14. this.search = this.search.bind(this);
  15. }
  16. search(data) {
  17. const keyword = data.keyword;
  18. if (keyword === '') {
  19. this.setState({
  20. searchedPages: [],
  21. });
  22. return true;
  23. }
  24. axios.get('/_api/search', {params: {q: keyword}})
  25. .then((res) => {
  26. if (res.data.ok) {
  27. this.setState({
  28. searchedPages: res.data.data
  29. });
  30. }
  31. // TODO error
  32. })
  33. .catch((res) => {
  34. // TODO error
  35. });
  36. }
  37. render() {
  38. return (
  39. <div className="search-box">
  40. <SearchForm onSearchFormChanged={this.search} />
  41. <SearchSuggest searchedPages={this.state.searchedPages} />
  42. </div>
  43. );
  44. }
  45. }
  46. SearchBox.propTypes = {
  47. //pollInterval: React.PropTypes.number,
  48. };
  49. SearchBox.defaultProps = {
  50. //pollInterval: 1000,
  51. };