HeaderSearchBox.jsx 2.6 KB

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