TextInputForPageTitleAndPath.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { FC } from 'react';
  2. import type { Dispatch, SetStateAction } from 'react';
  3. import type { IPagePopulatedToShowRevision } from '@growi/core';
  4. import { useTranslation } from 'next-i18next';
  5. import { ValidationTarget } from '~/client/util/input-validator';
  6. import ClosableTextInput from '../Common/ClosableTextInput';
  7. import { usePagePathSubmitHandler } from './page-header-utils';
  8. type StateHandler = {
  9. isRenameInputShown: boolean
  10. setRenameInputShown: Dispatch<SetStateAction<boolean>>
  11. }
  12. type Props = {
  13. currentPagePath: string
  14. currentPage: IPagePopulatedToShowRevision
  15. stateHandler: StateHandler
  16. inputValue: string
  17. CustomComponent: () => JSX.Element
  18. handleInputChange?: (string) => void
  19. }
  20. export const TextInputForPageTitleAndPath: FC<Props> = (props) => {
  21. const {
  22. currentPagePath, currentPage, stateHandler, inputValue, CustomComponent, handleInputChange,
  23. } = props;
  24. const { t } = useTranslation();
  25. const { isRenameInputShown, setRenameInputShown } = stateHandler;
  26. const pagePathSubmitHandler = usePagePathSubmitHandler(currentPage, currentPagePath, setRenameInputShown);
  27. return (
  28. <>
  29. {isRenameInputShown ? (
  30. <div className="flex-fill">
  31. <ClosableTextInput
  32. value={inputValue}
  33. placeholder={t('Input page name')}
  34. onClickOutside={() => { setRenameInputShown(false) }}
  35. onPressEnter={pagePathSubmitHandler}
  36. validationTarget={ValidationTarget.PAGE}
  37. handleInputChange={handleInputChange}
  38. />
  39. </div>
  40. ) : (
  41. <CustomComponent />
  42. )}
  43. </>
  44. );
  45. };