129 lines
4.2 KiB
TypeScript
129 lines
4.2 KiB
TypeScript
"use client";
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import { ProductReviewsSection } from './ProductReviewsSection';
|
|
|
|
interface ProductTabsWrapperProps {
|
|
productId?: string;
|
|
productName?: string;
|
|
initialReviewsCount?: number;
|
|
descriptionHtml: React.ReactNode;
|
|
specsHtml: React.ReactNode;
|
|
showSpecifications?: boolean;
|
|
}
|
|
|
|
type TabId = 'description' | 'specs' | 'reviews';
|
|
|
|
const BOX = 'border border-gray-200 rounded-xl overflow-hidden';
|
|
|
|
export const ProductTabsWrapper: React.FC<ProductTabsWrapperProps> = ({
|
|
productId,
|
|
productName,
|
|
initialReviewsCount = 0,
|
|
descriptionHtml,
|
|
specsHtml,
|
|
showSpecifications = true,
|
|
}) => {
|
|
const [activeTab, setActiveTab] = useState<TabId>('description');
|
|
const [reviewsCount, setReviewsCount] = useState<number>(initialReviewsCount);
|
|
|
|
useEffect(() => {
|
|
if (typeof window !== 'undefined' && window.location.hash === '#reviews') {
|
|
setActiveTab('reviews');
|
|
setTimeout(() => {
|
|
document.getElementById('product-tabs')?.scrollIntoView({ behavior: 'smooth' });
|
|
}, 150);
|
|
}
|
|
}, []);
|
|
|
|
return (
|
|
<div id="product-tabs" className="bg-white mb-10 text-left">
|
|
{/* Modern High-Visibility Tab Bar */}
|
|
<div className="flex items-center justify-center border-b border-gray-200 gap-2 sm:gap-6 overflow-x-auto pb-px">
|
|
<button
|
|
id="tab-description-btn"
|
|
type="button"
|
|
onClick={() => setActiveTab('description')}
|
|
className={`pb-3 px-3 text-[15px] sm:text-[16px] font-semibold cursor-pointer border-b-2 transition-all flex items-center gap-1.5 ${
|
|
activeTab === 'description'
|
|
? 'border-primary text-primary font-bold'
|
|
: 'border-transparent text-gray-500 hover:text-gray-900'
|
|
}`}
|
|
>
|
|
<span>Description</span>
|
|
</button>
|
|
|
|
{showSpecifications && (
|
|
<button
|
|
id="tab-specs-btn"
|
|
type="button"
|
|
onClick={() => setActiveTab('specs')}
|
|
className={`pb-3 px-3 text-[15px] sm:text-[16px] font-semibold cursor-pointer border-b-2 transition-all flex items-center gap-1.5 ${
|
|
activeTab === 'specs'
|
|
? 'border-primary text-primary font-bold'
|
|
: 'border-transparent text-gray-500 hover:text-gray-900'
|
|
}`}
|
|
>
|
|
<span>Specifications</span>
|
|
</button>
|
|
)}
|
|
|
|
<button
|
|
id="tab-reviews-btn"
|
|
type="button"
|
|
onClick={() => setActiveTab('reviews')}
|
|
className={`pb-3 px-3 text-[15px] sm:text-[16px] font-semibold cursor-pointer border-b-2 transition-all flex items-center gap-2 ${
|
|
activeTab === 'reviews'
|
|
? 'border-primary text-primary font-bold'
|
|
: 'border-transparent text-gray-500 hover:text-gray-900'
|
|
}`}
|
|
>
|
|
<span>Customer Reviews</span>
|
|
<span
|
|
className={`px-2 py-0.5 rounded-full text-xs font-bold transition-colors ${
|
|
activeTab === 'reviews'
|
|
? 'bg-primary text-white'
|
|
: 'bg-gray-100 text-gray-700'
|
|
}`}
|
|
>
|
|
{reviewsCount}
|
|
</span>
|
|
</button>
|
|
</div>
|
|
|
|
{/* Tab Panels */}
|
|
<div className={`mt-5 ${BOX} bg-white`}>
|
|
|
|
{/* ── Description ── */}
|
|
<div className={activeTab === 'description' ? 'block' : 'hidden'}>
|
|
{descriptionHtml ? (
|
|
descriptionHtml
|
|
) : (
|
|
<p className="text-[13px] text-gray-400 py-8 text-center">
|
|
No description available for this product.
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
{/* ── Specifications ── */}
|
|
<div className={activeTab === 'specs' ? 'block' : 'hidden'}>
|
|
{specsHtml}
|
|
</div>
|
|
|
|
{/* ── Reviews ── */}
|
|
<div className={activeTab === 'reviews' ? 'block p-4 sm:p-6 md:p-8' : 'hidden'}>
|
|
{productId ? (
|
|
<ProductReviewsSection
|
|
productId={productId}
|
|
productName={productName || 'Product'}
|
|
onReviewsCountChange={setReviewsCount}
|
|
/>
|
|
) : (
|
|
<p className="text-xs text-gray-400 py-6 text-center">Reviews unavailable</p>
|
|
)}
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|