import React, { useCallback, useEffect, useRef, useState } from "react"; import styles from "./index.module.scss"; import { baseUrl } from "@/index"; import { SwapRightOutlined } from "@ant-design/icons"; import { useSelector } from "react-redux"; import { RootState } from "@/store"; import { InfoRowType, SearchType } from "@/types"; import history, { isMobileFu } from "@/utils/history"; import classNames from "classnames"; function Z1Search() { // 移动端没有搜索页面,直接跳首页 useEffect(() => { if (isMobileFu()) history.replace("/"); }, []); const [value, setValue] = useState(""); // 获取所有数据 const { village, architec, build } = useSelector( (state: RootState) => state.A0Layout.dataAll ); const dataAllRef = useRef([] as SearchType[]); const [data, setData] = useState([ { id: "0", type: "建筑", name: "", path: "", txt: "" }, ] as SearchType[]); useEffect(() => { const arr = [] as SearchType[]; village.forEach((v) => { arr.push({ id: v.id + "村落", name: v.name, txt: v.infoTxt, type: "村落", path: `/village?id=${v.id}`, }); }); for (const k in architec) { const arr2 = Reflect.get(architec, k).data as InfoRowType[]; arr2.forEach((v) => { arr.push({ id: v.id + "建筑", name: v.name, txt: v.txt, type: "建筑", path: `architecInfo?id=${v.id}`, }); }); } build.forEach((v) => { arr.push({ id: v.id + "构件", name: v.name, txt: v.txt, type: "构件", path: `/buildInfo?id=${v.id}`, }); }); dataAllRef.current = arr; setData(arr); }, [architec, build, village]); // 点击搜索 const btnSearchFu = useCallback(() => { const dom = document.querySelector(".Z1list") as HTMLDivElement; if (dom) dom.scrollTop = 0; if (value) { const arr = dataAllRef.current.filter( (v) => v.name.includes(value) || v.txt.includes(value) ); setData(arr); } else setData(dataAllRef.current); }, [value]); return (
开平碉楼与村落及赤坎旧镇
{/* 搜索框 */}