Sfoglia il codice sorgente

根据lint调整代码

zhangzf 21 ore fa
parent
commit
60dddce81e

+ 10 - 3
src/components/layout/navbar/NavbarSearchIcon.tsx

@@ -1,11 +1,18 @@
 "use client";
 
-import { useState } from "react";
+import { useState,useEffect } from "react";
 import SearchPop from "./SearchPop";
-
+import { usePathname } from "next/navigation";
 export default function NavbarSearchIcon() {
   const [searchOpen, setSearchOpen] = useState(false);
-
+  const pathname = usePathname();
+  useEffect(() => {
+    if (searchOpen) {
+      queueMicrotask(() => {
+        setSearchOpen(false);
+      });
+    }
+  }, [pathname, searchOpen]);
   const handleClick = () => {
     setSearchOpen((prev) => !prev);
   };

+ 9 - 9
src/components/layout/navbar/SearchPop.tsx

@@ -7,7 +7,7 @@ import Grid from "@/components/theme/ui/grid/Grid";
 import HotSearch from "@/app/(public)/search/_components/HotSearch";
 import NewProductGridItems from "@components/catalog/product/NewProductGridItems";
 import Recommend from "@/app/(public)/product/_components/youmaylike/Recommend";
-import { usePathname } from "next/navigation";
+// import { usePathname } from "next/navigation";
 // const ProductGridItems = dynamicImport(
 //   () => import("@/components/catalog/product/ProductGridItems"),
 // );
@@ -43,13 +43,8 @@ export default function SearchPop({
   const hasKeyword = keyword.trim().length > 0;
 
   const hasSearchResults = hasKeyword && products.length > 0;
-  const pathname = usePathname();
-  useEffect(() => {
-    // 打开状态下,路由变化就关闭弹窗
-    if (open) {
-      handleClose();
-    }
-  }, [pathname]);
+  // const pathname = usePathname();
+
   const showYouMayLike = !hasKeyword || (hasKeyword && products.length === 0);
 
   // =========================
@@ -118,7 +113,12 @@ export default function SearchPop({
     setKeyword("");
     onClose();
   };
-
+  // useEffect(() => {
+  //   // 打开状态下,路由变化就关闭弹窗
+  //   if (open) {
+  //     handleClose();
+  //   }
+  // }, [pathname]);
   // =========================
   // placeholder 轮播
   // =========================