Skip to content

Commit 68a4d9b

Browse files
authored
Merge pull request #712 from code0-tech/feat/#711
JSONViewer support for primitive values
2 parents 2ff3c2e + d6bbf03 commit 68a4d9b

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/components/json-view/JsonView.stories.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const Collapsed = () => {
4747
}
4848

4949
return <Card color={"secondary"} w={"500px"}>
50-
<JsonView value={value} collapsed={1}/>
50+
<JsonView value={value} collapsed={2}/>
5151
</Card>
5252
}
5353

@@ -67,3 +67,13 @@ export const WithDataTypes = () => {
6767
<JsonView value={value} displayDataTypes displayObjectSize/>
6868
</Card>
6969
}
70+
71+
export const Primitive = () => {
72+
73+
const value = "hello"
74+
75+
return <Card color={"secondary"} w={"500px"}>
76+
{/**@ts-ignore**/}
77+
<JsonView value={value} displayDataTypes displayObjectSize/>
78+
</Card>
79+
}

src/components/json-view/JsonView.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {IconCheck, IconChevronRight, IconCopy} from "@tabler/icons-react";
44
import {AnimatePresence, motion} from "motion/react";
55
import {Component, hashToColor, mergeComponentProps} from "../../utils";
66
import "./JsonView.style.scss";
7+
import {Text} from "../text/Text";
78

89
export interface JsonViewProps<T extends object = object> extends Omit<Component<HTMLDivElement>, 'value'> {
910
value?: T
@@ -63,9 +64,11 @@ export const JsonView = <T extends object = object>(props: JsonViewProps<T>) =>
6364

6465
const merged = mergeComponentProps("json-view", rest)
6566

66-
const safeValue = (value === null || value === undefined || typeof value !== 'object')
67-
? [value] as unknown as T
68-
: value
67+
if ((value === null || value === undefined || typeof value !== 'object')) {
68+
return <Text c={hashToColor("Text")}>
69+
{String(value)}
70+
</Text>
71+
}
6972

7073
return (
7174
<ReactJsonView
@@ -74,7 +77,7 @@ export const JsonView = <T extends object = object>(props: JsonViewProps<T>) =>
7477
enableClipboard={enableClipboard}
7578
indentWidth={indentWidth}
7679
{...merged}
77-
value={safeValue}
80+
value={value}
7881
style={{...JSON_VIEW_THEME, ...(merged.style ?? {})}}
7982
>
8083
<ReactJsonView.Arrow

0 commit comments

Comments
 (0)