1
1
import { FC , Fragment , PropsWithChildren , useId , cloneElement , useState , useEffect , forwardRef } from 'react' ;
2
- import { ValueView , ValueViewProps , Colon , Label , LabelProps , Line , typeMap } from './value' ;
2
+ import { ValueView , Type , ValueViewProps , Colon , Label , LabelProps , Line , typeMap } from './value' ;
3
3
import { TriangleArrow } from './arrow/TriangleArrow' ;
4
4
import { useExpandsStatus , store } from './store' ;
5
5
import { JsonViewProps } from './' ;
@@ -33,6 +33,8 @@ export interface RootNodeProps<T extends object> extends JsonViewProps<T> {
33
33
keyid ?: string ;
34
34
level ?: number ;
35
35
parentValue ?: T ;
36
+ isSet ?: boolean ;
37
+ isMap ?: boolean ;
36
38
namespace ?: Array < string | number > ;
37
39
setParentValue ?: React . Dispatch < React . SetStateAction < T > > ;
38
40
}
@@ -56,6 +58,8 @@ export const RootNode = forwardRef(
56
58
keyid = 'root' ,
57
59
quotes = '"' ,
58
60
namespace = [ ] ,
61
+ isSet = false ,
62
+ isMap = false ,
59
63
onCopied,
60
64
onExpand,
61
65
parentValue,
@@ -167,6 +171,8 @@ export const RootNode = forwardRef(
167
171
< Colon />
168
172
</ Fragment >
169
173
) }
174
+ { isSet && < Type type = "Set" /> }
175
+ { isMap && < Type type = "Map" /> }
170
176
< Meta start isArray = { isArray } level = { level } render = { components . braces } />
171
177
{ ! expand && < Ellipsis render = { components . ellipsis } count = { nameKeys . length } level = { level } /> }
172
178
{ ! expand && < Meta isArray = { isArray } level = { level } render = { components . braces } /> }
@@ -188,18 +194,23 @@ export const RootNode = forwardRef(
188
194
{ entries . length > 0 &&
189
195
[ ...entries ] . map ( ( [ key , itemVal ] , idx ) => {
190
196
const item = itemVal as T ;
197
+ const isMySet = item instanceof Set ;
198
+ const isMyMap = item instanceof Map ;
199
+ let myValue = isMySet ? Array . from ( item as Set < any > ) : isMyMap ? Object . fromEntries ( item ) : item ;
191
200
const isEmpty =
192
- ( Array . isArray ( item ) && ( item as [ ] ) . length === 0 ) ||
193
- ( typeof item === 'object' &&
194
- item &&
195
- ! ( ( item as any ) instanceof Date ) &&
196
- Object . keys ( item ) . length === 0 ) ;
197
- if ( Array . isArray ( item ) && ! isEmpty ) {
201
+ ( Array . isArray ( myValue ) && ( myValue as [ ] ) . length === 0 ) ||
202
+ ( typeof myValue === 'object' &&
203
+ myValue &&
204
+ ! ( ( myValue as any ) instanceof Date ) &&
205
+ Object . keys ( myValue ) . length === 0 ) ;
206
+ if ( ( Array . isArray ( myValue ) || isMySet || isMyMap ) && ! isEmpty ) {
198
207
const label = ( isArray ? idx : key ) as string ;
199
208
return (
200
209
< Line key = { label + idx } className = "w-rjv-wrap" >
201
210
< RootNode
202
- value = { item }
211
+ value = { myValue }
212
+ isSet = { isMySet }
213
+ isMap = { isMyMap }
203
214
namespace = { [ ...namespace , label ] }
204
215
keyName = { label }
205
216
keyid = { keyid + subkeyid + label }
@@ -208,11 +219,11 @@ export const RootNode = forwardRef(
208
219
</ Line >
209
220
) ;
210
221
}
211
- if ( typeof item === 'object' && item && ! ( ( item as any ) instanceof Date ) && ! isEmpty ) {
222
+ if ( typeof myValue === 'object' && myValue && ! ( ( myValue as any ) instanceof Date ) && ! isEmpty ) {
212
223
return (
213
224
< Line key = { key + '' + idx } className = "w-rjv-wrap" >
214
225
< RootNode
215
- value = { item }
226
+ value = { myValue }
216
227
namespace = { [ ...namespace , key ] }
217
228
keyName = { key }
218
229
keyid = { keyid + subkeyid + key }
@@ -221,12 +232,12 @@ export const RootNode = forwardRef(
221
232
</ Line >
222
233
) ;
223
234
}
224
- if ( typeof item === 'function' ) {
235
+ if ( typeof myValue === 'function' ) {
225
236
return ;
226
237
}
227
238
const renderKey = (
228
239
< Semicolon
229
- value = { item }
240
+ value = { myValue }
230
241
data-keys = { keyid }
231
242
quotes = { quotes }
232
243
namespace = { [ ...namespace , key ] }
@@ -237,7 +248,7 @@ export const RootNode = forwardRef(
237
248
keyName = { key }
238
249
/>
239
250
) ;
240
- const length = Array . isArray ( item ) ? item . length : getLength ( item ) ;
251
+ const length = Array . isArray ( myValue ) ? myValue . length : getLength ( myValue ) ;
241
252
countInfo = < CountInfo > { length } </ CountInfo > ;
242
253
if ( components . countInfo ) {
243
254
countInfo = components . countInfo ( { count : length , level, visible : expand } ) || countInfo ;
@@ -251,7 +262,8 @@ export const RootNode = forwardRef(
251
262
countInfo = { countInfo }
252
263
renderKey = { renderKey }
253
264
keyName = { key }
254
- value = { item }
265
+ isSet = { isSet }
266
+ value = { myValue }
255
267
/>
256
268
) ;
257
269
} ) }
0 commit comments