File tree Expand file tree Collapse file tree 4 files changed +160
-1
lines changed
2024 Prep/machine_coding/react/src Expand file tree Collapse file tree 4 files changed +160
-1
lines changed Original file line number Diff line number Diff line change
1
+ import { useState } from 'react'
2
+
3
+ interface IFileExplorerData {
4
+ name : string
5
+ type : string
6
+ children ?: IChildren [ ] | null
7
+ }
8
+ export interface IChildren {
9
+ name : string
10
+ type : string
11
+ children ?: IChildren [ ] | null
12
+ }
13
+
14
+ export default function FileExplorer ( { data } : { data : IFileExplorerData } ) {
15
+ const { name = '' , children, type = '' } = data
16
+ const [ collapse , setCollapse ] = useState ( false )
17
+ const toggleCollapse = ( ) => setCollapse ( ! collapse )
18
+
19
+ if ( type === 'file' ) {
20
+ return < div > 📄 { name } </ div >
21
+ }
22
+
23
+ return (
24
+ < div
25
+ style = { {
26
+ width : '100%' ,
27
+ } }
28
+ >
29
+ < div
30
+ style = { {
31
+ marginTop : 5 ,
32
+ display : 'flex' ,
33
+ flexDirection : 'column' ,
34
+ } }
35
+ >
36
+ { /* Render top level parent i.e folder */ }
37
+ < div
38
+ style = { {
39
+ marginLeft : 7 ,
40
+ border : '1px solid white' ,
41
+ } }
42
+ onClick = { toggleCollapse }
43
+ >
44
+ 📁 { name }
45
+ </ div >
46
+ { /* Render Children/Items inside the folder */ }
47
+ { collapse && (
48
+ < div
49
+ style = { {
50
+ paddingLeft : 20 ,
51
+ } }
52
+ >
53
+ { children &&
54
+ children . map ( ( exp : IFileExplorerData ) => {
55
+ return < FileExplorer data = { exp } />
56
+ } ) }
57
+ </ div >
58
+ ) }
59
+ </ div >
60
+ </ div >
61
+ )
62
+ }
Original file line number Diff line number Diff line change 1
1
import Loader from './Loader'
2
+ import InfiniteScroll from './InfiniteScroll'
3
+ import FileExplorer from './FileExplorer'
2
4
3
- export { Loader }
5
+ export { Loader , FileExplorer , InfiniteScroll }
Original file line number Diff line number Diff line change
1
+ export const data = {
2
+ name : 'Root' ,
3
+ type : 'folder' ,
4
+ children : [
5
+ {
6
+ name : 'Documents' ,
7
+ type : 'folder' ,
8
+ children : [
9
+ {
10
+ name : 'Work' ,
11
+ type : 'folder' ,
12
+ children : [
13
+ {
14
+ name : 'Reports' ,
15
+ type : 'folder' ,
16
+ children : [
17
+ {
18
+ name : 'Q1' ,
19
+ type : 'file' ,
20
+ } ,
21
+ {
22
+ name : 'Q2' ,
23
+ type : 'file' ,
24
+ } ,
25
+ ] ,
26
+ } ,
27
+ {
28
+ name : 'Presentations' ,
29
+ type : 'folder' ,
30
+ children : [
31
+ {
32
+ name : 'Client_A' ,
33
+ type : 'file' ,
34
+ } ,
35
+ {
36
+ name : 'Client_B' ,
37
+ type : 'file' ,
38
+ } ,
39
+ ] ,
40
+ } ,
41
+ ] ,
42
+ } ,
43
+ {
44
+ name : 'Personal' ,
45
+ type : 'folder' ,
46
+ children : [
47
+ {
48
+ name : 'Vacation' ,
49
+ type : 'file' ,
50
+ } ,
51
+ {
52
+ name : 'Family' ,
53
+ type : 'file' ,
54
+ } ,
55
+ ] ,
56
+ } ,
57
+ ] ,
58
+ } ,
59
+ {
60
+ name : 'Photos' ,
61
+ type : 'folder' ,
62
+ children : [
63
+ {
64
+ name : '2019' ,
65
+ type : 'folder' ,
66
+ children : [
67
+ {
68
+ name : 'Summer' ,
69
+ type : 'file' ,
70
+ } ,
71
+ {
72
+ name : 'Winter' ,
73
+ type : 'file' ,
74
+ } ,
75
+ ] ,
76
+ } ,
77
+ {
78
+ name : '2020' ,
79
+ type : 'folder' ,
80
+ children : [
81
+ {
82
+ name : 'Spring' ,
83
+ type : 'file' ,
84
+ } ,
85
+ {
86
+ name : 'Autumn' ,
87
+ type : 'file' ,
88
+ } ,
89
+ ] ,
90
+ } ,
91
+ ] ,
92
+ } ,
93
+ ] ,
94
+ }
Original file line number Diff line number Diff line change
1
+ export { data as fileExplorerData } from './file-explorer'
You can’t perform that action at this time.
0 commit comments