Accordion Component
Build an accordion where sections expand/collapse on click. Implement both single-open mode (only one section open at a time) and multi-open mode (multiple sections can be open simultaneously).
Requirements
- Render a list of items, each with a header and collapsible body
- Click a header to expand/collapse its body
- Single-open mode: opening one section collapses any previously open section
- Multi-open mode: any number of sections can be open at once
- Toggle between single-open and multi-open modes at runtime
- Smooth open/close animation
Key Patterns
openIndex: number | null (single-open)openItems: Set<number> (multi-open)CSS max-height transitionMUI Collapse componentToggle pattern with Set
Important
Interview Tip
For single-open: store openIndex (number|null). Clicking an open item sets it to null; clicking a closed item sets it to that index. For multi-open: store a Set<number>. Toggle = if Set has index, delete it; else add it. Spread into a new Set to trigger re-render: setOpen(new Set(prev).add/delete).