PagePathLabel.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React, { FC } from 'react';
  2. import { DevidedPagePath } from '@growi/core';
  3. type TextElemProps = {
  4. children?: React.ReactNode
  5. isHTML?: boolean,
  6. }
  7. const TextElement: FC<TextElemProps> = (props: TextElemProps) => (
  8. <>
  9. { props.isHTML
  10. // eslint-disable-next-line react/no-danger
  11. ? <span dangerouslySetInnerHTML={{ __html: props.children?.toString() || '' }}></span>
  12. : <>{props.children}</>
  13. }
  14. </>
  15. );
  16. type Props = {
  17. path: string,
  18. isLatterOnly?: boolean,
  19. isFormerOnly?: boolean,
  20. isPathIncludedHtml?: boolean,
  21. additionalClassNames?: string[],
  22. }
  23. export const PagePathLabel: FC<Props> = (props:Props) => {
  24. const {
  25. isLatterOnly, isFormerOnly, isPathIncludedHtml, additionalClassNames, path,
  26. } = props;
  27. const dPagePath = new DevidedPagePath(path, false, true);
  28. const classNames = additionalClassNames || [];
  29. let textElem;
  30. if (isLatterOnly) {
  31. textElem = <TextElement isHTML={isPathIncludedHtml}>{dPagePath.latter}</TextElement>;
  32. }
  33. else if (isFormerOnly) {
  34. textElem = dPagePath.isFormerRoot
  35. ? <>/</>
  36. : <TextElement isHTML={isPathIncludedHtml}>{dPagePath.former}</TextElement>;
  37. }
  38. else {
  39. textElem = dPagePath.isRoot
  40. ? <strong>/</strong>
  41. : <TextElement isHTML={isPathIncludedHtml}>{dPagePath.former}/<strong>{dPagePath.latter}</strong></TextElement>;
  42. }
  43. return <span className={classNames.join(' ')}>{textElem}</span>;
  44. };