Skip to content

Commit

Permalink
fix: number-string like '1234' remind origin type
Browse files Browse the repository at this point in the history
  • Loading branch information
blucas.wu committed Dec 19, 2023
1 parent e5a9568 commit dc30a3a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
32 changes: 31 additions & 1 deletion examples/assets/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import json from './big-size.json';

export const data = [
{
type: 'Primitive source',
type: 'Primitive string',
getData: () => {
const originSource = 'Hello, @huolala-tech/react-json-view';
return {
Expand All @@ -11,6 +11,26 @@ export const data = [
};
}
},
{
type: 'Number string',
getData: () => {
const originSource = '1234567890';
return {
originSource,
renderSource: originSource
};
}
},
{
type: 'Number value',
getData: () => {
const originSource = 1234567890;
return {
originSource,
renderSource: originSource
};
}
},
{
type: 'Normal object source',
getData: () => {
Expand Down Expand Up @@ -57,6 +77,16 @@ export const data = [
};
}
},
{
type: 'Undefined',
getData: () => {
const originSource = undefined;
return {
originSource: 'undefined',
renderSource: originSource
};
}
},
{
type: 'Ellipse string',
getData: () => {
Expand Down
41 changes: 30 additions & 11 deletions src/ReactJsonView/components/JsonNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isBoolean,
isListOrMap,
isNumber,
isString,
shortTitle
} from '../../utils';
import { ArrowRight } from './SvgIcon';
Expand All @@ -25,8 +26,27 @@ const PrimitiveContent = ({ content }: { content: React.ReactNode }) => {
return React.createElement(Fragment, null, computedContent);
};

const computePrimitiveDataClassname = (data: unknown) => {
if (data === null) return 'null';

const type = typeof data;
const primitiveType = ['number', 'boolean'];
if (primitiveType.indexOf(type) > -1) {
return type;
}
if (isString(data)) {
try {
const dataMaybeObject = JSON.parse(data);
return isListOrMap(dataMaybeObject) ? '' : 'string';
} catch (e) {
return 'string';
}
}
return '';
};

const JsonNode = ({
source = null,
source,
depth = 1,
label = ''
}: {
Expand All @@ -49,6 +69,7 @@ const JsonNode = ({

let data: unknown;
try {
// Exclude the invalid data
data = JSON.parse(JSON.stringify(source));
} catch (e) {
label = 'Error';
Expand All @@ -62,15 +83,9 @@ const JsonNode = ({
return null;
}, [label]);

let className = '';
const type = typeof data;
const primitiveType = ['string', 'number', 'boolean'];
if (primitiveType.indexOf(type) > -1) {
className = type;
} else if (data === null) {
className = 'null';
}
if (className) {
const primitiveClassname = computePrimitiveDataClassname(data);

if (primitiveClassname) {
return (
<code className="rjv-primitive">
{labelContent && (
Expand All @@ -81,14 +96,18 @@ const JsonNode = ({
{labelContent}
</>
)}
<span className={clsx('rjv-primitive-type', className)}>
<span className={clsx('rjv-primitive-type', primitiveClassname)}>
<PrimitiveContent content={`${data}`} />
</span>
{copyable && <Copyable data={JSON.stringify(data, null, 2)} />}
</code>
);
}

if (isString(data)) {
data = JSON.parse(data);
}

if (isListOrMap(data)) {
const title = shortTitle(data, maxTitleSize);

Expand Down
11 changes: 1 addition & 10 deletions src/ReactJsonView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,12 @@ import './index.less';
import { Options } from '../../types';
import { ConfigProvider, useConfigInfo } from './components/ConfigContext';
import JsonNode from './components/JsonNode';
import { isString } from '../utils';

const InitJsonNode = () => {
const { source, darkMode, rootLabel } = useConfigInfo();
let data: Options['source'] = source;
if (isString(source)) {
try {
data = JSON.parse(source);
} catch (e) {
//
}
}
return (
<div data-dark-mode={darkMode}>
<JsonNode label={rootLabel} source={data} />
<JsonNode label={rootLabel} source={source} />
</div>
);
};
Expand Down

0 comments on commit dc30a3a

Please sign in to comment.