2
0

SearchForm.js 2.5 KB

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