import { useCallback, useState } from "react"; import { Box } from "../box"; import { Text } from "../text"; import { Button } from "../button"; import { commonProps } from "../../utils/storybook"; import { Accordion } from "./"; import type { Meta, StoryObj } from "@storybook/react"; import type { AccordionItemsProps } from "."; const mockAccordionItem: AccordionItemsProps = [ { value: "1", header: Header 1, body: ( Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam ), }, { value: "2", header: Header 2, body: ( Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam ), }, { value: "3", header: Header 3, body: ( Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam ), }, ]; const meta: Meta = { title: "Accordion", component: Accordion, tags: ["autodocs"], argTypes: { type: { options: ["single", "multiple"], control: { type: "select" }, table: { defaultValue: { summary: "single", }, }, description: "Determines whether one or multiple items can be opened at the same time.", }, items: { control: false, table: { type: { summary: `Array & { header: React.ReactElement; body: React.ReactElement; }>`, }, }, }, asChild: { description: "Change the default rendered element for the one passed as a child, merging their props and behavior.", control: false, table: { defaultValue: { summary: "false", }, }, }, headerProps: { table: { category: "Anatomy Props", type: { summary: "React.ComponentProps", }, }, }, triggerProps: { table: { category: "Anatomy Props", type: { summary: "React.ComponentProps", }, }, }, contentProps: { table: { category: "Anatomy Props", type: { summary: "React.ComponentProps", }, }, }, ...commonProps, }, args: { type: "single", items: mockAccordionItem }, }; export default meta; type Story = StoryObj; export const Uncontrolled: Story = { render: (args) => ( ), }; export const Controlled: Story = { render: (args) => { const [current, setCurrent] = useState(args.type === "single" ? "" : []); const onClick = useCallback(() => { setCurrent((prev) => { const parsedPrev = Number(prev); const nextValue = isNaN(parsedPrev) ? 1 : parsedPrev < 3 ? parsedPrev + 1 : 0; return nextValue ? nextValue.toString() : ""; }); }, []); const toggleValue = (value: string) => { setCurrent((prev) => { const index = prev.indexOf(value); if (index === -1) { return [...prev, value]; } const copy = [...prev]; copy.splice(index, 1); return copy; }); }; return ( {/* @ts-expect-error value props will match `type` */} {args.type === "single" ? ( ) : ( <> )} ); }, };