Skip to content

Commit 0d5c686

Browse files
committed
fix: restore automatic expansion of specified number of rows. fix #79
1 parent 1c162a0 commit 0d5c686

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

core/README.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -747,8 +747,11 @@ export default function Demo() {
747747

748748
## Default Collapse/Expand
749749

750+
Determines whether the node should be expanded on the first render. The default value of `collapsed` is `false`.
751+
If both `collapsed` and `shouldExpandNodeInitially` are set, `collapsed` takes precedence; `shouldExpandNodeInitially` only takes effect when `collapsed` is `false`.
752+
750753
```tsx mdx:preview
751-
import React from 'react';
754+
import React, { useState } from 'react';
752755
import JsonView from '@uiw/react-json-view';
753756
const object = {
754757
string: 'Lorem ipsum dolor sit amet',
@@ -765,21 +768,26 @@ const object = {
765768
],
766769
}
767770
export default function Demo() {
771+
const [collapsed, setCollapsed] = useState(false)
768772
return (
769-
<JsonView
770-
value={object}
771-
collapsed={2}
772-
shouldExpandNodeInitially={(isExpanded, { value, keys, level }) => {
773-
if (keys.length > 0 && keys[0] == "object") {
774-
return false
775-
}
776-
return isExpanded
777-
}}
778-
style={{
779-
'--w-rjv-background-color': '#ffffff',
780-
}}
781-
>
782-
</JsonView>
773+
<>
774+
<input type="checkbox" id="collapsed" checked={collapsed} onChange={(e) => setCollapsed(e.target.checked)} />
775+
<label for="collapsed"> collapsed={collapsed == true ? "false" : "2"} </label>
776+
<JsonView
777+
value={object}
778+
collapsed={collapsed == true ? false : 2}
779+
shouldExpandNodeInitially={(isExpanded, { value, keys, level }) => {
780+
if (keys.length > 0 && keys[0] == "object") {
781+
return false
782+
}
783+
return isExpanded
784+
}}
785+
style={{
786+
'--w-rjv-background-color': '#ffffff',
787+
}}
788+
>
789+
</JsonView>
790+
</>
783791
)
784792
}
785793
```
@@ -869,13 +877,13 @@ export default function Demo() {
869877
if (!isExpanded) {
870878
return (
871879
<svg viewBox="0 0 24 24" {...svgProps}>
872-
<path d="M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M13,7H11V11H7V13H11V17H13V13H17V11H13V7Z" />
880+
<path d="M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M7,13H17V11H7" />
873881
</svg>
874882
);
875883
}
876884
return (
877885
<svg viewBox="0 0 24 24" {...svgProps}>
878-
<path d="M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M7,13H17V11H7" />
886+
<path d="M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M13,7H11V11H7V13H11V17H13V13H17V11H13V7Z" />
879887
</svg>
880888
);
881889
}}

core/src/comps/KeyValues.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ export const KeyValues = <T extends object>(props: KeyValuesProps<T>) => {
2626
const isExpanded = expands[expandKey] ?? (shouldExpandNodeInitially ? false : defaultExpanded);
2727
const shouldExpand =
2828
shouldExpandNodeInitially && shouldExpandNodeInitially(!isExpanded, { value, keys, level, keyName, parentValue });
29-
30-
if (expands[expandKey] === undefined && !shouldExpand) {
31-
return null;
29+
if (shouldExpandNodeInitially && collapsed === false) {
30+
if (expands[expandKey] === undefined && !shouldExpand) {
31+
return null;
32+
}
33+
} else {
34+
if (expands[expandKey] === undefined && shouldExpand) {
35+
return null;
36+
}
3237
}
3338
if (isExpanded) {
3439
return null;

core/src/comps/NestedClose.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ export const NestedClose = <T extends object>(props: NestedCloseProps<T>) => {
1818
const isExpanded = expands[expandKey] ?? (shouldExpandNodeInitially ? false : defaultExpanded);
1919
const shouldExpand =
2020
shouldExpandNodeInitially && shouldExpandNodeInitially(!isExpanded, { value, keys, level, keyName, parentValue });
21-
if (expands[expandKey] === undefined && !shouldExpand) {
22-
return null;
21+
if (shouldExpandNodeInitially && collapsed === false) {
22+
if (expands[expandKey] === undefined && !shouldExpand) {
23+
return null;
24+
}
25+
} else {
26+
if (expands[expandKey] === undefined && shouldExpand) {
27+
return null;
28+
}
2329
}
2430
const len = Object.keys(value!).length;
2531
if (isExpanded || len === 0) {

core/src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const JsonView: JsonViewComponent = forwardRef<HTMLDivElement, JsonViewProps<obj
134134
value,
135135
children,
136136
collapsed = false,
137-
shouldExpandNodeInitially = () => true,
137+
shouldExpandNodeInitially,
138138
indentWidth = 15,
139139
displayObjectSize = true,
140140
shortenTextAfterLength = 30,
@@ -163,7 +163,7 @@ const JsonView: JsonViewComponent = forwardRef<HTMLDivElement, JsonViewProps<obj
163163
value,
164164
objectSortKeys,
165165
indentWidth,
166-
shouldExpandNodeInitially: collapsed === false ? shouldExpandNodeInitially : () => false,
166+
shouldExpandNodeInitially: collapsed === false ? shouldExpandNodeInitially : undefined,
167167
displayObjectSize,
168168
collapsed,
169169
enableClipboard,

0 commit comments

Comments
 (0)