PagePathAutoComplete.jsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { pathUtils } from 'growi-commons';
  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. emptyLabelExceptError={null}
  36. placeholder="Input page path"
  37. keywordOnInit={getKeywordOnInit(initializedPath)}
  38. />
  39. );
  40. };
  41. PagePathAutoComplete.propTypes = {
  42. initializedPath: PropTypes.string,
  43. addTrailingSlash: PropTypes.bool,
  44. onSubmit: PropTypes.func,
  45. onInputChange: PropTypes.func,
  46. };
  47. PagePathAutoComplete.defaultProps = {
  48. initializedPath: '/',
  49. };
  50. export default PagePathAutoComplete;