File tree Expand file tree Collapse file tree 5 files changed +109
-0
lines changed
React-Day/0002. Accordion Component with All Panel Open Expand file tree Collapse file tree 5 files changed +109
-0
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " accordion" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " " ,
5
+ "keywords" : [],
6
+ "main" : " src/index.js" ,
7
+ "dependencies" : {
8
+ "react" : " 16.12.0" ,
9
+ "react-dom" : " 16.12.0" ,
10
+ "react-scripts" : " 3.0.1"
11
+ },
12
+ "devDependencies" : {
13
+ "typescript" : " 3.8.3"
14
+ },
15
+ "scripts" : {
16
+ "start" : " react-scripts start" ,
17
+ "build" : " react-scripts build" ,
18
+ "test" : " react-scripts test --env=jsdom" ,
19
+ "eject" : " react-scripts eject"
20
+ },
21
+ "browserslist" : [
22
+ " >0.2%" ,
23
+ " not dead" ,
24
+ " not ie <= 11" ,
25
+ " not op_mini all"
26
+ ]
27
+ }
Original file line number Diff line number Diff line change
1
+ import React , { useState } from "react" ;
2
+ import "./styles.css" ;
3
+
4
+ function Accordion ( props ) {
5
+ const [ panelOpenState , setPanelState ] = useState ( [ ] ) ;
6
+
7
+ const togglePanel = ( index ) => {
8
+ setPanelState ( {
9
+ ...panelOpenState ,
10
+ [ index ] : ! panelOpenState [ index ]
11
+ } ) ;
12
+ } ;
13
+
14
+ return (
15
+ < React . Fragment >
16
+ { props . data . map ( ( collapsible , index ) => {
17
+ return (
18
+ < div className = "panel" key = { index } >
19
+ < div className = "header" onClick = { ( ) => togglePanel ( index ) } >
20
+ { collapsible . heading }
21
+ </ div >
22
+ { panelOpenState [ index ] === true && (
23
+ < div className = "content" > { collapsible . content } </ div >
24
+ ) }
25
+ </ div >
26
+ ) ;
27
+ } ) }
28
+ </ React . Fragment >
29
+ ) ;
30
+ }
31
+
32
+ export default Accordion ;
Original file line number Diff line number Diff line change
1
+ import React from "react" ;
2
+ import Accordion from "./Accordion" ;
3
+
4
+ const data = [
5
+ { heading : "Heading 1" , content : "Content of first Panel" } ,
6
+ { heading : "Heading 2" , content : "Content of first Pane2" } ,
7
+ { heading : "Heading 3" , content : "Content of first Pane3" } ,
8
+ { heading : "Heading 4" , content : "Content of first Pane4" } ,
9
+ { heading : "Heading 5" , content : "Content of first Pane5" }
10
+ ] ;
11
+
12
+ export default function App ( ) {
13
+ return (
14
+ < div className = "App" >
15
+ < Accordion data = { data } />
16
+ </ div >
17
+ ) ;
18
+ }
Original file line number Diff line number Diff line change
1
+ import React from "react" ;
2
+ import ReactDOM from "react-dom" ;
3
+
4
+ import App from "./App" ;
5
+
6
+ const rootElement = document . getElementById ( "root" ) ;
7
+ ReactDOM . render (
8
+ < React . StrictMode >
9
+ < h2 > Accordion Example</ h2 >
10
+ < br />
11
+ < App />
12
+ </ React . StrictMode > ,
13
+ rootElement
14
+ ) ;
Original file line number Diff line number Diff line change
1
+ .App {
2
+ font-family : sans-serif;
3
+ }
4
+
5
+ .header {
6
+ cursor : pointer;
7
+ padding : 15px ;
8
+ background-color : skyblue;
9
+ }
10
+
11
+ .content {
12
+ border : solid 1px skyblue;
13
+ padding : 15px ;
14
+ }
15
+
16
+ .panel {
17
+ padding-bottom : 10px ;
18
+ }
You can’t perform that action at this time.
0 commit comments