index.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import React, { useCallback, useEffect, useState } from "react";
  2. import styles from "./index.module.scss";
  3. import { Swiper, SwiperSlide } from "swiper/react";
  4. import classNames from "classnames";
  5. // Import Swiper styles
  6. import "swiper/css";
  7. import { envUrl } from "@/utils/history";
  8. import { A2SwType } from "@/types";
  9. import store from "@/store";
  10. type Props = {
  11. data: A2SwType[];
  12. type: number;
  13. };
  14. function SwiperCom({ data, type }: Props) {
  15. useEffect(() => {
  16. setActive(0);
  17. }, [type]);
  18. // 选中状态
  19. const [active, setActive] = useState(0);
  20. const cutAcFu = useCallback(
  21. (item: A2SwType) => {
  22. setActive(item.id);
  23. if (type === 1) {
  24. // 如果是历史,打开视频弹窗
  25. store.dispatch({
  26. type: "main/lookVideo",
  27. payload: {
  28. id: item.id,
  29. title: item.info?.title!,
  30. txt: item.info?.txt!,
  31. },
  32. });
  33. } else {
  34. window.open(item.path);
  35. }
  36. },
  37. [type]
  38. );
  39. // swiper实例
  40. // const mySwiperRef = useRef<any>(null);
  41. return (
  42. <div className={styles.SwiperCom}>
  43. <Swiper
  44. // centeredSlides={true}
  45. spaceBetween={0}
  46. slidesPerView={6}
  47. className={classNames(
  48. "SwiperComBox",
  49. data.length <= 6 ? "SwiperComBoxCen" : ""
  50. )}
  51. // onSwiper={(swiper) => (mySwiperRef.current = swiper)}
  52. >
  53. {data.map((v) => (
  54. <SwiperSlide key={v.id}>
  55. <div
  56. title={v.name}
  57. onClick={() => cutAcFu(v)}
  58. className={classNames(
  59. "SwiperComRow",
  60. active === v.id ? "SwiperComRowAc" : ""
  61. )}
  62. >
  63. <img src={envUrl + `/swData/${type}/${v.img}`} alt="" />
  64. <div className="SwiperComSm">
  65. <p>{v.name}</p>
  66. </div>
  67. <div className="SwiperComName">{v.name}</div>
  68. </div>
  69. </SwiperSlide>
  70. ))}
  71. </Swiper>
  72. </div>
  73. );
  74. }
  75. const MemoSwiperCom = React.memo(SwiperCom);
  76. export default MemoSwiperCom;