import type { Meta, StoryObj } from '@storybook/react'; import { Accordion } from './Accordion'; import { AccordionItem } from './AccordionItem'; import { useArgs } from '@storybook/preview-api'; const meta: Meta = { title: 'Components/Accordion', component: Accordion, subcomponents: { AccordionItem }, args: { bordered: true, }, }; export default meta; type Story = StoryObj; export const Default: Story = { args: { children: [

We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.

,

A well regulated Militia, being necessary to the security of a free State, the right of the people to keep and bear Arms, shall not be infringed.

, ], }, }; export const Controlled: Story = { args: { bordered: true, openItems: [0], } as any, render: function Component(args) { const [{ openItems }, setOpenItems] = useArgs(); const handleChange = function (index: number) { if (openItems?.includes(index)) { setOpenItems({ openItems: openItems?.filter((item: number) => item !== index) }); } else { setOpenItems({ openItems: openItems && [...openItems, index].sort() }); } }; return ( handleChange(0)} >

In a controlled accordion, the accordion panel's open state is controlled by the application, by passing isControlledOpen and onChange props. The isControlledOpen boolean flag sets the panel to an open or close state.

handleChange(1)} >

In the args below you will notice the openItems array being updated with the changes set using isControlledOpen and onChange

); }, };