TextInputForPageTitleAndPath.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { FC, useCallback } from 'react';
  2. import type { Dispatch, SetStateAction } from 'react';
  3. import nodePath from 'path';
  4. import type { IPagePopulatedToShowRevision } from '@growi/core';
  5. import { pathUtils } from '@growi/core/dist/utils';
  6. import { useTranslation } from 'next-i18next';
  7. import { ValidationTarget } from '~/client/util/input-validator';
  8. import ClosableTextInput from '../Common/ClosableTextInput';
  9. import { usePagePathRenameHandler } from './page-header-utils';
  10. type StateHandler = {
  11. isRenameInputShown: boolean
  12. setRenameInputShown: Dispatch<SetStateAction<boolean>>
  13. }
  14. type Props = {
  15. currentPage: IPagePopulatedToShowRevision
  16. stateHandler: StateHandler
  17. inputValue: string
  18. CustomComponent: JSX.Element
  19. handleInputChange?: (string) => void
  20. }
  21. export const TextInputForPageTitleAndPath: FC<Props> = (props) => {
  22. const {
  23. currentPage, stateHandler, inputValue, CustomComponent, handleInputChange,
  24. } = props;
  25. const { t } = useTranslation();
  26. const { isRenameInputShown, setRenameInputShown } = stateHandler;
  27. const onRenameFinish = () => {
  28. setRenameInputShown(false);
  29. };
  30. const onRenameFailure = () => {
  31. setRenameInputShown(true);
  32. };
  33. const pagePathRenameHandler = usePagePathRenameHandler(currentPage, onRenameFinish, onRenameFailure);
  34. const onPressEnter = useCallback((inputPagePath: string) => {
  35. const parentPath = pathUtils.addTrailingSlash(nodePath.dirname(currentPage.path ?? ''));
  36. const newPagePath = nodePath.resolve(parentPath, inputPagePath);
  37. pagePathRenameHandler(newPagePath);
  38. }, [currentPage.path, pagePathRenameHandler]);
  39. return (
  40. <>
  41. {isRenameInputShown ? (
  42. <div className="flex-fill">
  43. <ClosableTextInput
  44. value={inputValue}
  45. placeholder={t('Input page name')}
  46. onPressEnter={onPressEnter}
  47. validationTarget={ValidationTarget.PAGE}
  48. handleInputChange={handleInputChange}
  49. />
  50. </div>
  51. ) : (
  52. <>{ CustomComponent }</>
  53. )}
  54. </>
  55. );
  56. };