index.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use client";
  2. import { usePathname, useRouter } from "@/i18n/routing";
  3. import clsx from "clsx";
  4. import { useTranslations } from "next-intl";
  5. import { FC, PropsWithChildren, ReactNode, useEffect, useState } from "react";
  6. import styles from "./style.module.scss";
  7. import clearCache from "./actions";
  8. /**
  9. * @description 自定义头部返回组件
  10. * @param {string} title header标题
  11. * @param {ReactNode} children 插槽内容
  12. * @param {() => ReactNode} headerRender 自定义渲染
  13. */
  14. export interface HeaderBackProps {
  15. title?: string;
  16. children?: ReactNode;
  17. showBack?: Boolean;
  18. headerRender?: () => ReactNode;
  19. className?: string;
  20. useBg?: Boolean;
  21. Right?: () => ReactNode; // 右侧自定义渲染,优先级高于childre
  22. }
  23. const HeaderBack: FC<PropsWithChildren<HeaderBackProps>> = ({
  24. title = "",
  25. showBack = true,
  26. children,
  27. className,
  28. useBg = false,
  29. Right,
  30. }) => {
  31. const t = useTranslations("HeaderBack");
  32. const iconClassName1 = clsx(
  33. {
  34. [styles.iconfontIcon1]: true,
  35. },
  36. "iconfont icon-xiangzuo1 text-[#87ccda]"
  37. );
  38. const iconClassName2 = clsx(
  39. {
  40. [styles.iconfontIcon2]: true,
  41. },
  42. "iconfont icon-company_nav_icon_home text-[#87ccda]"
  43. );
  44. let pathname = usePathname();
  45. let [selfTitle, setSelfTitle] = useState("");
  46. const setSelfTitleFun = () => {
  47. if (pathname == "/deposit") {
  48. setSelfTitle(t("Depósito"));
  49. return;
  50. }
  51. setSelfTitle("");
  52. };
  53. useEffect(() => {
  54. setSelfTitleFun();
  55. // eslint-disable-next-line react-hooks/exhaustive-deps
  56. }, [pathname]);
  57. const router = useRouter();
  58. const goPage = async (path = "") => {
  59. if (path) {
  60. await clearCache();
  61. router.replace("/");
  62. return;
  63. }
  64. router.back();
  65. };
  66. const cls = clsx(styles.headerBack, className, useBg && styles.useBg);
  67. return (
  68. <div className={cls}>
  69. <div className={styles.left}>
  70. {showBack && <span className={iconClassName1} onClick={() => goPage()}></span>}
  71. </div>
  72. <div className={clsx(styles.title, "text-[#87ccda]")}>
  73. {(title || selfTitle) && <span>{title || selfTitle}</span>}
  74. </div>
  75. <div className={styles.content}>{children}</div>
  76. {!Right && (
  77. <span className={styles.right} onClick={() => goPage("home")}>
  78. <span className={iconClassName2}></span>
  79. </span>
  80. )}
  81. {Right && Right()}
  82. </div>
  83. );
  84. };
  85. export default HeaderBack;