-
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.
- Loading branch information
1 parent
57b487d
commit 7610ea5
Showing
5 changed files
with
140 additions
and
98 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
15 changes: 2 additions & 13 deletions
15
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,22 +1,11 @@ | ||
import useLinked from './useLinked'; | ||
import { IAccordionSectionsProps } from '.'; | ||
|
||
import React, { FC, FunctionComponentElement } from 'react'; | ||
|
||
export interface IAccordionRootProps { | ||
children?: React.ReactNode; | ||
linked?: boolean; | ||
openSection?: number; | ||
children?: FunctionComponentElement<IAccordionSectionsProps>; | ||
} | ||
|
||
export const AccordionRoot: FC<IAccordionRootProps> = ({ | ||
children, | ||
linked, | ||
openSection, | ||
}) => { | ||
if (linked) { | ||
const { setUsingLinked } = useLinked(openSection); | ||
setUsingLinked(true); | ||
} | ||
export const AccordionRoot: FC<IAccordionRootProps> = ({ children }) => { | ||
return <div data-testid="kda-accordion-wrapper">{children}</div>; | ||
}; |
63 changes: 16 additions & 47 deletions
63
packages/libs/react-ui/src/components/Accordion/AccordionSection.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,64 +1,33 @@ | ||
import { | ||
accordionContentWrapperClass, | ||
accordionSectionClass, | ||
accordionTitleClass, | ||
accordionTitleVariants, | ||
toggleButtonClass, | ||
} from './Accordion.css'; | ||
import useLinked from './useLinked'; | ||
import { toggleButtonClass } from './Accordion.css'; | ||
|
||
import { SystemIcon } from '@components/Icon'; | ||
import classNames from 'classnames'; | ||
import React, { FC, useState } from 'react'; | ||
import React, { FC } from 'react'; | ||
|
||
export interface IAccordionSectionProps { | ||
title: React.ReactNode; | ||
children: React.ReactNode; | ||
onToggle?: () => void; | ||
onOpen?: () => void; | ||
isOpen?: boolean; | ||
onClose?: () => void; | ||
onOpen?: () => void; | ||
title: React.ReactNode; | ||
} | ||
|
||
export const AccordionSection: FC<IAccordionSectionProps> = ({ | ||
title, | ||
children, | ||
onOpen, | ||
onClose, | ||
isOpen, | ||
}) => { | ||
const { usingLinked, activeSection, setActiveSection } = useLinked(); | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
const onToggle = (): void => (isOpen ? onClose?.() : onOpen?.()); | ||
const handleClick = (): void => { | ||
setIsOpen(!isOpen); | ||
}; | ||
|
||
return ( | ||
<div className={accordionSectionClass} data-testid="kda-accordion-section"> | ||
<div | ||
data-testid="kda-accordion-section-title" | ||
onClick={() => { | ||
handleClick(); | ||
onToggle(); | ||
}} | ||
className={classNames( | ||
accordionTitleClass, | ||
accordionTitleVariants[isOpen ? 'opened' : 'closed'], | ||
)} | ||
> | ||
<span>{title}</span> | ||
<div> | ||
<span>{title}</span> | ||
|
||
<button | ||
role="button" | ||
className={classNames(toggleButtonClass, { | ||
isOpen, | ||
})} | ||
> | ||
<SystemIcon.Close size="sm" /> | ||
</button> | ||
</div> | ||
|
||
{isOpen && <div className={accordionContentWrapperClass}>{children}</div>} | ||
<button | ||
role="button" | ||
className={classNames(toggleButtonClass, { | ||
isOpen, | ||
})} | ||
> | ||
<SystemIcon.Close size="sm" /> | ||
</button> | ||
</div> | ||
); | ||
}; |
94 changes: 88 additions & 6 deletions
94
packages/libs/react-ui/src/components/Accordion/AccordionSections.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,19 +1,101 @@ | ||
import { | ||
accordionContentWrapperClass, | ||
accordionSectionClass, | ||
accordionTitleClass, | ||
accordionTitleVariants, | ||
} from './Accordion.css'; | ||
import useLinked from './useLinked'; | ||
import { IAccordionSectionProps } from '.'; | ||
|
||
import React, { FC, FunctionComponentElement } from 'react'; | ||
import classNames from 'classnames'; | ||
import React, { | ||
FC, | ||
FunctionComponentElement, | ||
useCallback, | ||
useEffect, | ||
} from 'react'; | ||
|
||
export interface IAccordionSectionsProps { | ||
// children?: FunctionComponentElement<IAccordionSectionProps>[]; | ||
children?: React.ReactNode; | ||
children?: FunctionComponentElement<IAccordionSectionProps>[]; | ||
linked?: boolean; | ||
openSection?: number; | ||
} | ||
|
||
export const AccordionSections: FC<IAccordionSectionsProps> = ({ | ||
children, | ||
linked, | ||
openSection = 0, | ||
linked = false, | ||
openSection, | ||
}) => { | ||
return <div data-testid="kda-accordion-sections">{children}</div>; | ||
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> | ||
); | ||
}; |
10 changes: 5 additions & 5 deletions
10
packages/libs/react-ui/src/components/Accordion/useLinked.ts
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,16 +1,16 @@ | ||
import { useState } from 'react'; | ||
|
||
interface IUseLinkedReturn { | ||
activeSection: number; | ||
setActiveSection: React.Dispatch<React.SetStateAction<number>>; | ||
openSections: number[]; | ||
setOpenSections: React.Dispatch<React.SetStateAction<number[]>>; | ||
usingLinked: boolean; | ||
setUsingLinked: React.Dispatch<React.SetStateAction<boolean>>; | ||
} | ||
|
||
const useLinked = (openSection = 0): IUseLinkedReturn => { | ||
const useLinked = (openSection = -1): IUseLinkedReturn => { | ||
const [usingLinked, setUsingLinked] = useState(false); | ||
const [activeSection, setActiveSection] = useState(openSection); | ||
return { activeSection, setActiveSection, usingLinked, setUsingLinked }; | ||
const [openSections, setOpenSections] = useState([openSection]); | ||
return { openSections, setOpenSections, usingLinked, setUsingLinked }; | ||
}; | ||
|
||
export default useLinked; |