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 (
{/* 返回 */}
history.go(-1)}>
{/* 主体 */}

近代建筑一张图

开平碉楼与村落及赤坎旧镇

{/* 搜索框 */}
{ if (e.key === "Enter") btnSearchFu(); }} >
setValue(e.target.value.trim())} />
搜 索
{/* 信息展示 */} {data.length ? (
{data.map((v) => (
history.push(v.path)} >
{v.name}
{v.type}
查看
))}
) : (
没有找到相关信息...
)}
); } const MemoZ1Search = React.memo(Z1Search); export default MemoZ1Search;