| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- "use client";
- import { useState } from "react";
- import { Swiper, SwiperSlide } from "swiper/react";
- import type { Swiper as SwiperType } from "swiper/types";
- import { Navigation } from "swiper/modules";
- import Image from "next/image";
- import type { HomeSectionImage } from "@/types/home/type";
- interface jinGangProps {
- jinGang: HomeSectionImage[];
- }
- // const MOCK_DATA = [
- // [
- // {
- // name: "Hd Lace Wig",
- // img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095740_8181b0e039800e07.png",
- // },
- // {
- // name: "Ready To Go",
- // img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095740_8181b0e039800e07.png",
- // },
- // {
- // name: "Colored Wig",
- // img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095740_8181b0e039800e07.png",
- // },
- // {
- // name: "U&V Part Wig",
- // img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095740_8181b0e039800e07.png",
- // },
- // {
- // name: "New Arrival",
- // img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095740_8181b0e039800e07.png",
- // },
- // {
- // name: "360 Wig",
- // img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095740_8181b0e039800e07.png",
- // },
- // ],
- // [
- // {
- // name: "13x4 Lace Wig",
- // img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095800_8181b0e039800e07.png",
- // },
- // {
- // name: "Body Wave Wig",
- // img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095800_8181b0e039800e07.png",
- // },
- // {
- // name: "Deep Wave Wig",
- // img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095800_8181b0e039800e07.png",
- // },
- // {
- // name: "Straight Wig",
- // img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095800_8181b0e039800e07.png",
- // },
- // {
- // name: "Curly Wig",
- // img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095800_8181b0e039800e07.png",
- // },
- // {
- // name: "Bob Wig",
- // img: "https://cdn.asteriahair.com/uploads/202602/11/20260211095800_8181b0e039800e07.png",
- // },
- // ],
- // ];
- export default function JinGangSwiper({ jinGang }: jinGangProps) {
- const [activeIndex, setActiveIndex] = useState(0);
- const [swiperRef, setSwiperRef] = useState<SwiperType | null>(null);
- if (!jinGang || jinGang.length === 0) return null;
- // 4. 真实数据按每6个拆分一组,替换原来的模拟拆分逻辑
- const chunkSize = 6;
- const productBlocks: HomeSectionImage[][] = [];
- for (let i = 0; i < jinGang.length; i += chunkSize) {
- productBlocks.push(jinGang.slice(i, i + chunkSize));
- }
- const handleSlideChange = (swiper: any) => {
- setActiveIndex(swiper.activeIndex);
- };
- const handleIndicatorClick = (index: any) => {
- if (swiperRef) {
- swiperRef.slideTo(index);
- setActiveIndex(index);
- }
- };
- return (
- <div className="max-w-screen-md mx-auto p-4">
- <Swiper
- modules={[Navigation]}
- spaceBetween={10}
- slidesPerView={1}
- onSwiper={(swiper) => setSwiperRef(swiper)}
- onSlideChange={handleSlideChange}
- className="mb-6"
- >
- {productBlocks.map((block, blockIndex) => (
- <SwiperSlide key={blockIndex}>
- <div className="grid grid-cols-3 gap-2">
- {block.map((item, idx) => (
- <div
- key={`block1-${idx}`}
- className="relative w-full aspect-[3/4]"
- >
- <Image
- src={item.image}
- alt={item.title}
- fill
- className="object-cover rounded-md"
- />
- {/* <div className="absolute bottom-2 left-2 text-white text-sm font-medium bg-black/50 px-2 py-1 rounded">
- {item.name}
- </div> */}
- </div>
- ))}
- </div>
- </SwiperSlide>
- ))}
- </Swiper>
- {/* 底部指示器按钮:2个,激活态黑色,未激活灰色 */}
- <div className="flex justify-center gap-2">
- <button
- onClick={() => handleIndicatorClick(0)}
- className={`w-6 h-[5px] rounded-full opacity-100 ${
- activeIndex === 0 ? "bg-[#333]" : "bg-[#E5E5E5]"
- }`}
- aria-label="切换到第一块"
- />
- <button
- onClick={() => handleIndicatorClick(1)}
- className={`w-6 h-[5px] rounded-full opacity-100 ${
- activeIndex === 1 ? "bg-[#333]" : "bg-[#E5E5E5]"
- }`}
- aria-label="切换到第二块"
- />
- </div>
- </div>
- );
- }
|