Skip to content

Commit 1bad79f

Browse files
committed
Improve Accordion
1 parent 4efdd39 commit 1bad79f

File tree

5 files changed

+43
-37
lines changed

5 files changed

+43
-37
lines changed

src/Components/Accordion/Accordion.stories.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,23 @@ const sections = [
182182
];
183183

184184
export const Primary = () => {
185-
const [expandedSections, setExpandedSections] = useState<number[]>([]);
185+
const [expandedSections, setExpandedSections] = useState<number[]>([
186+
sections[0].id
187+
]);
186188

187189
return (
188190
<Accordion
189191
className='w-[500px]'
190192
sections={sections}
191-
expandedSections={expandedSections}
192-
setExpandedSections={setExpandedSections}
193+
expanded={expandedSections}
194+
setExpanded={(expanded: number[]) => {
195+
console.log(
196+
"🚀 ─── file: Accordion.stories.tsx:196 ─── Primary ─── expanded:",
197+
expanded
198+
);
199+
200+
setExpandedSections(expanded);
201+
}}
193202
onSectionItemClick={sectionItem => {
194203
action("onSectionItemClick")(sectionItem);
195204
}}

src/Components/Accordion/Accordion.test.tsx

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ describe("Accordion component", () => {
2828
const { asFragment } = render(
2929
<Accordion
3030
sections={mockSections}
31-
setExpandedSections={() => null}
32-
expandedSections={[1]}
31+
setExpanded={() => null}
32+
expanded={[1]}
3333
onSectionItemClick={vi.fn()}
3434
/>
3535
);
3636
expect(asFragment()).toMatchSnapshot();
3737
});
3838

3939
test("renders all sections and items correctly", () => {
40-
const setExpandedSections = vi.fn();
40+
const setExpanded = vi.fn();
4141
render(
4242
<Accordion
4343
sections={mockSections}
44-
setExpandedSections={setExpandedSections}
45-
expandedSections={[1]}
44+
setExpanded={setExpanded}
45+
expanded={[1]}
4646
onSectionItemClick={vi.fn()}
4747
/>
4848
);
@@ -54,32 +54,29 @@ describe("Accordion component", () => {
5454
});
5555

5656
test("expands and collapses sections on click", () => {
57-
const setExpandedSections = vi.fn();
57+
const setExpanded = vi.fn();
5858

5959
render(
6060
<Accordion
6161
sections={mockSections}
62-
setExpandedSections={setExpandedSections}
63-
expandedSections={[1]}
62+
setExpanded={setExpanded}
63+
expanded={[mockSections[0].id]}
6464
onSectionItemClick={vi.fn()}
6565
/>
6666
);
6767

6868
fireEvent.click(screen.getByText("Section 1"));
69-
expect(setExpandedSections).toHaveBeenCalledWith([1]);
70-
71-
fireEvent.click(screen.getByText("Section 1"));
72-
expect(setExpandedSections).toHaveBeenCalledWith([1]);
69+
expect(setExpanded).toHaveBeenCalledWith([]);
7370
});
7471

7572
test("handles section item click events", () => {
7673
const onSectionItemClick = vi.fn();
7774
render(
7875
<Accordion
7976
sections={mockSections}
80-
setExpandedSections={vi.fn()}
77+
setExpanded={vi.fn()}
8178
onSectionItemClick={onSectionItemClick}
82-
expandedSections={[1]}
79+
expanded={[1]}
8380
/>
8481
);
8582

@@ -93,8 +90,8 @@ describe("Accordion component", () => {
9390
sections={mockSections}
9491
className='custom-class'
9592
sectionTitleClassName='custom-title-class'
96-
setExpandedSections={vi.fn()}
97-
expandedSections={[]}
93+
setExpanded={vi.fn()}
94+
expanded={[]}
9895
onSectionItemClick={vi.fn()}
9996
/>
10097
);

src/Components/Accordion/__snapshots__/Accordion.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exports[`Accordion component > renders correctly 1`] = `
66
class=""
77
>
88
<div
9-
class="border border-primary-text-color p-2 mb-0 border-b-0"
9+
class="border border-primary-text-color p-2 mb-0 border-b-0"
1010
role="presentation"
1111
>
1212
<h3
@@ -78,7 +78,7 @@ exports[`Accordion component > renders correctly 1`] = `
7878
</ul>
7979
</div>
8080
<div
81-
class="border border-primary-text-color p-2 "
81+
class="border border-primary-text-color p-2"
8282
role="presentation"
8383
>
8484
<h3

src/Components/Accordion/index.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ const Accordion: FC<IAccordionProps> = ({
1212
itemsLabelText,
1313
onSectionItemClick,
1414
selectedItemId = null,
15-
expandedSections,
16-
setExpandedSections
15+
expanded,
16+
setExpanded
1717
}) => {
18-
const toggleExpandSection = (sectionId: number) => {
19-
if (expandedSections.includes(sectionId)) {
20-
setExpandedSections(expandedSections.filter(id => id !== sectionId));
18+
const toggleExpand = (sectionId: number) => {
19+
if (expanded.includes(sectionId)) {
20+
setExpanded(expanded.filter(id => id !== sectionId));
2121
} else {
22-
setExpandedSections([...expandedSections, sectionId]);
22+
setExpanded([...expanded, sectionId]);
2323
}
2424
};
2525

@@ -31,7 +31,7 @@ const Accordion: FC<IAccordionProps> = ({
3131
{sections.map((section, idx) => (
3232
<div
3333
key={section.id}
34-
className={classNames("border border-primary-text-color p-2 ", {
34+
className={classNames("border border-primary-text-color p-2", {
3535
"mb-0 border-b-0": idx !== sections.length - 1
3636
})}
3737
role='presentation'
@@ -42,21 +42,21 @@ const Accordion: FC<IAccordionProps> = ({
4242
sectionTitleClassName,
4343
{
4444
"mb-2 border-b border-primary-text-color pb-2":
45-
expandedSections.includes(section.id)
45+
expanded.includes(section.id)
4646
}
4747
)}
48-
onClick={() => toggleExpandSection(section.id)}
48+
onClick={() => toggleExpand(section.id)}
4949
onKeyDown={e => {
5050
if (e.key === "Enter" || e.key === " ") {
51-
toggleExpandSection(section.id);
51+
toggleExpand(section.id);
5252
}
5353
}}
5454
tabIndex={0}
5555
role='button'
56-
aria-expanded={expandedSections.includes(section.id)}
56+
aria-expanded={expanded.includes(section.id)}
5757
>
5858
<div className='flex items-center gap-1 overflow-hidden text-ellipsis whitespace-nowrap text-base'>
59-
{expandedSections.includes(section.id) ? (
59+
{expanded.includes(section.id) ? (
6060
<IconExpandLess className='w-8 cursor-pointer fill-primary-text-color hover:fill-primary' />
6161
) : (
6262
<IconExpandMore className='w-8 cursor-pointer fill-primary-text-color hover:fill-primary' />
@@ -71,7 +71,7 @@ const Accordion: FC<IAccordionProps> = ({
7171
</div>
7272
)}
7373
</h3>
74-
{expandedSections.indexOf(section.id) >= 0 && (
74+
{expanded.includes(section.id) && (
7575
<ul
7676
className='ml-[10px] leading-8'
7777
role='region'

src/Components/Accordion/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface IAccordionProps {
1818
sectionTitleClassName?: string;
1919
itemsLabelText?: string;
2020
onSectionItemClick: (item: ISectionItem) => void;
21-
selectedItemId?: number;
22-
expandedSections: number[];
23-
setExpandedSections: (sections: number[]) => void;
21+
selectedItemId?: number | null;
22+
expanded: number[];
23+
setExpanded: (sections: number[]) => void;
2424
}

0 commit comments

Comments
 (0)