Skip to content

Commit 87a29ec

Browse files
committed
fix: default shouldExpandNodeInitially to false for collapsed nodes. #59 #76
1 parent e3734c9 commit 87a29ec

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

core/src/comps/KeyValues.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ export const KeyValues = <T extends object>(props: KeyValuesProps<T>) => {
2323
const { objectSortKeys, indentWidth, collapsed, shouldExpandNodeInitially } = useStore();
2424
const defaultExpanded =
2525
typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false;
26-
const isExpanded = expands[expandKey] ?? defaultExpanded;
26+
const isExpanded = expands[expandKey] ?? (shouldExpandNodeInitially ? false : defaultExpanded);
2727
const shouldExpand =
2828
shouldExpandNodeInitially && shouldExpandNodeInitially(!isExpanded, { value, keys, level, keyName, parentValue });
29+
30+
console.log('KeyValues', expands[expandKey], defaultExpanded, !shouldExpand, shouldExpand, isExpanded);
2931
if (expands[expandKey] === undefined && !shouldExpand) {
3032
return null;
3133
}

core/src/comps/NestedClose.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ export const NestedClose = <T extends object>(props: NestedCloseProps<T>) => {
1515
const { collapsed, shouldExpandNodeInitially } = useStore();
1616
const defaultExpanded =
1717
typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false;
18-
const isExpanded = expands[expandKey] ?? defaultExpanded;
18+
const isExpanded = expands[expandKey] ?? (shouldExpandNodeInitially ? false : defaultExpanded);
1919
const shouldExpand =
2020
shouldExpandNodeInitially && shouldExpandNodeInitially(!isExpanded, { value, keys, level, keyName, parentValue });
21+
console.log('NestedClose', expands[expandKey], defaultExpanded, shouldExpand);
2122
if (expands[expandKey] === undefined && !shouldExpand) {
2223
return null;
2324
}

core/src/comps/NestedOpen.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ export const NestedOpen = <T extends object>(props: NestedOpenProps<T>) => {
2323
const { onExpand, collapsed, shouldExpandNodeInitially } = useStore();
2424
const defaultExpanded =
2525
typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false;
26-
let isExpanded = expands[expandKey] ?? defaultExpanded;
26+
let isExpanded = expands[expandKey] ?? (shouldExpandNodeInitially ? false : defaultExpanded);
2727
const shouldExpand =
2828
shouldExpandNodeInitially && shouldExpandNodeInitially(!isExpanded, { value, keys, level, keyName, parentValue });
29+
console.log('NestedOpen', expands[expandKey], defaultExpanded, shouldExpand, !shouldExpand);
2930
if (expands[expandKey] === undefined && shouldExpandNodeInitially) {
3031
isExpanded = !shouldExpand;
3132
}

core/src/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ const JsonView: JsonViewComponent = forwardRef<HTMLDivElement, JsonViewProps<obj
125125
style,
126126
value,
127127
children,
128-
collapsed,
129-
shouldExpandNodeInitially,
128+
collapsed = false,
129+
shouldExpandNodeInitially = () => true,
130130
indentWidth = 15,
131131
displayObjectSize = true,
132132
shortenTextAfterLength = 30,
@@ -155,7 +155,7 @@ const JsonView: JsonViewComponent = forwardRef<HTMLDivElement, JsonViewProps<obj
155155
value,
156156
objectSortKeys,
157157
indentWidth,
158-
shouldExpandNodeInitially,
158+
shouldExpandNodeInitially: collapsed === false ? shouldExpandNodeInitially : () => false,
159159
displayObjectSize,
160160
collapsed,
161161
enableClipboard,

example/src/demo.tsx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React, { useState } from 'react';
12
import JsonView, { ShouldExpandNodeInitially } from '@uiw/react-json-view';
23

34
const avatar = 'https://i.imgur.com/MK3eW3As.jpg';
@@ -44,7 +45,35 @@ const shouldExpandNodeInitially: ShouldExpandNodeInitially<object> = (isExpanded
4445
};
4546

4647
export default function App() {
47-
return <JsonView value={example} displayObjectSize={false} shouldExpandNodeInitially={shouldExpandNodeInitially} />;
48-
// return <JsonView value={example} displayObjectSize={false} shouldExpandNodeInitially={() => true} collapsed={true} />;
49-
// return <JsonView value={example} displayObjectSize={false} shouldExpandNodeInitially={() => false} />;
48+
const [collapsed, setCollapsed] = useState(true);
49+
// return <JsonView value={example} />;
50+
return (
51+
<div>
52+
<JsonView
53+
value={example}
54+
displayObjectSize={false}
55+
shouldExpandNodeInitially={shouldExpandNodeInitially}
56+
collapsed={collapsed}
57+
/>
58+
<button style={{ marginTop: 12 }} onClick={() => setCollapsed(!collapsed)}>
59+
{collapsed ? 'Expand All' : 'Collapse All'}
60+
</button>
61+
</div>
62+
);
63+
return (
64+
<JsonView
65+
value={example}
66+
displayObjectSize={false}
67+
shouldExpandNodeInitially={shouldExpandNodeInitially}
68+
collapsed={true}
69+
/>
70+
);
71+
return <JsonView value={example} displayObjectSize={false} collapsed={true} />;
72+
return <JsonView value={example} displayObjectSize={false} collapsed={false} />;
73+
return (
74+
<JsonView value={example} displayObjectSize={false} shouldExpandNodeInitially={() => true} collapsed={false} />
75+
);
76+
return <JsonView value={example} displayObjectSize={false} shouldExpandNodeInitially={() => true} collapsed={true} />;
77+
return <JsonView value={example} displayObjectSize={false} shouldExpandNodeInitially={() => true} />;
78+
return <JsonView value={example} displayObjectSize={false} shouldExpandNodeInitially={() => false} />;
5079
}

0 commit comments

Comments
 (0)