PageTitleHeader.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import type { FC } from 'react';
  2. import { useState, useMemo } from 'react';
  3. import nodePath from 'path';
  4. import type { IPagePopulatedToShowRevision } from '@growi/core';
  5. import { TextInputForPageTitleAndPath } from './TextInputForPageTitleAndPath';
  6. type Props = {
  7. currentPagePath: string,
  8. currentPage: IPagePopulatedToShowRevision;
  9. }
  10. export const PageTitleHeader: FC<Props> = (props) => {
  11. const { currentPagePath, currentPage } = props;
  12. const pageName = nodePath.basename(currentPagePath ?? '') || '/';
  13. const [isRenameInputShown, setRenameInputShown] = useState(false);
  14. const [inputText, setInputText] = useState(pageName);
  15. const stateHandler = { isRenameInputShown, setRenameInputShown };
  16. const PageTitle = useMemo(() => (<div onClick={() => setRenameInputShown(true)}>{pageName}</div>), [pageName]);
  17. const handleInputChange = (inputText: string) => {
  18. if (inputText !== '') {
  19. setInputText(inputText);
  20. }
  21. else {
  22. setInputText(pageName);
  23. }
  24. };
  25. const onBlurHandler = () => {
  26. if (pageName === inputText) {
  27. setRenameInputShown(false);
  28. }
  29. };
  30. return (
  31. <div onBlur={onBlurHandler}>
  32. <TextInputForPageTitleAndPath
  33. currentPage={currentPage}
  34. stateHandler={stateHandler}
  35. inputValue={inputText}
  36. CustomComponent={PageTitle}
  37. handleInputChange={handleInputChange}
  38. />
  39. </div>
  40. );
  41. };