Skip to content

Commit e4ea21d

Browse files
committed
fix: default shouldExpandNodeInitially to false for collapsed nodes. #59 #76
1 parent 393d202 commit e4ea21d

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

core/src/comps/KeyValues.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export const KeyValues = <T extends object>(props: KeyValuesProps<T>) => {
2525
typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false;
2626
const isExpanded = expands[expandKey] ?? defaultExpanded;
2727
const shouldExpand =
28-
shouldExpandNodeInitially && shouldExpandNodeInitially(isExpanded, { value, keys, level, keyName, parentValue });
29-
if (expands[expandKey] === undefined && !!shouldExpand) {
28+
shouldExpandNodeInitially && shouldExpandNodeInitially(!isExpanded, { value, keys, level, keyName, parentValue });
29+
if (expands[expandKey] === undefined && !shouldExpand) {
3030
return null;
3131
}
3232
if (isExpanded) {

core/src/comps/NestedClose.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export const NestedClose = <T extends object>(props: NestedCloseProps<T>) => {
1717
typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false;
1818
const isExpanded = expands[expandKey] ?? defaultExpanded;
1919
const shouldExpand =
20-
shouldExpandNodeInitially && shouldExpandNodeInitially(isExpanded, { value, keys, level, keyName, parentValue });
21-
if (expands[expandKey] === undefined && !!shouldExpand) {
20+
shouldExpandNodeInitially && shouldExpandNodeInitially(!isExpanded, { value, keys, level, keyName, parentValue });
21+
if (expands[expandKey] === undefined && !shouldExpand) {
2222
return null;
2323
}
2424
const len = Object.keys(value!).length;

core/src/comps/NestedOpen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export const NestedOpen = <T extends object>(props: NestedOpenProps<T>) => {
2525
typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false;
2626
let isExpanded = expands[expandKey] ?? defaultExpanded;
2727
const shouldExpand =
28-
shouldExpandNodeInitially && shouldExpandNodeInitially(isExpanded, { value, keys, level, keyName, parentValue });
28+
shouldExpandNodeInitially && shouldExpandNodeInitially(!isExpanded, { value, keys, level, keyName, parentValue });
2929
if (expands[expandKey] === undefined && shouldExpandNodeInitially) {
30-
isExpanded = !!shouldExpand;
30+
isExpanded = !shouldExpand;
3131
}
3232
const click = () => {
3333
const opt = { expand: !isExpanded, value, keyid: expandKey, keyName };

example/src/demo.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,20 @@ const example = {
3131
};
3232

3333
const shouldExpandNodeInitially: ShouldExpandNodeInitially<object> = (isExpanded, props) => {
34-
const { value, level, keyName, parentValue, keys } = props;
34+
const { value, level } = props;
3535
const isArray = Array.isArray(value);
3636
const isObject = typeof value === 'object' && !isArray;
3737
if (isArray) {
38-
return isExpanded || value.length > 5;
38+
return value.length < 5;
3939
}
4040
if (isObject && level > 3) {
41-
return isExpanded || true; // Expand if it's an object and level is greater than 3
41+
return false;
4242
}
4343
return isExpanded;
4444
};
4545

4646
export default function App() {
4747
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} />;
4850
}

0 commit comments

Comments
 (0)