SearchForm.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import React from 'react';
  2. import { FormGroup, Button, InputGroup } from 'react-bootstrap';
  3. import SearchTypeahead from '../SearchTypeahead';
  4. import UserPicture from '../User/UserPicture';
  5. import PageListMeta from '../PageList/PageListMeta';
  6. import PagePath from '../PageList/PagePath';
  7. import PropTypes from 'prop-types';
  8. // Header.SearchForm
  9. export default class SearchForm extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.crowi = window.crowi; // FIXME
  13. this.state = {
  14. input: '',
  15. keyword: '',
  16. pages: [],
  17. isLoading: false,
  18. searchError: null,
  19. };
  20. this.onSearchSuccess = this.onSearchSuccess.bind(this);
  21. this.onSearchError = this.onSearchError.bind(this);
  22. this.onInputChange = this.onInputChange.bind(this);
  23. this.onChange = this.onChange.bind(this);
  24. }
  25. componentDidMount() {
  26. }
  27. componentWillUnmount() {
  28. }
  29. onSearchSuccess(res) {
  30. this.setState({
  31. isLoading: false,
  32. keyword: '',
  33. pages: res.data,
  34. });
  35. }
  36. onSearchError(err) {
  37. this.setState({
  38. isLoading: false,
  39. searchError: err,
  40. });
  41. }
  42. onInputChange(text) {
  43. this.setState({input: text});
  44. }
  45. onChange(selected) {
  46. const page = selected[0]; // should be single page selected
  47. // navigate to page
  48. if (page != null) {
  49. window.location = page.path;
  50. }
  51. }
  52. render() {
  53. const emptyLabel = (this.state.searchError !== null)
  54. ? 'Error on searching.'
  55. : 'No matches found on title... Hit [Enter] key so that search on contents.';
  56. return (
  57. <form
  58. action="/_search"
  59. className="search-form form-group input-group search-top-input-group"
  60. >
  61. <FormGroup>
  62. <InputGroup>
  63. <SearchTypeahead
  64. crowi={this.crowi}
  65. onSearchSuccess={this.onSearchSuccess}
  66. onSearchError={this.onSearchError}
  67. onInputChange={this.onInputChange}
  68. onChange={this.onChange}
  69. emptyLabel={emptyLabel}
  70. placeholder="Search ..."
  71. />
  72. <InputGroup.Button>
  73. <Button type="submit">
  74. <i className="search-top-icon fa fa-search"></i>
  75. </Button >
  76. </InputGroup.Button>
  77. </InputGroup>
  78. </FormGroup>
  79. </form>
  80. );
  81. }
  82. }
  83. SearchForm.propTypes = {
  84. };
  85. SearchForm.defaultProps = {
  86. };