1
1
import React , { useState } from 'react' ;
2
2
3
3
const ClickableLink = ( ) : JSX . Element => {
4
- //initialize showInfo to false
5
- const [ showInfo , setShowInfo ] = useState ( false ) ;
4
+ //visibleKeys is an ojc that tracks which keys are visible or hidden
5
+ // ex
6
+ // {
7
+ // "ThemeProvider": true,
8
+ // "ThemeProvider.ThemeContext1": false
9
+ // }
10
+ const [ visibleKeys , setVisibleKeys ] = useState ( { } ) ;
6
11
7
- const toggleInfo = ( ) => {
8
- //invert the value of showInfo
9
- setShowInfo ( ! showInfo ) ;
12
+ const toggleInfo = ( key :string ) => {
13
+ setVisibleKeys ( ( prev ) => ( {
14
+ ...prev , //copies all current keys and their visibility from prev object
15
+ [ key ] : ! prev [ key ] // adds or updates the key in the visibleKeys object.
16
+ } ) )
10
17
} ;
18
+ const someInfo = {
19
+ ThemeProvider : {
20
+ UserProvider : {
21
+ UserContext : 'some info here' ,
22
+ } ,
23
+ ThemeContext1 : {
24
+ Nested : 'some info here' } ,
25
+ ThemeConext2 : 'some info here'
26
+ } ,
27
+ // UserProvider: {
28
+ // UserContext: 'some info here',
29
+ // }
30
+ }
11
31
32
+
12
33
13
- return (
14
- < div >
15
- < span
16
- style = { { color : 'blue' , cursor : 'pointer' } }
17
- onClick = { toggleInfo }
18
- >
19
- ThemeProvider
20
- </ span >
34
+ //create a function to iterate through the nested structure
35
+
36
+ // const renderNestedObjec = (obj) => {
37
+ // return Object.keys(obj).map((key) => (
38
+ // <div
39
+ // style={{ color: 'blue', cursor: 'pointer' }}
40
+ // onClick={toggleInfo(key)}
41
+ // >
42
+ // <strong>{key}</strong>:{" "}
43
+ // {typeof obj[key] === 'object' ? renderNestedObjec(obj[key]) : <span>{obj[key]}</span> }
44
+ // </div>
45
+ // ))
46
+ // }
47
+ // return (
48
+ // <div>
49
+ // {renderNestedObjec(someInfo)}
50
+ // </div>
51
+ // )
52
+ // <div>
53
+ // {Object.keys(info).map((key) => (
54
+ // <div
55
+ // style={{ color: 'blue', cursor: 'pointer' }}
56
+ // onClick={toggleInfo}
57
+ // >
58
+ // {key}
59
+ // </div>
60
+ // ))}
61
+
21
62
{ /* displayed onClick
22
63
logical And operator: if showInfo is true --> thes result on the righthand side is evaluated and returned
23
64
*/ }
24
- { showInfo && (
25
- < div >
26
- < h1 > ThemeContext</ h1 >
27
- < p >
28
- theme: 'dark', toggleTheme: f(), colors: primary: bdjsbdjsbj
29
- </ p >
30
- </ div >
31
- ) }
65
+ { /* {showInfo && (
66
+
67
+ )} */ }
68
+ // </div>
69
+ // );
70
+
71
+
72
+ // Recursive function to render nested objects
73
+ const renderNestedObjec = ( obj , parentKey = '' ) => { //parentKey is unique keys that represent the hierarchy of the object.
74
+ return Object . keys ( obj ) . map ( ( key ) => {
75
+ const fullKey = parentKey ? `${ parentKey } .${ key } ` : key ; // if parentKey does not exisit --> fullKey = key, else fullKey=ParentKey.Key
76
+ return (
77
+ // each key is rendered as a div
78
+ < div key = { fullKey } style = { { marginLeft : '20px' } } >
79
+ < div
80
+ style = { { color : 'blue' , cursor : 'pointer' } }
81
+ onClick = { ( ) => toggleInfo ( fullKey ) } // Pass the unique key to toggle visibility
82
+ >
83
+ < strong > { key } </ strong >
84
+ </ div >
85
+ { visibleKeys [ fullKey ] && // Check if the key is visible
86
+ ( typeof obj [ key ] === 'object' //check if the value of the key is an object
87
+ ? renderNestedObjec ( obj [ key ] , fullKey ) // Recursive rendering for nested objects
88
+ //if value is not an object
89
+ : < div style = { { marginLeft : '20px' } } > { obj [ key ] } </ div > ) }
32
90
</ div >
33
- ) ;
34
- }
91
+ ) ;
92
+ } ) ;
93
+ } ;
94
+
95
+ return < div > { renderNestedObjec ( someInfo ) } </ div > ;
96
+ } ;
97
+
35
98
36
99
export default ClickableLink ;
0 commit comments