Skip to content

Commit

Permalink
feat: add autoClose prop to accordion
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandar-r committed Mar 26, 2024
1 parent 24e044a commit 9217095
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/core/Accordion/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ type AccordionRowProps = {
active: boolean;
last: boolean;
name: string;
index: number;
index;
children: ReactNode;
arrowIcon: boolean;
activeIndex: number;
setActiveIndex: (index: number) => void;
};

type AccordionProps = {
export type AccordionProps = {
data: AccordionData[];
arrowIcon: boolean;
topBorder: boolean;
bottomBorder: boolean;
id: string;
autoClose?: boolean;
};

const AccordionRow = ({
Expand All @@ -40,11 +40,7 @@ const AccordionRow = ({
}: AccordionRowProps) => {
let iconActive: JSX.Element, iconInactive: JSX.Element;
const handleSetIndex = () => {
if (active) {
setActiveIndex(null);
} else {
setActiveIndex(index);
}
setActiveIndex(index);
};

if (arrowIcon) {
Expand Down Expand Up @@ -107,8 +103,23 @@ const Accordion = ({
topBorder,
bottomBorder,
arrowIcon,
autoClose,
}: AccordionProps) => {
const [activeIndex, setActiveIndex] = useState(null);
const [activeIndexes, setActiveIndexes] = useState([]);

const handleSetIndex = (index: number) => {
const currentIndexIsActive = activeIndexes.includes(index);

if (autoClose) {
setActiveIndexes(currentIndexIsActive ? [] : [index]);
} else {
setActiveIndexes(
currentIndexIsActive
? activeIndexes.filter((i) => i !== index)
: [...activeIndexes, index]
);
}
};

return (
<div className="ui-grid-mx max-w-screen-sm sm:mx-auto" id={id}>
Expand All @@ -119,12 +130,11 @@ const Accordion = ({
name={item.name}
arrowIcon={arrowIcon}
index={currentIndex}
activeIndex={activeIndex}
last={data.length === currentIndex + 1}
topBorder={topBorder && currentIndex === 0}
bottomBorder={bottomBorder && data.length === currentIndex + 1}
active={activeIndex === currentIndex}
setActiveIndex={setActiveIndex}
active={activeIndexes.includes(currentIndex)}
setActiveIndex={handleSetIndex}
>
{item.content}
</AccordionRow>
Expand Down

0 comments on commit 9217095

Please sign in to comment.