@@ -2,11 +2,13 @@ import { For, Match, Show, Switch, createSignal } from 'solid-js'
2
2
import clsx from 'clsx'
3
3
import { css , useStyles } from '../styles/use-styles'
4
4
import { CopiedCopier , Copier , ErrorCopier } from './icons'
5
+ import type { CollapsiblePaths } from '../utils/deep-keys'
5
6
6
- export function JsonTree ( props : {
7
- value : any
7
+ export function JsonTree < TData , TName extends CollapsiblePaths < TData > > ( props : {
8
+ value : TData
8
9
copyable ?: boolean
9
10
defaultExpansionDepth ?: number
11
+ collapsePaths ?: Array < TName >
10
12
} ) {
11
13
return (
12
14
< JsonValue
@@ -15,6 +17,8 @@ export function JsonTree(props: {
15
17
copyable = { props . copyable }
16
18
depth = { 0 }
17
19
defaultExpansionDepth = { props . defaultExpansionDepth ?? 1 }
20
+ path = ""
21
+ collapsePaths = { props . collapsePaths }
18
22
/>
19
23
)
20
24
}
@@ -25,8 +29,12 @@ function JsonValue(props: {
25
29
isRoot ?: boolean
26
30
isLastKey ?: boolean
27
31
copyable ?: boolean
32
+
28
33
defaultExpansionDepth : number
29
34
depth : number
35
+
36
+ collapsePaths ?: Array < string >
37
+ path : string
30
38
} ) {
31
39
const {
32
40
value,
@@ -36,6 +44,8 @@ function JsonValue(props: {
36
44
copyable,
37
45
defaultExpansionDepth,
38
46
depth,
47
+ collapsePaths,
48
+ path,
39
49
} = props
40
50
const styles = useStyles ( )
41
51
@@ -75,6 +85,8 @@ function JsonValue(props: {
75
85
copyable = { copyable }
76
86
keyName = { keyName }
77
87
value = { value }
88
+ collapsePaths = { collapsePaths }
89
+ path = { path }
78
90
/>
79
91
)
80
92
}
@@ -86,6 +98,8 @@ function JsonValue(props: {
86
98
copyable = { copyable }
87
99
keyName = { keyName }
88
100
value = { value }
101
+ collapsePaths = { collapsePaths }
102
+ path = { path }
89
103
/>
90
104
)
91
105
}
@@ -107,15 +121,22 @@ const ArrayValue = ({
107
121
copyable,
108
122
defaultExpansionDepth,
109
123
depth,
124
+ collapsePaths,
125
+ path,
110
126
} : {
111
127
value : Array < any >
112
128
copyable ?: boolean
113
129
keyName ?: string
114
130
defaultExpansionDepth : number
115
131
depth : number
132
+ collapsePaths ?: Array < string >
133
+ path : string
116
134
} ) => {
117
135
const styles = useStyles ( )
118
- const [ expanded , setExpanded ] = createSignal ( depth <= defaultExpansionDepth )
136
+
137
+ const [ expanded , setExpanded ] = createSignal (
138
+ depth <= defaultExpansionDepth && ! collapsePaths ?. includes ( path ) ,
139
+ )
119
140
120
141
if ( value . length === 0 ) {
121
142
return (
@@ -125,6 +146,7 @@ const ArrayValue = ({
125
146
"{ keyName } ":{ ' ' }
126
147
</ span >
127
148
) }
149
+
128
150
< span class = { styles ( ) . tree . valueBraces } > []</ span >
129
151
</ span >
130
152
)
@@ -135,6 +157,7 @@ const ArrayValue = ({
135
157
onClick = { ( ) => setExpanded ( ! expanded ( ) ) }
136
158
expanded = { expanded ( ) }
137
159
/>
160
+
138
161
{ keyName && (
139
162
< span
140
163
onclick = { ( e ) => {
@@ -148,7 +171,9 @@ const ArrayValue = ({
148
171
< span class = { styles ( ) . tree . info } > { value . length } items</ span >
149
172
</ span >
150
173
) }
174
+
151
175
< span class = { styles ( ) . tree . valueBraces } > [</ span >
176
+
152
177
< Show when = { expanded ( ) } >
153
178
< span class = { styles ( ) . tree . expandedLine ( Boolean ( keyName ) ) } >
154
179
< For each = { value } >
@@ -161,12 +186,15 @@ const ArrayValue = ({
161
186
isLastKey = { isLastKey }
162
187
defaultExpansionDepth = { defaultExpansionDepth }
163
188
depth = { depth + 1 }
189
+ collapsePaths = { collapsePaths }
190
+ path = { path ? `${ path } [${ i ( ) } ]` : `[${ i ( ) } ]` }
164
191
/>
165
192
)
166
193
} }
167
194
</ For >
168
195
</ span >
169
196
</ Show >
197
+
170
198
< Show when = { ! expanded ( ) } >
171
199
< span
172
200
onClick = { ( e ) => {
@@ -190,15 +218,23 @@ const ObjectValue = ({
190
218
copyable,
191
219
defaultExpansionDepth,
192
220
depth,
221
+ collapsePaths,
222
+ path,
193
223
} : {
194
224
value : Record < string , any >
195
225
keyName ?: string
196
226
copyable ?: boolean
197
227
defaultExpansionDepth : number
198
228
depth : number
229
+ collapsePaths ?: Array < string >
230
+ path : string
199
231
} ) => {
200
232
const styles = useStyles ( )
201
- const [ expanded , setExpanded ] = createSignal ( depth <= defaultExpansionDepth )
233
+
234
+ const [ expanded , setExpanded ] = createSignal (
235
+ depth <= defaultExpansionDepth && ! collapsePaths ?. includes ( path ) ,
236
+ )
237
+
202
238
const keys = Object . keys ( value )
203
239
const lastKeyName = keys [ keys . length - 1 ]
204
240
@@ -210,10 +246,12 @@ const ObjectValue = ({
210
246
"{ keyName } ":{ ' ' }
211
247
</ span >
212
248
) }
249
+
213
250
< span class = { styles ( ) . tree . valueBraces } > { '{}' } </ span >
214
251
</ span >
215
252
)
216
253
}
254
+
217
255
return (
218
256
< span class = { styles ( ) . tree . expanderContainer } >
219
257
{ keyName && (
@@ -222,6 +260,7 @@ const ObjectValue = ({
222
260
expanded = { expanded ( ) }
223
261
/>
224
262
) }
263
+
225
264
{ keyName && (
226
265
< span
227
266
onClick = { ( e ) => {
@@ -235,7 +274,9 @@ const ObjectValue = ({
235
274
< span class = { styles ( ) . tree . info } > { keys . length } items</ span >
236
275
</ span >
237
276
) }
277
+
238
278
< span class = { styles ( ) . tree . valueBraces } > { '{' } </ span >
279
+
239
280
< Show when = { expanded ( ) } >
240
281
< span class = { styles ( ) . tree . expandedLine ( Boolean ( keyName ) ) } >
241
282
< For each = { keys } >
@@ -248,12 +289,15 @@ const ObjectValue = ({
248
289
copyable = { copyable }
249
290
defaultExpansionDepth = { defaultExpansionDepth }
250
291
depth = { depth + 1 }
292
+ collapsePaths = { collapsePaths }
293
+ path = { `${ path } ${ path ? '.' : '' } ${ k } ` }
251
294
/>
252
295
</ >
253
296
) }
254
297
</ For >
255
298
</ span >
256
299
</ Show >
300
+
257
301
< Show when = { ! expanded ( ) } >
258
302
< span
259
303
onClick = { ( e ) => {
@@ -266,6 +310,7 @@ const ObjectValue = ({
266
310
{ `...` }
267
311
</ span >
268
312
</ Show >
313
+
269
314
< span class = { styles ( ) . tree . valueBraces } > { '}' } </ span >
270
315
</ span >
271
316
)
0 commit comments