add collapse/expand all feature for react-json-tree

This commit is contained in:
lucataglia 2023-05-18 15:33:28 +02:00
parent 58a8135b08
commit 5961ae4cba
3 changed files with 14 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import JSONArrow from './JSONArrow';
import getCollectionEntries from './getCollectionEntries';
import JSONNode from './JSONNode';
@ -110,6 +110,7 @@ export default function JSONNestedNode(props: Props) {
nodeType,
nodeTypeIndicator,
shouldExpandNodeInitially,
shouldExpandNode,
styling,
} = props;
@ -118,6 +119,15 @@ export default function JSONNestedNode(props: Props) {
isCircular ? false : shouldExpandNodeInitially(keyPath, data, level)
);
useEffect(() => {
if(shouldExpandNode === undefined){
return
}
setExpanded(shouldExpandNode);
}, [shouldExpandNode])
const handleClick = useCallback(() => {
if (expandable) setExpanded(!expanded);
}, [expandable, expanded]);

View File

@ -41,6 +41,7 @@ export function JSONTree({
labelRenderer = defaultLabelRenderer,
valueRenderer = identity,
shouldExpandNodeInitially = expandRootNode,
shouldExpandNode,
hideRoot = false,
getItemString = defaultItemString,
postprocessValue = identity,
@ -64,6 +65,7 @@ export function JSONTree({
labelRenderer={labelRenderer}
valueRenderer={valueRenderer}
shouldExpandNodeInitially={shouldExpandNodeInitially}
shouldExpandNode={shouldExpandNode}
hideRoot={hideRoot}
getItemString={getItemString}
postprocessValue={postprocessValue}

View File

@ -47,6 +47,7 @@ export interface CommonExternalProps {
labelRenderer: LabelRenderer;
valueRenderer: ValueRenderer;
shouldExpandNodeInitially: ShouldExpandNodeInitially;
shouldExpandNode: boolean | undefined;
hideRoot: boolean;
getItemString: GetItemString;
postprocessValue: PostprocessValue;