SearchForm.jsx 3.8 KB

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