feat(wiki): add accessible accordion disclosures for ingredients
Deploy Canina / deploy (push) Successful in 2m6s
Deploy Canina / deploy (push) Successful in 2m6s
This commit is contained in:
1 parent
2c3e4bb752
commit
5b20bab381
2 files changed
+130
-12
No files matched your search
@@ -2,7 +2,7 @@
|
||||
import { useState, useEffect, useMemo } from "react";
|
||||
import { Product, IngredientInfo } from "../lib/data/products";
|
||||
import { motion } from "motion/react";
|
||||
import { FlaskConical, CheckCircle2, ChevronRight, ChevronLeft, Beaker, Search } from "lucide-react";
|
||||
import { FlaskConical, CheckCircle2, ChevronLeft, ChevronDown, Beaker, Search } from "lucide-react";
|
||||
import { productService } from "../lib/services/productService";
|
||||
|
||||
import Link from "next/link";
|
||||
@@ -68,6 +68,7 @@ export default function IngredientWiki() {
|
||||
const [products, setProducts] = useState<Product[]>([]);
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const [visibleCount, setVisibleCount] = useState(8);
|
||||
const [expandedIngredients, setExpandedIngredients] = useState<Set<string>>(new Set());
|
||||
|
||||
useEffect(() => {
|
||||
productService.getProducts({ limit: 999 })
|
||||
@@ -115,6 +116,12 @@ export default function IngredientWiki() {
|
||||
|
||||
useEffect(() => {
|
||||
if (!termParam || ingredientsWiki.length === 0) return;
|
||||
setExpandedIngredients(current => {
|
||||
if (current.has(termParam)) return current;
|
||||
const next = new Set(current);
|
||||
next.add(termParam);
|
||||
return next;
|
||||
});
|
||||
const targetIndex = ingredientsWiki.findIndex(ing => ing.id === termParam);
|
||||
if (targetIndex >= visibleCount) {
|
||||
setVisibleCount(Math.ceil((targetIndex + 1) / 8) * 8);
|
||||
@@ -240,13 +247,51 @@ export default function IngredientWiki() {
|
||||
{visibleIngredients.map((ing, idx) => {
|
||||
const hasIngredient = getProductsForIngredient(ing.id, ing.name);
|
||||
const isTargeted = termParam === ing.id;
|
||||
const isExpanded = expandedIngredients.has(ing.id);
|
||||
const safeId = ing.id.replace(/[^a-zA-Z0-9_-]/g, '-');
|
||||
const triggerId = `wiki-trigger-${safeId}`;
|
||||
const panelId = `wiki-panel-${safeId}`;
|
||||
|
||||
return (
|
||||
<div
|
||||
key={ing.id}
|
||||
id={`wiki-${ing.id}`}
|
||||
className={`flex flex-col lg:flex-row items-stretch gap-5 sm:gap-10 p-4 sm:p-8 lg:p-10 rounded-2xl sm:rounded-[2.5rem] transition-all duration-300 border border-medical-gray-200/80 shadow-xs hover:shadow-lg ${isTargeted ? 'bg-canina-blue/5 border-2 border-canina-blue ring-4 ring-canina-blue/10 shadow-xl' : 'bg-white'} ${idx % 2 === 1 ? 'lg:flex-row-reverse' : ''}`}
|
||||
className={`p-3 sm:p-5 lg:p-6 rounded-2xl sm:rounded-[2.5rem] transition-all duration-300 border border-medical-gray-200/80 shadow-xs hover:shadow-lg ${isTargeted ? 'bg-canina-blue/5 border-2 border-canina-blue ring-4 ring-canina-blue/10 shadow-xl' : 'bg-white'}`}
|
||||
>
|
||||
<h3>
|
||||
<button
|
||||
type="button"
|
||||
id={triggerId}
|
||||
aria-expanded={isExpanded}
|
||||
aria-controls={panelId}
|
||||
onClick={() => setExpandedIngredients(current => {
|
||||
const next = new Set(current);
|
||||
if (next.has(ing.id)) next.delete(ing.id); else next.add(ing.id);
|
||||
return next;
|
||||
})}
|
||||
className="w-full min-h-11 flex items-center gap-3 text-right rounded-xl p-1 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-canina-blue focus-visible:ring-offset-2 cursor-pointer"
|
||||
>
|
||||
<span className="w-10 h-10 sm:w-12 sm:h-12 rounded-xl sm:rounded-2xl bg-medical-gray-900 flex items-center justify-center text-white shadow-md shrink-0">
|
||||
<Beaker className="w-6 h-6 text-canina-gold" />
|
||||
</span>
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block text-base sm:text-2xl font-black text-medical-gray-900 leading-tight font-vazir">{ing.name}</span>
|
||||
<span data-testid="wiki-ingredient-code" className="text-[10px] sm:text-[12px] text-canina-blue font-bold tracking-normal sm:tracking-wider font-vazir inline-block max-w-full mt-1 bg-canina-blue/5 px-2 py-0.5 rounded-lg border border-canina-blue/10 break-all">کد ماده مؤثره: <span className="font-mono">{ing.id}</span></span>
|
||||
</span>
|
||||
<span className="shrink-0 flex items-center gap-1.5 text-[11px] sm:text-xs font-black text-canina-blue">
|
||||
<span>{isExpanded ? 'بستن جزئیات' : 'نمایش جزئیات'}</span>
|
||||
<ChevronDown aria-hidden="true" className={`w-4 h-4 transition-transform motion-reduce:transition-none ${isExpanded ? 'rotate-180' : ''}`} />
|
||||
</span>
|
||||
</button>
|
||||
</h3>
|
||||
|
||||
<div
|
||||
id={panelId}
|
||||
role="region"
|
||||
aria-labelledby={triggerId}
|
||||
hidden={!isExpanded}
|
||||
className={`mt-4 flex flex-col lg:flex-row items-stretch gap-5 sm:gap-10 ${idx % 2 === 1 ? 'lg:flex-row-reverse' : ''}`}
|
||||
>
|
||||
{/* Content */}
|
||||
<div className="lg:w-1/2 flex flex-col justify-between space-y-6">
|
||||
<motion.div
|
||||
@@ -255,16 +300,6 @@ export default function IngredientWiki() {
|
||||
viewport={{ once: true }}
|
||||
transition={{ duration: 0.6 }}
|
||||
>
|
||||
<div className="flex items-center gap-3 mb-3 sm:mb-4">
|
||||
<div className="w-10 h-10 sm:w-12 sm:h-12 rounded-xl sm:rounded-2xl bg-medical-gray-900 flex items-center justify-center text-white shadow-md shrink-0">
|
||||
<Beaker className="w-6 h-6 text-canina-gold" />
|
||||
</div>
|
||||
<div>
|
||||
<h3 className="text-base sm:text-2xl font-black text-medical-gray-900 leading-tight font-vazir">{ing.name}</h3>
|
||||
<span data-testid="wiki-ingredient-code" className="text-[10px] sm:text-[12px] text-canina-blue font-bold tracking-normal sm:tracking-wider font-vazir inline-block max-w-full mt-1 bg-canina-blue/5 px-2 py-0.5 rounded-lg border border-canina-blue/10 break-all">کد ماده مؤثره: <span className="font-mono">{ing.id}</span></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p className="text-xs sm:text-sm text-medical-gray-600 leading-relaxed mb-3 sm:mb-4 font-vazir">
|
||||
{ing.description}
|
||||
</p>
|
||||
@@ -355,6 +390,7 @@ export default function IngredientWiki() {
|
||||
<div className="absolute -bottom-20 -left-20 w-80 h-80 bg-canina-blue rounded-full blur-[100px] opacity-10 pointer-events-none" />
|
||||
</motion.div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
|
||||
@@ -281,6 +281,88 @@ describe('IngredientWiki Component', () => {
|
||||
});
|
||||
|
||||
|
||||
it('renders accessible disclosures and preserves complete content while toggling', async () => {
|
||||
(api.get as any).mockResolvedValueOnce({ data: [{
|
||||
id: 'biotin', slug: 'biotin', nameFa: 'بیوتین', description: 'توضیح کامل بیوتین',
|
||||
benefits: ['فایده اول', 'فایده دوم'],
|
||||
}] });
|
||||
(productService.getProducts as any).mockResolvedValueOnce({ data: [{
|
||||
id: 'p-1', slug: 'biotin-tabs', name: 'محصول بیوتین', main_ingredients: ['بیوتین'], image: '/biotin.png',
|
||||
}] });
|
||||
|
||||
const { container } = render(<IngredientWiki />);
|
||||
await waitFor(() => expect(screen.getByRole('button', { name: /بیوتین.*نمایش جزئیات/ })).toBeInTheDocument());
|
||||
|
||||
const trigger = screen.getByRole('button', { name: /بیوتین.*نمایش جزئیات/ });
|
||||
expect(trigger).toHaveAttribute('aria-expanded', 'false');
|
||||
const panelId = trigger.getAttribute('aria-controls');
|
||||
expect(panelId).toBeTruthy();
|
||||
const panel = container.querySelector(`#${panelId}`);
|
||||
expect(panel).toHaveAttribute('role', 'region');
|
||||
expect(panel).toHaveAttribute('aria-labelledby', trigger.id);
|
||||
expect(panel).not.toBeVisible();
|
||||
expect(screen.getByText('توضیح کامل بیوتین')).toBeInTheDocument();
|
||||
expect(screen.getByText('فایده اول')).toBeInTheDocument();
|
||||
expect(screen.getByText('فایده دوم')).toBeInTheDocument();
|
||||
expect(screen.getByText('محصول بیوتین')).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(trigger);
|
||||
expect(trigger).toHaveAttribute('aria-expanded', 'true');
|
||||
expect(panel).toBeVisible();
|
||||
expect(screen.getByText('توضیح کامل بیوتین')).toBeInTheDocument();
|
||||
expect(screen.getByText('محصول بیوتین')).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(trigger);
|
||||
expect(trigger).toHaveAttribute('aria-expanded', 'false');
|
||||
expect(panel).not.toBeVisible();
|
||||
expect(screen.getByText('توضیح کامل بیوتین')).toBeInTheDocument();
|
||||
expect(screen.getByText('محصول بیوتین')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('auto-expands a deep-linked ingredient beyond the first page', async () => {
|
||||
mockSearchParamsGet.mockImplementation((param: string) => param === 'term' ? 'ingredient-13' : null);
|
||||
(productService.getProducts as any).mockResolvedValueOnce({ data: [] });
|
||||
const ingredients = Array.from({ length: 18 }, (_, index) => ({
|
||||
id: `ingredient-${index + 1}`,
|
||||
slug: `ingredient-${index + 1}`,
|
||||
nameFa: `ماده شماره ${index + 1}`,
|
||||
description: `توضیح ماده ${index + 1}`,
|
||||
benefits: [],
|
||||
}));
|
||||
(api.get as any).mockResolvedValueOnce({ data: ingredients });
|
||||
|
||||
render(<IngredientWiki />);
|
||||
|
||||
const targetTrigger = await screen.findByRole('button', { name: /ماده شماره 13.*بستن جزئیات/ });
|
||||
expect(targetTrigger).toHaveAttribute('aria-expanded', 'true');
|
||||
expect(document.getElementById(targetTrigger.getAttribute('aria-controls')!)).toBeVisible();
|
||||
expect(screen.getByText(/نمایش ۱۶ از ۱۸ ماده/)).toBeInTheDocument();
|
||||
expect(document.getElementById('wiki-trigger-ingredient-1')).toHaveAttribute('aria-expanded', 'false');
|
||||
});
|
||||
|
||||
it('keeps an expanded ingredient open after loading another page', async () => {
|
||||
(productService.getProducts as any).mockResolvedValueOnce({ data: [] });
|
||||
const ingredients = Array.from({ length: 18 }, (_, index) => ({
|
||||
id: `ingredient-${index + 1}`,
|
||||
slug: `ingredient-${index + 1}`,
|
||||
nameFa: `ماده شماره ${index + 1}`,
|
||||
description: `توضیح ماده ${index + 1}`,
|
||||
benefits: [],
|
||||
}));
|
||||
(api.get as any).mockResolvedValueOnce({ data: ingredients });
|
||||
|
||||
render(<IngredientWiki />);
|
||||
const firstTrigger = await screen.findByRole('button', { name: /ماده شماره 1.*نمایش جزئیات/ });
|
||||
fireEvent.click(firstTrigger);
|
||||
expect(firstTrigger).toHaveAttribute('aria-expanded', 'true');
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: /مشاهده ۸ ماده بیشتر/ }));
|
||||
expect(firstTrigger).toHaveAttribute('aria-expanded', 'true');
|
||||
expect(screen.getByRole('button', { name: /ماده شماره 9.*نمایش جزئیات/ })).toHaveAttribute('aria-expanded', 'false');
|
||||
expect(screen.getByText(/نمایش ۱۶ از ۱۸ ماده/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
|
||||
it('keeps the mobile breadcrumb close to the top edge', () => {
|
||||
(productService.getProducts as any).mockResolvedValueOnce({ data: [] });
|
||||
(api.get as any).mockResolvedValueOnce({ data: [] });
|
||||
|
||||
Reference in new issue
Block a user