SearchForm.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import SearchTypeahead from './SearchTypeahead';
  4. // SearchTypeahead wrapper
  5. export default class SearchForm extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. searchError: null,
  10. };
  11. this.onSearchError = this.onSearchError.bind(this);
  12. this.onChange = this.onChange.bind(this);
  13. }
  14. componentDidMount() {
  15. }
  16. componentWillUnmount() {
  17. }
  18. onSearchError(err) {
  19. this.setState({
  20. searchError: err,
  21. });
  22. }
  23. onChange(selected) {
  24. const page = selected[0]; // should be single page selected
  25. // navigate to page
  26. if (page != null) {
  27. window.location = page.path;
  28. }
  29. }
  30. getHelpElement() {
  31. const t = this.props.t;
  32. return (
  33. <table className="table m-1 search-help">
  34. <caption className="text-left text-primary p-2 mb-2">
  35. <h5 className="m-1"><i className="icon-magnifier pr-2 mb-2"/>{ t('search_help.title') }</h5>
  36. </caption>
  37. <tbody>
  38. <tr>
  39. <th className="text-right pt-2">
  40. <code>word1</code> <code>word2</code><br></br>
  41. <small>({ t('search_help.and.syntax help') })</small>
  42. </th>
  43. <td><h6 className="m-0 pt-1">{ t('search_help.and.desc', { word1: 'word1', word2: 'word2' }) }</h6></td>
  44. </tr>
  45. <tr>
  46. <th className="text-right pt-2">
  47. <code>"This is GROWI"</code><br></br>
  48. <small>({ t('search_help.phrase.syntax help') })</small>
  49. </th>
  50. <td><h6 className="m-0 pt-1">{ t('search_help.phrase.desc', { phrase: 'This is GROWI' }) }</h6></td>
  51. </tr>
  52. <tr>
  53. <th className="text-right pt-2"><code>-keyword</code></th>
  54. <td><h6 className="m-0 pt-1">{ t('search_help.exclude.desc', { word: 'keyword' }) }</h6></td>
  55. </tr>
  56. <tr>
  57. <th className="text-right pt-2"><code>prefix:/user/</code></th>
  58. <td><h6 className="m-0 pt-1">{ t('search_help.prefix.desc', { path: '/user/' }) }</h6></td>
  59. </tr>
  60. <tr>
  61. <th className="text-right pt-2"><code>-prefix:/user/</code></th>
  62. <td><h6 className="m-0 pt-1">{ t('search_help.exclude_prefix.desc', { path: '/user/' }) }</h6></td>
  63. </tr>
  64. </tbody>
  65. </table>
  66. );
  67. }
  68. render() {
  69. const t = this.props.t;
  70. const emptyLabel = (this.state.searchError !== null)
  71. ? 'Error on searching.'
  72. : t('search.search page bodies');
  73. return (
  74. <SearchTypeahead
  75. crowi={this.props.crowi}
  76. onChange={this.onChange}
  77. onSubmit={this.props.onSubmit}
  78. onInputChange={this.props.onInputChange}
  79. onSearchError={this.onSearchError}
  80. emptyLabel={emptyLabel}
  81. placeholder="Search ..."
  82. promptText={this.getHelpElement()}
  83. keywordOnInit={this.props.keyword}
  84. />
  85. );
  86. }
  87. }
  88. SearchForm.propTypes = {
  89. t: PropTypes.func.isRequired, // i18next
  90. crowi: PropTypes.object.isRequired,
  91. keyword: PropTypes.string,
  92. onSubmit: PropTypes.func.isRequired,
  93. onInputChange: PropTypes.func,
  94. };
  95. SearchForm.defaultProps = {
  96. onInputChange: () => {},
  97. };