File tree Expand file tree Collapse file tree 2 files changed +26
-29
lines changed
React-Day/0001. Accordion Component with Single Panel Open/src Expand file tree Collapse file tree 2 files changed +26
-29
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ import React , { useState } from "react" ;
2
+ import { IAccordionData } from "../App" ;
3
+ import "../styles.css" ;
4
+
5
+ interface IAccordionProps {
6
+ data : IAccordionData [ ] ;
7
+ }
8
+
9
+ export default function Accordion ( { data } : IAccordionProps ) {
10
+ const [ openPanelIndex , setOpenPanelIndex ] = useState < number | null > ( null ) ;
11
+
12
+ function togglePanel ( index : number ) {
13
+ setOpenPanelIndex ( openPanelIndex === index ? null : index ) ;
14
+ }
15
+
16
+ return (
17
+ < div >
18
+ { data . map ( ( item , index ) => (
19
+ < div key = { index } >
20
+ < div onClick = { ( ) => togglePanel ( index ) } > { item . heading } </ div >
21
+ { openPanelIndex === index && < div > { item . content } </ div > }
22
+ </ div >
23
+ ) ) }
24
+ </ div >
25
+ ) ;
26
+ }
You can’t perform that action at this time.
0 commit comments