HeaderSearchBox.jsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import SearchForm from './SearchForm';
  5. class HeaderSearchBox extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. text: '',
  10. isScopeChildren: false,
  11. };
  12. this.onInputChange = this.onInputChange.bind(this);
  13. this.onClickAllPages = this.onClickAllPages.bind(this);
  14. this.onClickChildren = this.onClickChildren.bind(this);
  15. this.search = this.search.bind(this);
  16. }
  17. componentDidMount() {
  18. }
  19. componentWillUnmount() {
  20. }
  21. onInputChange(text) {
  22. this.setState({ text });
  23. }
  24. onClickAllPages() {
  25. this.setState({ isScopeChildren: false });
  26. }
  27. onClickChildren() {
  28. this.setState({ isScopeChildren: true });
  29. }
  30. search() {
  31. const url = new URL(window.location.href);
  32. url.pathname = '/_search';
  33. // construct search query
  34. let q = this.state.text;
  35. if (this.state.isScopeChildren) {
  36. q += ` prefix:${window.location.pathname}`;
  37. }
  38. url.searchParams.append('q', q);
  39. window.location.href = url.href;
  40. }
  41. render() {
  42. const t = this.props.t;
  43. const scopeLabel = this.state.isScopeChildren
  44. ? t('header_search_box.label.This tree')
  45. : 'All pages';
  46. return (
  47. <div className="form-group">
  48. <div className="input-group flex-nowrap">
  49. <div className="input-group-prepend">
  50. <button className="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true">
  51. {scopeLabel}
  52. </button>
  53. <div className="dropdown-menu">
  54. <a className="dropdown-item" type="button" onClick={this.onClickAllPages}>All pages</a>
  55. <a className="dropdown-item" type="button" onClick={this.onClickChildren}>{ t('header_search_box.item_label.This tree') }</a>
  56. </div>
  57. </div>
  58. <SearchForm
  59. t={this.props.t}
  60. crowi={this.props.crowi}
  61. onInputChange={this.onInputChange}
  62. onSubmit={this.search}
  63. placeholder="Search ..."
  64. />
  65. <div className="btn-group-submit-search">
  66. <span className="btn-link" onClick={this.search}>
  67. <i className="icon-magnifier"></i>
  68. </span>
  69. </div>
  70. </div>
  71. </div>
  72. );
  73. }
  74. }
  75. HeaderSearchBox.propTypes = {
  76. t: PropTypes.func.isRequired, // i18next
  77. crowi: PropTypes.object.isRequired,
  78. };
  79. export default withTranslation()(HeaderSearchBox);