SearchForm.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // Header.SearchForm
  7. export default class SearchForm extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.crowi = window.crowi; // FIXME
  11. this.state = {
  12. searchError: null,
  13. };
  14. this.onSearchError = this.onSearchError.bind(this);
  15. this.onChange = this.onChange.bind(this);
  16. this.onSubmit = this.onSubmit.bind(this);
  17. }
  18. componentDidMount() {
  19. }
  20. componentWillUnmount() {
  21. }
  22. onSearchError(err) {
  23. this.setState({
  24. searchError: err,
  25. });
  26. }
  27. onChange(selected) {
  28. const page = selected[0]; // should be single page selected
  29. // navigate to page
  30. if (page != null) {
  31. window.location = page.path;
  32. }
  33. }
  34. getHelpElement() {
  35. return (
  36. <table className="table m-1">
  37. <caption className="text-left text-primary p-2 mb-2">
  38. <h5 className="m-1"><i className="icon-magnifier pr-2 mb-2"/>Search Help</h5>
  39. </caption>
  40. <tr>
  41. <td className="text-right mt-0 pr-2 p-1"><code>keyword</code></td>
  42. <th className="mr-2"><h6 className="pr-2 m-0 pt-1">記事名 or 本文に<samp>"keyword"</samp>を含む</h6></th>
  43. </tr>
  44. <tr>
  45. <td className="text-right mt-0 pr-2 p-1"><code>a b</code></td>
  46. <th><h6 className="m-0 pt-1">文字列<samp>"a"</samp>と<samp>"b"</samp>を含む (スペース区切り)</h6></th>
  47. </tr>
  48. <tr>
  49. <td className="text-right mt-0 pr-2 p-1"><code>-keyword</code></td>
  50. <th><h6 className="m-0 pt-1">文字列<samp>"keyword"</samp>を含まない</h6></th>
  51. </tr>
  52. </table>
  53. );
  54. }
  55. onSubmit(query) {
  56. this.refs.form.submit(query);
  57. }
  58. render() {
  59. const emptyLabel = (this.state.searchError !== null)
  60. ? 'Error on searching.'
  61. : 'No matches found on title... Hit [Enter] key so that search on contents.';
  62. return (
  63. <form
  64. ref='form'
  65. action='/_search'
  66. className='search-form form-group input-group search-input-group'
  67. >
  68. <FormGroup>
  69. <InputGroup>
  70. <SearchTypeahead
  71. crowi={this.crowi}
  72. onChange={this.onChange}
  73. onSubmit={this.onSubmit}
  74. emptyLabel={emptyLabel}
  75. placeholder="Search ..."
  76. promptText={this.getHelpElement()}
  77. />
  78. <InputGroup.Button>
  79. <Button type="submit" bsStyle="link">
  80. <i className="icon-magnifier"></i>
  81. </Button >
  82. </InputGroup.Button>
  83. </InputGroup>
  84. </FormGroup>
  85. </form>
  86. );
  87. }
  88. }
  89. SearchForm.propTypes = {
  90. };
  91. SearchForm.defaultProps = {
  92. };