Skip to content

Commit

Permalink
feat(components/standalone-accordion): make controlled
Browse files Browse the repository at this point in the history
  • Loading branch information
psychedelicious committed Jan 19, 2024
1 parent 6a55da4 commit 4a32205
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/components/accordion/standalone-accordion.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useDisclosure } from '@chakra-ui/react';
import type { Meta, StoryObj } from '@storybook/react';

import { Tab, TabList, TabPanel, TabPanels, Tabs } from '../tabs';
Expand All @@ -15,12 +16,14 @@ export default meta;
type Story = StoryObj<typeof StandaloneAccordion>;

const Component = (props: StandaloneAccordionProps) => {
const { isOpen, onToggle } = useDisclosure();
return (
<StandaloneAccordion
{...props}
label="The Best Flavours of Banana Sushi"
badges={['Yum', 'Gourmet', 'Barf']}
defaultIsOpen
isOpen={isOpen}
onToggle={onToggle}
>
<Tabs variant="collapse">
<TabList>
Expand Down
20 changes: 15 additions & 5 deletions lib/components/accordion/standalone-accordion.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import type { PropsWithChildren } from 'react';
import { memo } from 'react';
import type { MouseEventHandler, PropsWithChildren } from 'react';
import { memo, useCallback } from 'react';

import { AccordionButton } from './accordion-button';
import { Accordion, AccordionItem, AccordionPanel } from './wrapper';

export type StandaloneAccordionProps = PropsWithChildren<{
label: string;
badges?: (string | number)[];
defaultIsOpen?: boolean;
isOpen: boolean;
onToggle: () => void;
}>;

export const StandaloneAccordion = memo((props: StandaloneAccordionProps) => {
const onClick = useCallback<MouseEventHandler<HTMLButtonElement>>(
(e) => {
e.preventDefault();
props.onToggle();
},
[props]
);
return (
<Accordion allowToggle defaultIndex={props.defaultIsOpen ? 0 : undefined}>
<Accordion index={props.isOpen ? 0 : undefined} allowToggle>
<AccordionItem>
<AccordionButton badges={props.badges}>{props.label}</AccordionButton>
<AccordionButton badges={props.badges} onClick={onClick}>
{props.label}
</AccordionButton>
<AccordionPanel>{props.children}</AccordionPanel>
</AccordionItem>
</Accordion>
Expand Down

0 comments on commit 4a32205

Please sign in to comment.