Skip to content

Commit

Permalink
fix: sync accordion open items with default values
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiehenson committed Nov 21, 2024
1 parent f634a72 commit a6e1fa5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions src/core/Accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,18 @@ const Accordion = ({
options,
...props
}: AccordionProps) => {
const [openRowValues, setOpenRowValues] = useState<string | string[]>([]);
const openIndexes = useMemo(() => {
const indexValues = data.map((_, i) => `accordion-item-${i}`);
return options?.fullyOpen
? indexValues
: indexValues.filter((_, index) =>
options?.defaultOpenIndexes?.includes(index),
);
}, [options?.defaultOpenIndexes, options?.fullyOpen, data.length]);

const [openRowValues, setOpenRowValues] = useState<string | string[]>(
openIndexes,
);
const innerAccordion = data.map((item, index) => (
<AccordionRow
key={item.name}
Expand All @@ -166,15 +177,6 @@ const Accordion = ({
</AccordionRow>
));

const openIndexes = useMemo(() => {
const indexValues = data.map((_, i) => `accordion-item-${i}`);
return options?.fullyOpen
? indexValues
: indexValues.filter((_, index) =>
options?.defaultOpenIndexes?.includes(index),
);
}, [options?.defaultOpenIndexes, options?.fullyOpen, data.length]);

return (
<div {...props}>
{options?.autoClose ? (
Expand Down
2 changes: 1 addition & 1 deletion src/core/Accordion/Accordion.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const SelectableHeaders = {
render: () =>
AccordionPresentation({
data: dataWithIcons,
options: { selectable: true },
options: { selectable: true, defaultOpenIndexes: [0] },
}),
parameters: {
docs: {
Expand Down

0 comments on commit a6e1fa5

Please sign in to comment.