PagePathAutoComplete.jsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. if (path == null) {
  26. return;
  27. }
  28. return addTrailingSlash
  29. ? pathUtils.addTrailingSlash(path)
  30. : pathUtils.removeTrailingSlash(path);
  31. }
  32. return (
  33. <SearchTypeahead
  34. onSubmit={submitHandler}
  35. onChange={inputChangeHandler}
  36. onInputChange={props.onInputChange}
  37. inputName="new_path"
  38. placeholder="Input page path"
  39. keywordOnInit={getKeywordOnInit(initializedPath)}
  40. autoFocus={props.autoFocus}
  41. />
  42. );
  43. };
  44. PagePathAutoComplete.propTypes = {
  45. initializedPath: PropTypes.string,
  46. addTrailingSlash: PropTypes.bool,
  47. onSubmit: PropTypes.func,
  48. onInputChange: PropTypes.func,
  49. autoFocus: PropTypes.bool,
  50. };
  51. PagePathAutoComplete.defaultProps = {
  52. initializedPath: '/',
  53. autoFocus: false,
  54. };
  55. export default PagePathAutoComplete;