From 1f4099499f5ac225c15ea41fbfaed897900baf35 Mon Sep 17 00:00:00 2001
From: jaywcjlove <398188662@qq.com>
Date: Thu, 17 Oct 2024 04:15:46 +0800
Subject: [PATCH] feat: add shouldExpandNodeInitially props. #59
---
core/README.md | 39 ++++++++++++++++++++++++++++++++++
core/src/Container.tsx | 2 +-
core/src/comps/KeyValues.tsx | 15 +++++++++----
core/src/comps/NestedClose.tsx | 18 +++++++++++-----
core/src/comps/NestedOpen.tsx | 14 +++++++-----
core/src/index.tsx | 7 ++++++
core/src/store.tsx | 1 +
www/src/App.tsx | 2 +-
8 files changed, 82 insertions(+), 16 deletions(-)
diff --git a/core/README.md b/core/README.md
index 2e4ce86b..013efe78 100644
--- a/core/README.md
+++ b/core/README.md
@@ -710,6 +710,45 @@ export default function Demo() {
}
```
+## Default Collapse/Expand
+
+```tsx mdx:preview
+import React from 'react';
+import JsonView from '@uiw/react-json-view';
+const object = {
+ string: 'Lorem ipsum dolor sit amet',
+ integer: 42,
+ float: 114.514,
+ object: {
+ 'first-child': true,
+ 'second-child': false,
+ 'last-child': null,
+ },
+ nestedArray: [
+ [1, 2],
+ [3, 4],
+ ],
+}
+export default function Demo() {
+ return (
+ {
+ if (keys.length > 0 && keys[0] == "object") {
+ return true
+ }
+ return isExpanded
+ }}
+ style={{
+ '--w-rjv-background-color': '#ffffff',
+ }}
+ >
+
+ )
+}
+```
+
## Modify Icon Style
Use built-in default icons.
diff --git a/core/src/Container.tsx b/core/src/Container.tsx
index ab6bd367..bf4fed56 100644
--- a/core/src/Container.tsx
+++ b/core/src/Container.tsx
@@ -54,7 +54,7 @@ export const Container = forwardRef((props: ContainerProps,
parentValue={parentValue}
keyName={keyName}
/>
-
+
);
});
diff --git a/core/src/comps/KeyValues.tsx b/core/src/comps/KeyValues.tsx
index a0c1049b..d52cd3f0 100644
--- a/core/src/comps/KeyValues.tsx
+++ b/core/src/comps/KeyValues.tsx
@@ -20,11 +20,18 @@ interface KeyValuesProps extends SectionElementResult {
export const KeyValues = (props: KeyValuesProps) => {
const { value, expandKey = '', level, keys = [] } = props;
const expands = useExpandsStore();
- const { objectSortKeys, indentWidth, collapsed } = useStore();
+ const { objectSortKeys, indentWidth, collapsed, shouldExpandNodeInitially } = useStore();
const isMyArray = Array.isArray(value);
- const isExpanded =
- expands[expandKey] ??
- (typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false);
+ const defaultExpanded =
+ typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false;
+ const isExpanded = expands[expandKey] ?? defaultExpanded;
+ if (
+ expands[expandKey] === undefined &&
+ shouldExpandNodeInitially &&
+ shouldExpandNodeInitially(isExpanded, { value, keys, level })
+ ) {
+ return null;
+ }
if (isExpanded) {
return null;
}
diff --git a/core/src/comps/NestedClose.tsx b/core/src/comps/NestedClose.tsx
index 86b91e9b..76f858a9 100644
--- a/core/src/comps/NestedClose.tsx
+++ b/core/src/comps/NestedClose.tsx
@@ -6,18 +6,26 @@ interface NestedCloseProps {
value?: T;
expandKey: string;
level: number;
+ keys?: (string | number)[];
}
export const NestedClose = (props: NestedCloseProps) => {
- const { value, expandKey, level } = props;
+ const { value, expandKey, level, keys = [] } = props;
const expands = useExpandsStore();
const isArray = Array.isArray(value);
- const { collapsed } = useStore();
+ const { collapsed, shouldExpandNodeInitially } = useStore();
const isMySet = value instanceof Set;
- const isExpanded =
- expands[expandKey] ??
- (typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false);
+ const defaultExpanded =
+ typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false;
+ const isExpanded = expands[expandKey] ?? defaultExpanded;
const len = Object.keys(value!).length;
+ if (
+ expands[expandKey] === undefined &&
+ shouldExpandNodeInitially &&
+ shouldExpandNodeInitially(isExpanded, { value, keys, level })
+ ) {
+ return null;
+ }
if (isExpanded || len === 0) {
return null;
}
diff --git a/core/src/comps/NestedOpen.tsx b/core/src/comps/NestedOpen.tsx
index 7fc8a67b..af831112 100644
--- a/core/src/comps/NestedOpen.tsx
+++ b/core/src/comps/NestedOpen.tsx
@@ -16,16 +16,20 @@ export interface NestedOpenProps extends SectionElementResult<
}
export const NestedOpen = (props: NestedOpenProps) => {
- const { keyName, expandKey, keys, initialValue, value, parentValue, level } = props;
+ const { keyName, expandKey, keys = [], initialValue, value, parentValue, level } = props;
const expands = useExpandsStore();
const dispatchExpands = useExpandsDispatch();
- const { onExpand, collapsed } = useStore();
+ const { onExpand, collapsed, shouldExpandNodeInitially } = useStore();
const isArray = Array.isArray(value);
const isMySet = value instanceof Set;
- const isExpanded =
- expands[expandKey] ??
- (typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false);
+ const defaultExpanded =
+ typeof collapsed === 'boolean' ? collapsed : typeof collapsed === 'number' ? level > collapsed : false;
const isObject = typeof value === 'object';
+ let isExpanded = expands[expandKey] ?? defaultExpanded;
+ const shouldExpand = shouldExpandNodeInitially && shouldExpandNodeInitially(isExpanded, { value, keys, level });
+ if (expands[expandKey] === undefined && shouldExpand !== undefined) {
+ isExpanded = shouldExpand;
+ }
const click = () => {
const opt = { expand: !isExpanded, value, keyid: expandKey, keyName };
onExpand && onExpand(opt);
diff --git a/core/src/index.tsx b/core/src/index.tsx
index b1a3f3ca..6ea3f83d 100644
--- a/core/src/index.tsx
+++ b/core/src/index.tsx
@@ -57,6 +57,11 @@ export interface JsonViewProps
enableClipboard?: boolean;
/** When set to true, all nodes will be collapsed by default. Use an integer value to collapse at a particular depth. @default false */
collapsed?: boolean | number;
+ /** Determine whether the node should be expanded on the first render, or you can use collapsed to control the level of expansion (by default, the root is expanded). */
+ shouldExpandNodeInitially?: (
+ isExpanded: boolean,
+ props: { value?: T; keys: (number | string)[]; level: number },
+ ) => boolean;
/** Whether to highlight updates. @default true */
highlightUpdates?: boolean;
/** Shorten long JSON strings, Set to `0` to disable this feature @default 30 */
@@ -107,6 +112,7 @@ const JsonView: JsonViewComponent = forwardRef {
enableClipboard?: JsonViewProps['enableClipboard'];
highlightUpdates?: JsonViewProps['highlightUpdates'];
collapsed?: JsonViewProps['collapsed'];
+ shouldExpandNodeInitially?: JsonViewProps['shouldExpandNodeInitially'];
indentWidth?: number;
}
diff --git a/www/src/App.tsx b/www/src/App.tsx
index 6c52e210..16985ec7 100644
--- a/www/src/App.tsx
+++ b/www/src/App.tsx
@@ -38,7 +38,7 @@ export default function App() {
Editor
*/}
- {tabs === 'preview' && }
+ {/* {tabs === 'preview' && } */}
{/* {tabs === 'editor' && } */}
);