PagePathAutoComplete.jsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { pathUtils } from '@growi/core';
  4. import SearchTypeahead from './SearchTypeahead';
  5. const PagePathAutoComplete = (props) => {
  6. const {
  7. addTrailingSlash, onSubmit, onInputChange, initializedPath,
  8. } = props;
  9. function inputChangeHandler(pages) {
  10. if (onInputChange == null) {
  11. return;
  12. }
  13. const page = pages[0]; // should be single page selected
  14. if (page != null) {
  15. onInputChange(page.path);
  16. }
  17. }
  18. function submitHandler() {
  19. if (onSubmit == null) {
  20. return;
  21. }
  22. onSubmit();
  23. }
  24. function getKeywordOnInit(path) {
  25. return addTrailingSlash
  26. ? pathUtils.addTrailingSlash(path)
  27. : pathUtils.removeTrailingSlash(path);
  28. }
  29. return (
  30. <SearchTypeahead
  31. onSubmit={submitHandler}
  32. onChange={inputChangeHandler}
  33. onInputChange={props.onInputChange}
  34. inputName="new_path"
  35. placeholder="Input page path"
  36. keywordOnInit={getKeywordOnInit(initializedPath)}
  37. autoFocus={props.autoFocus}
  38. />
  39. );
  40. };
  41. PagePathAutoComplete.propTypes = {
  42. initializedPath: PropTypes.string,
  43. addTrailingSlash: PropTypes.bool,
  44. onSubmit: PropTypes.func,
  45. onInputChange: PropTypes.func,
  46. autoFocus: PropTypes.bool,
  47. };
  48. PagePathAutoComplete.defaultProps = {
  49. initializedPath: '/',
  50. autoFocus: false,
  51. };
  52. export default PagePathAutoComplete;