SearchForm.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 search-help">
  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. <tbody>
  41. <tr>
  42. <td className="text-right mt-0 pr-2 p-1"><code>keyword</code></td>
  43. <th className="mr-2"><h6 className="pr-2 m-0 pt-1">記事名 or 本文に<samp>"keyword"</samp>を含む</h6></th>
  44. </tr>
  45. <tr>
  46. <td className="text-right mt-0 pr-2 p-1"><code>a b</code></td>
  47. <th><h6 className="m-0 pt-1">文字列<samp>"a"</samp>と<samp>"b"</samp>を含む (スペース区切り)</h6></th>
  48. </tr>
  49. <tr>
  50. <td className="text-right mt-0 pr-2 p-1"><code>-keyword</code></td>
  51. <th><h6 className="m-0 pt-1">文字列<samp>"keyword"</samp>を含まない</h6></th>
  52. </tr>
  53. </tbody>
  54. </table>
  55. );
  56. }
  57. onSubmit(query) {
  58. this.refs.form.submit(query);
  59. }
  60. render() {
  61. const emptyLabel = (this.state.searchError !== null)
  62. ? 'Error on searching.'
  63. : 'No matches found on title... Hit [Enter] key so that search on contents.';
  64. return (
  65. <form
  66. ref='form'
  67. action='/_search'
  68. className='search-form form-group input-group search-input-group'
  69. >
  70. <FormGroup>
  71. <InputGroup>
  72. <SearchTypeahead
  73. crowi={this.crowi}
  74. onChange={this.onChange}
  75. onSubmit={this.onSubmit}
  76. emptyLabel={emptyLabel}
  77. placeholder="Search ..."
  78. promptText={this.getHelpElement()}
  79. />
  80. <InputGroup.Button>
  81. <Button type="submit" bsStyle="link">
  82. <i className="icon-magnifier"></i>
  83. </Button >
  84. </InputGroup.Button>
  85. </InputGroup>
  86. </FormGroup>
  87. </form>
  88. );
  89. }
  90. }
  91. SearchForm.propTypes = {
  92. };
  93. SearchForm.defaultProps = {
  94. };