2025-08-04 17:45:44 +02:00
|
|
|
import { forwardRef, type InputHTMLAttributes } from "react";
|
|
|
|
|
import {
|
2025-08-28 18:27:07 +02:00
|
|
|
Accordion,
|
|
|
|
|
AccordionContent,
|
|
|
|
|
AccordionItem,
|
|
|
|
|
AccordionTrigger,
|
2025-08-04 17:45:44 +02:00
|
|
|
} from "~/components/ui/accordion";
|
|
|
|
|
import type { Faq } from "~/i18n/locales";
|
|
|
|
|
import { cn } from "~/lib/utils";
|
|
|
|
|
|
|
|
|
|
interface AccordionCompProps extends InputHTMLAttributes<HTMLDivElement> {
|
2025-08-28 18:27:07 +02:00
|
|
|
texts: Faq[];
|
2025-11-03 18:49:27 +01:00
|
|
|
defaultOpen?: number;
|
2025-08-04 17:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Renders an accordion component with collapsible items.
|
|
|
|
|
*
|
|
|
|
|
* @component
|
|
|
|
|
* @example
|
|
|
|
|
* ```tsx
|
|
|
|
|
* <AccordionComp
|
|
|
|
|
* texts={[
|
|
|
|
|
* { title: "Item 1", description: "Description for Item 1" },
|
|
|
|
|
* { title: "Item 2", description: "Description for Item 2" },
|
|
|
|
|
* { title: "Item 3", description: "Description for Item 3" },
|
|
|
|
|
* ]}
|
|
|
|
|
* className="my-4"
|
|
|
|
|
* />
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
export const AccordionComp = forwardRef<HTMLDivElement, AccordionCompProps>(
|
2025-11-03 18:49:27 +01:00
|
|
|
({ texts, defaultOpen, className }, ref) => {
|
2025-08-28 18:27:07 +02:00
|
|
|
if (!texts || texts.length === 0) {
|
|
|
|
|
return null; // Return null if no texts are provided
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={cn("mx-auto flex max-w-xl justify-center", className)}
|
|
|
|
|
ref={ref}
|
|
|
|
|
>
|
2025-11-03 18:49:27 +01:00
|
|
|
<Accordion
|
|
|
|
|
className="w-full"
|
|
|
|
|
collapsible
|
|
|
|
|
defaultValue={
|
|
|
|
|
defaultOpen !== undefined ? `item-${defaultOpen}` : undefined
|
|
|
|
|
}
|
|
|
|
|
type="single"
|
|
|
|
|
>
|
2025-08-28 18:27:07 +02:00
|
|
|
{texts.map((text, index) => (
|
2025-11-14 17:21:21 +01:00
|
|
|
<AccordionItem
|
|
|
|
|
// biome-ignore lint/suspicious/noArrayIndexKey: <index is safe here>
|
|
|
|
|
key={`ac-item-${index}`}
|
|
|
|
|
value={`item-${index}`}
|
|
|
|
|
>
|
2025-12-04 17:16:49 +01:00
|
|
|
<AccordionTrigger className="cursor-pointer gap-4 text-left text-md">
|
2025-08-28 18:27:07 +02:00
|
|
|
{text.title}
|
|
|
|
|
</AccordionTrigger>
|
|
|
|
|
<AccordionContent className="text-base">
|
|
|
|
|
{text.description}
|
|
|
|
|
</AccordionContent>
|
|
|
|
|
</AccordionItem>
|
|
|
|
|
))}
|
|
|
|
|
</Accordion>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
},
|
2025-08-04 17:45:44 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
AccordionComp.displayName = "AccordionComp";
|