Skip to content

Commit 07c779c

Browse files
authored
feat: add hideRootExpand (#17)
* feat: add hideRootExpand prop - force root node into expanded state
1 parent a86bad7 commit 07c779c

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

examples/src/App.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ const App = () => (
9494
<JSONTree hideRoot={true} data={data} />
9595
</div>
9696
<br />
97+
98+
<h3>Force root node open</h3>
99+
<div style={{ background: "#222" }}>
100+
<JSONTree hideRootExpand={true} data={data} />
101+
</div>
102+
<br />
103+
97104
<h3>Theming Example</h3>
98105
<p>
99106
Styles are managed with css variables, override the default values to

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gtk-grafana/react-json-tree",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"description": "React JSON Viewer Component, Extracted from redux-devtools",
55
"keywords": [
66
"react",

src/JSONNestedNode.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export default function JSONNestedNode(props: Props) {
105105
expandable,
106106
getItemString,
107107
hideRoot,
108+
hideRootExpand,
108109
isCircular,
109110
keyPath,
110111
labelRenderer,
@@ -114,14 +115,20 @@ export default function JSONNestedNode(props: Props) {
114115
shouldExpandNodeInitially,
115116
} = props;
116117

118+
const isRoot = keyPath[0] === "root";
119+
const showExpand = hideRootExpand
120+
? expandable && !isRoot && hideRootExpand
121+
: expandable;
122+
const isNodeExpandable = expandable && showExpand;
123+
117124
const [expanded, setExpanded] = useState<boolean>(
118125
// calculate individual node expansion if necessary
119126
isCircular ? false : shouldExpandNodeInitially(keyPath, data, level),
120127
);
121128

122129
const handleClick = useCallback(() => {
123-
if (expandable) setExpanded(!expanded);
124-
}, [expandable, expanded]);
130+
if (isNodeExpandable) setExpanded(!expanded);
131+
}, [isNodeExpandable, expanded]);
125132

126133
const renderedChildren =
127134
expanded || (hideRoot && level === 0)
@@ -142,11 +149,11 @@ export default function JSONNestedNode(props: Props) {
142149
createItemString(data, collectionLimit),
143150
keyPath,
144151
);
145-
const stylingArgs = [keyPath, nodeType, expanded, expandable] as const;
152+
const stylingArgs = [keyPath, nodeType, expanded, isNodeExpandable] as const;
146153

147154
return hideRoot ? (
148155
<NodeListItem
149-
expandable={expandable}
156+
expandable={isNodeExpandable}
150157
expanded={expanded}
151158
nodeType={nodeType}
152159
keyPath={keyPath}
@@ -156,14 +163,14 @@ export default function JSONNestedNode(props: Props) {
156163
</NodeListItem>
157164
) : (
158165
<NodeListItem
159-
expandable={expandable}
166+
expandable={isNodeExpandable}
160167
expanded={expanded}
161168
nodeType={nodeType}
162169
keyPath={keyPath}
163-
className={`${styles.nestedNode} ${expanded ? styles.nestedNodeExpanded : ""} ${expandable ? styles.nestedNodeExpandable : ""}`}
170+
className={`${styles.nestedNode} ${expanded ? styles.nestedNodeExpanded : ""} ${isNodeExpandable ? styles.nestedNodeExpandable : ""}`}
164171
>
165172
<span className={styles.nestedNodeLabelWrap}>
166-
{expandable && (
173+
{showExpand && (
167174
<JSONArrow
168175
nodeType={nodeType}
169176
expanded={expanded}
@@ -173,7 +180,7 @@ export default function JSONNestedNode(props: Props) {
173180
<span
174181
data-nodetype={nodeType}
175182
data-keypath={keyPath[0]}
176-
className={`${styles.nestedNodeLabel} ${expanded ? styles.nestedNodeLabelExpanded : ""} ${expandable ? styles.nestedNodeLabelExpandable : ""}`}
183+
className={`${styles.nestedNodeLabel} ${expanded ? styles.nestedNodeLabelExpanded : ""} ${isNodeExpandable ? styles.nestedNodeLabelExpandable : ""}`}
177184
onClick={handleClick}
178185
>
179186
{labelRenderer(...stylingArgs)}

src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function JSONTree({
4040
valueRenderer = identity,
4141
shouldExpandNodeInitially = expandRootNode,
4242
hideRoot = false,
43+
hideRootExpand = false,
4344
getItemString = defaultItemString,
4445
postprocessValue = identity,
4546
isCustomNode = noCustomNode,
@@ -54,6 +55,7 @@ export function JSONTree({
5455
className={styles.tree}
5556
>
5657
<JSONNode
58+
hideRootExpand={hideRootExpand}
5759
keyPath={hideRoot ? [] : keyPath}
5860
value={postprocessValue(value)}
5961
isCustomNode={isCustomNode}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export interface CommonExternalProps {
5050
valueRenderer: ValueRenderer;
5151
shouldExpandNodeInitially: ShouldExpandNodeInitially;
5252
hideRoot: boolean;
53+
hideRootExpand: boolean;
5354
getItemString: GetItemString;
5455
postprocessValue: PostprocessValue;
5556
isCustomNode: IsCustomNode;

0 commit comments

Comments
 (0)