-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove unneccesary accordion wrapper
- Loading branch information
1 parent
73375ac
commit 8d0dba7
Showing
3 changed files
with
109 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 90 additions & 5 deletions
95
packages/libs/react-ui/src/components/Accordion/Accordion.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,97 @@ | ||
import { IAccordionSectionsProps } from '.'; | ||
import { | ||
accordionContentWrapperClass, | ||
accordionSectionClass, | ||
accordionTitleClass, | ||
accordionTitleVariants, | ||
} from './Accordion.css'; | ||
import useLinked from './useLinked'; | ||
import { IAccordionSectionProps } from '.'; | ||
|
||
import classNames from 'classnames'; | ||
import type { FC, FunctionComponentElement } from 'react'; | ||
import React from 'react'; | ||
import React, { useCallback, useEffect } from 'react'; | ||
|
||
export interface IAccordionRootProps { | ||
children?: FunctionComponentElement<IAccordionSectionsProps>; | ||
children?: FunctionComponentElement<IAccordionSectionProps>[]; | ||
linked?: boolean; | ||
openSection?: number; | ||
} | ||
|
||
export const AccordionRoot: FC<IAccordionRootProps> = ({ children }) => { | ||
return <div data-testid="kda-accordion-wrapper">{children}</div>; | ||
export const AccordionRoot: FC<IAccordionRootProps> = ({ | ||
children, | ||
linked = false, | ||
openSection, | ||
}) => { | ||
const { usingLinked, setUsingLinked, openSections, setOpenSections } = | ||
useLinked(openSection); | ||
|
||
useEffect(() => { | ||
setUsingLinked(linked); | ||
if (linked && openSections.length > 1) { | ||
const lastOpen = openSections.pop() || -1; | ||
setOpenSections([lastOpen]); | ||
} | ||
}, [linked]); | ||
|
||
const handleToggleSection = useCallback( | ||
( | ||
index: number, | ||
{ onOpen, onClose }: Pick<IAccordionSectionProps, 'onOpen' | 'onClose'>, | ||
): void => { | ||
const isOpen = openSections.includes(index); | ||
if (isOpen) { | ||
setOpenSections(openSections.filter((i) => i !== index)); | ||
onClose?.(); | ||
} else { | ||
setOpenSections(usingLinked ? [index] : [...openSections, index]); | ||
onOpen?.(); | ||
} | ||
}, | ||
[openSections, usingLinked], | ||
); | ||
|
||
return ( | ||
<div data-testid="kda-accordion-sections"> | ||
{React.Children.map(children, (section, sectionIndex) => ( | ||
<section | ||
className={accordionSectionClass} | ||
data-testid="kda-accordion-section" | ||
> | ||
<div | ||
data-testid="kda-accordion-section-title" | ||
onClick={() => | ||
handleToggleSection(sectionIndex, { | ||
onOpen: section?.props.onOpen, | ||
onClose: section?.props.onClose, | ||
}) | ||
} | ||
className={classNames( | ||
accordionTitleClass, | ||
accordionTitleVariants[ | ||
openSections.includes(sectionIndex) ? 'opened' : 'closed' | ||
], | ||
)} | ||
> | ||
{React.cloneElement( | ||
section as React.ReactElement< | ||
HTMLElement | IAccordionSectionProps, | ||
| string | ||
| React.JSXElementConstructor< | ||
JSX.Element & IAccordionSectionProps | ||
> | ||
>, | ||
{ | ||
isOpen: openSections.includes(sectionIndex), | ||
}, | ||
)} | ||
</div> | ||
{openSections.includes(sectionIndex) && section && ( | ||
<div className={accordionContentWrapperClass}> | ||
{section.props.children} | ||
</div> | ||
)} | ||
</section> | ||
))} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters