SearchForm.js 2.9 KB

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