PagePathAutoComplete.jsx 1.5 KB

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