index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import React, { useCallback, useEffect, useRef, useState } from "react";
  2. import styles from "./index.module.scss";
  3. import { baseUrl } from "@/index";
  4. import { SwapRightOutlined } from "@ant-design/icons";
  5. import { useSelector } from "react-redux";
  6. import { RootState } from "@/store";
  7. import { InfoRowType, SearchType } from "@/types";
  8. import history, { isMobileFu } from "@/utils/history";
  9. import classNames from "classnames";
  10. function Z1Search() {
  11. // 移动端没有搜索页面,直接跳首页
  12. useEffect(() => {
  13. if (isMobileFu()) history.replace("/");
  14. }, []);
  15. const [value, setValue] = useState("");
  16. // 获取所有数据
  17. const { village, architec, build } = useSelector(
  18. (state: RootState) => state.A0Layout.dataAll
  19. );
  20. const dataAllRef = useRef([] as SearchType[]);
  21. const [data, setData] = useState([
  22. { id: "0", type: "建筑", name: "", path: "", txt: "" },
  23. ] as SearchType[]);
  24. useEffect(() => {
  25. const arr = [] as SearchType[];
  26. village.forEach((v) => {
  27. arr.push({
  28. id: v.id + "村落",
  29. name: v.name,
  30. txt: v.infoTxt,
  31. type: "村落",
  32. path: `/village?id=${v.id}`,
  33. });
  34. });
  35. for (const k in architec) {
  36. const arr2 = Reflect.get(architec, k).data as InfoRowType[];
  37. arr2.forEach((v) => {
  38. arr.push({
  39. id: v.id + "建筑",
  40. name: v.name,
  41. txt: v.txt,
  42. type: "建筑",
  43. path: `architecInfo?id=${v.id}`,
  44. });
  45. });
  46. }
  47. build.forEach((v) => {
  48. arr.push({
  49. id: v.id + "构件",
  50. name: v.name,
  51. txt: v.txt,
  52. type: "构件",
  53. path: `/buildInfo?id=${v.id}`,
  54. });
  55. });
  56. dataAllRef.current = arr;
  57. setData(arr);
  58. }, [architec, build, village]);
  59. // 点击搜索
  60. const btnSearchFu = useCallback(() => {
  61. const dom = document.querySelector(".Z1list") as HTMLDivElement;
  62. if (dom) dom.scrollTop = 0;
  63. if (value) {
  64. const arr = dataAllRef.current.filter(
  65. (v) => v.name.includes(value) || v.txt.includes(value)
  66. );
  67. setData(arr);
  68. } else setData(dataAllRef.current);
  69. }, [value]);
  70. return (
  71. <div
  72. className={styles.Z1Search}
  73. style={{ backgroundImage: `url(${baseUrl}/Z1Search/pc/bg.jpg)` }}
  74. >
  75. {/* 返回 */}
  76. <div className="Z1back" onClick={() => history.go(-1)}></div>
  77. {/* 主体 */}
  78. <div className="Z1main">
  79. <h1>近代建筑一张图</h1>
  80. <p>开平碉楼与村落及赤坎旧镇</p>
  81. {/* 搜索框 */}
  82. <div
  83. className="Z1input"
  84. onKeyUp={(e) => {
  85. if (e.key === "Enter") btnSearchFu();
  86. }}
  87. >
  88. <div className="Z1input1"></div>
  89. <div className="Z1input2">
  90. <input
  91. placeholder="请输入关键词..."
  92. maxLength={30}
  93. value={value}
  94. onChange={(e) => setValue(e.target.value.trim())}
  95. />
  96. </div>
  97. <div className="Z1input3" onClick={btnSearchFu}>
  98. 搜 索
  99. </div>
  100. </div>
  101. {/* 信息展示 */}
  102. {data.length ? (
  103. <div className="Z1list mySorrl">
  104. {data.map((v) => (
  105. <div
  106. className={classNames(
  107. "Z1listRow",
  108. v.id === "0" ? "Z1listRowHide" : ""
  109. )}
  110. key={v.id}
  111. onClick={() => history.push(v.path)}
  112. >
  113. <div className="Z1listRow1">
  114. <div>{v.name}</div>
  115. <div className="Z1listRow1_1">{v.type}</div>
  116. </div>
  117. <div className="Z1listRow2">
  118. <div>查看</div>
  119. <div className="Z1listRow2_1">
  120. <SwapRightOutlined rev={undefined} />
  121. </div>
  122. </div>
  123. </div>
  124. ))}
  125. </div>
  126. ) : (
  127. <div className="Z1listNone">没有找到相关信息...</div>
  128. )}
  129. </div>
  130. </div>
  131. );
  132. }
  133. const MemoZ1Search = React.memo(Z1Search);
  134. export default MemoZ1Search;