PagePathAutoComplete.jsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { pathUtils } from 'growi-commons';
  4. import SearchTypeahead from './SearchTypeahead';
  5. export default class PagePathAutoComplete extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. };
  10. this.crowi = this.props.crowi;
  11. this.onSubmit = this.onSubmit.bind(this);
  12. this.getKeywordOnInit = this.getKeywordOnInit.bind(this);
  13. }
  14. componentDidMount() {
  15. }
  16. componentWillUnmount() {
  17. }
  18. onSubmit(query) {
  19. // get the closest form element
  20. const elem = this.rootDom;
  21. const form = elem.closest('form');
  22. // submit with jQuery
  23. $(form).submit();
  24. }
  25. getKeywordOnInit(path) {
  26. return this.props.addTrailingSlash
  27. ? pathUtils.addTrailingSlash(path)
  28. : pathUtils.removeTrailingSlash(path);
  29. }
  30. render() {
  31. return (
  32. <div ref={(c) => { this.rootDom = c }}>
  33. <SearchTypeahead
  34. ref={this.searchTypeaheadDom}
  35. crowi={this.crowi}
  36. onSubmit={this.onSubmit}
  37. inputName="new_path"
  38. emptyLabelExceptError={null}
  39. placeholder="Input page path"
  40. keywordOnInit={this.getKeywordOnInit(this.props.initializedPath)}
  41. />
  42. </div>
  43. );
  44. }
  45. }
  46. PagePathAutoComplete.propTypes = {
  47. crowi: PropTypes.object.isRequired,
  48. initializedPath: PropTypes.string,
  49. addTrailingSlash: PropTypes.bool,
  50. };
  51. PagePathAutoComplete.defaultProps = {
  52. initializedPath: '/',
  53. };