Skip to content

Commit a4513c6

Browse files
committed
feat: add components.countInfo props.
1 parent b9f806b commit a4513c6

File tree

13 files changed

+280
-84
lines changed

13 files changed

+280
-84
lines changed

core/README.md

Lines changed: 73 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -150,71 +150,6 @@ export default function Demo() {
150150
}
151151
```
152152

153-
Inspector
154-
155-
```tsx mdx:preview
156-
import React from 'react';
157-
import JsonView from '@uiw/react-json-view';
158-
159-
const object = [
160-
{
161-
"_id": "56dcf573b09c217d39fd7621",
162-
"name": "Howard Christensen",
163-
"email": "[email protected]",
164-
"phone": "+1 (830) 529-3176",
165-
"address": "511 Royce Street, Hilltop, Tennessee, 9712"
166-
},
167-
{
168-
"_id": "56dcf57323630b06251e93cd",
169-
"name": "Eleanor Lynn",
170-
"email": "[email protected]",
171-
"phone": "+1 (911) 576-2345",
172-
"address": "547 Dearborn Court, Trona, California, 8629"
173-
},
174-
{
175-
"_id": "56dcf5738279cac6b081e512",
176-
"name": "Baxter Mooney",
177-
"email": "[email protected]",
178-
"phone": "+1 (954) 456-3456",
179-
"address": "349 Cumberland Walk, Washington, Alaska, 3154"
180-
},
181-
{
182-
"_id": "56dcf57303accabd43740957",
183-
"name": "Calhoun Tyson",
184-
"email": "[email protected]",
185-
"phone": "+1 (818) 456-2529",
186-
"address": "367 Lyme Avenue, Ladera, Louisiana, 6292"
187-
},
188-
{
189-
"_id": "56dcf57391ea6a9d1f60df0c",
190-
"name": "Judith Jimenez",
191-
"email": "[email protected]",
192-
"phone": "+1 (950) 587-3415",
193-
"address": "269 Bogart Street, Sultana, Vermont, 7833"
194-
},
195-
{
196-
"_id": "56dcf5735a7a0718a61f294d",
197-
"name": "Newman Lawson",
198-
"email": "[email protected]",
199-
"phone": "+1 (814) 484-2827",
200-
"address": "969 Conduit Boulevard, Lowell, Oregon, 4118"
201-
}
202-
]
203-
204-
const customTheme = {
205-
'--w-rjv-background-color': '#fff',
206-
'--w-rjv-border-left-width': 0,
207-
'--w-rjv-color': '#881391',
208-
'--w-rjv-type-int-color': '#881391',
209-
};
210-
211-
export default function Demo() {
212-
return (
213-
<JsonView value={object} style={customTheme} quotes="" displayDataTypes={false} enableClipboard={false} />
214-
)
215-
}
216-
```
217-
218153
## Online Editing Theme
219154

220155
Online custom style example, please check in the [documentation website](https://uiwjs.github.io/react-json-view/)
@@ -465,6 +400,79 @@ export default function Demo() {
465400
}
466401
```
467402

403+
Inspector
404+
405+
```tsx mdx:preview
406+
import React from 'react';
407+
import JsonView from '@uiw/react-json-view';
408+
409+
const object = [
410+
{
411+
"_id": "56dcf573b09c217d39fd7621",
412+
"name": "Howard Christensen",
413+
"email": "[email protected]",
414+
"phone": "+1 (830) 529-3176",
415+
"address": "511 Royce Street, Hilltop, Tennessee, 9712"
416+
},
417+
{
418+
"_id": "56dcf57323630b06251e93cd",
419+
"name": "Eleanor Lynn",
420+
"email": "[email protected]",
421+
"phone": "+1 (911) 576-2345",
422+
"address": "547 Dearborn Court, Trona, California, 8629"
423+
},
424+
{
425+
"_id": "56dcf5738279cac6b081e512",
426+
"name": "Baxter Mooney",
427+
"email": "[email protected]",
428+
"phone": "+1 (954) 456-3456",
429+
"address": "349 Cumberland Walk, Washington, Alaska, 3154"
430+
},
431+
{
432+
"_id": "56dcf57303accabd43740957",
433+
"name": "Calhoun Tyson",
434+
"email": "[email protected]",
435+
"phone": "+1 (818) 456-2529",
436+
"address": "367 Lyme Avenue, Ladera, Louisiana, 6292"
437+
},
438+
]
439+
440+
const customTheme = {
441+
'--w-rjv-background-color': '#fff',
442+
'--w-rjv-border-left-width': 0,
443+
'--w-rjv-color': '#881391',
444+
'--w-rjv-type-int-color': '#881391',
445+
};
446+
447+
export default function Demo() {
448+
return (
449+
<JsonView
450+
value={object}
451+
style={customTheme}
452+
quotes=""
453+
enableClipboard={false}
454+
displayDataTypes={false}
455+
components={{
456+
braces: ({ level }) => level !== 1 && <span />,
457+
countInfo: ({ level, count, visible }) => {
458+
if (level === 1) {
459+
return <span />;
460+
//return visible ? <span>{Array.from({ length: 3 }, () => 'Object').join(', ')}</span> : <span />
461+
}
462+
return <span style={{ color: '#222' }}>Object</span>;
463+
},
464+
ellipsis: ({ level, count }) => {
465+
if (level === 1) {
466+
return <span>{Array.from({ length: 3 }, () => 'Object').join(', ')}</span>;
467+
}
468+
return <span />;
469+
},
470+
}}
471+
/>
472+
);
473+
}
474+
```
475+
468476
## Editor JSON
469477

470478
```tsx mdx:preview

core/src/comps/ellipsis.test.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Ellipsis } from './ellipsis';
33

44
it('renders <Ellipsis /> test case', () => {
55
const component = renderer.create(
6-
<Ellipsis />,
6+
<Ellipsis count={1} level={1} />,
77
);
88
let tree = component.toJSON();
99
expect(tree).toHaveProperty('type');
@@ -21,7 +21,7 @@ it('renders <Ellipsis /> test case', () => {
2121

2222
it('renders <Ellipsis /> render props test case', () => {
2323
const component = renderer.create(
24-
<Ellipsis render={(props) => <del {...props}>xxx</del>} />,
24+
<Ellipsis count={1} level={1} render={(props) => <del {...props}>xxx</del>} />,
2525
);
2626
let tree = component.toJSON();
2727
expect(tree).toHaveProperty('type');
@@ -30,6 +30,8 @@ it('renders <Ellipsis /> render props test case', () => {
3030
expect(tree).toHaveProperty('type', 'del');
3131
expect(tree).toHaveProperty('props', {
3232
className: 'w-rjv-ellipsis ',
33+
count: 1,
34+
level: 1,
3335
style: {
3436
cursor: 'pointer',
3537
}

core/src/comps/ellipsis.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { FC, PropsWithChildren } from 'react';
22

33
export interface EllipsisProps extends React.HTMLAttributes<HTMLSpanElement> {
4+
count: number;
5+
level: number;
46
render?: (props: EllipsisProps) => JSX.Element;
57
}
6-
export const Ellipsis: FC<PropsWithChildren<EllipsisProps>> = ({ style, render, ...props }) => {
8+
export const Ellipsis: FC<PropsWithChildren<EllipsisProps>> = ({ style, render, count, level, ...props }) => {
79
const styl = { cursor: 'pointer', ...style };
810
const className = `w-rjv-ellipsis ${props.className || ''}`;
9-
if (render) return render({ style: styl, ...props, className });
11+
if (render) return render({ style: styl, count, level, ...props, className });
1012
return (
1113
<span className={className} style={styl} {...props}>
1214
...

core/src/comps/meta.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@ import { Label, LabelProps } from '../value';
33
export interface MetaProps extends LabelProps {
44
isArray?: boolean;
55
start?: boolean;
6-
render?: (props: Pick<MetaProps, 'start' | 'isArray' | 'className' | 'children'>) => JSX.Element;
6+
level?: number;
7+
render?: (props: Pick<MetaProps, 'start' | 'level' | 'isArray' | 'className' | 'children'>) => JSX.Element;
78
}
89

910
export function Meta(props: MetaProps) {
10-
const { isArray = false, start = false, className, render, ...reset } = props;
11+
const { isArray = false, start = false, level, className, render, ...reset } = props;
1112
const mark = isArray ? '[]' : '{}';
1213
const cls = `w-rjv-${isArray ? 'brackets' : 'curlybraces'}-${start ? 'start' : 'end'} ${className || ''}`;
1314
const color = `var(--w-rjv-${isArray ? 'brackets' : 'curlybraces'}-color, #236a7c)`;
14-
if (render)
15-
return render({
15+
if (render) {
16+
const viev = render({
1617
isArray,
18+
level,
1719
className: cls,
1820
style: { color },
1921
children: start ? mark.charAt(0) : mark.charAt(1),
2022
...reset,
2123
});
24+
if (viev) {
25+
return viev;
26+
}
27+
}
2228
return (
2329
<Label color={color} className={cls} {...reset}>
2430
{start ? mark.charAt(0) : mark.charAt(1)}

core/src/editor/index.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import renderer from 'react-test-renderer';
2+
import JsonViewEditor from './';
3+
4+
it('renders <JsonViewEditor /> test case', () => {
5+
const component = renderer.create(
6+
<JsonViewEditor />,
7+
);
8+
const tree = component.toJSON();
9+
expect(tree).toHaveProperty('type');
10+
expect(tree).toHaveProperty('props');
11+
expect(tree).toHaveProperty('type', 'div');
12+
expect(tree).toHaveProperty('props.style', {
13+
lineHeight: 1.4,
14+
fontFamily: 'var(--w-rjv-font-family, Menlo, monospace)',
15+
color: 'var(--w-rjv-color, #002b36)',
16+
backgroundColor: 'var(--w-rjv-background-color, #00000000)'
17+
});
18+
// @ts-ignore
19+
expect(tree.children).toHaveLength(3);
20+
expect(tree).toHaveProperty('props.className', 'w-json-view-container w-rjv w-rjv-inner');
21+
expect(tree).toHaveProperty('props.onMouseEnter');
22+
expect(tree).toHaveProperty('props.onMouseLeave');
23+
expect(tree).toHaveProperty('children[0].type', 'div');
24+
expect(tree).toHaveProperty('children[0].props.style', {
25+
"alignItems": "center", "display": "inline-flex"
26+
});
27+
expect(tree).toHaveProperty('children[0].props.onClick');
28+
});

core/src/editor/objectKey.test.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import renderer from 'react-test-renderer';
2+
import { ObjectKey } from './objectKey';
3+
4+
it('renders <ObjectKey /> test case', () => {
5+
const component = renderer.create(
6+
<ObjectKey />,
7+
);
8+
const tree = component.toJSON();
9+
10+
expect(tree).toHaveProperty('type');
11+
expect(tree).toHaveProperty('props');
12+
expect(tree).toHaveProperty('children', null);
13+
expect(tree).toHaveProperty('type', 'span');
14+
expect(tree).toHaveProperty('props.style', {
15+
color: undefined,
16+
fontSize: undefined,
17+
opacity: undefined,
18+
paddingRight: undefined
19+
})
20+
expect(tree).toHaveProperty('props.onClick');
21+
expect(tree).toHaveProperty('props.onFocus');
22+
expect(tree).toHaveProperty('props.onBlur');
23+
expect(tree).toHaveProperty('props.onKeyDown');
24+
expect(tree).toHaveProperty('props.autoFocus', false);
25+
expect(tree).toHaveProperty('props.contentEditable', false);
26+
expect(tree).toHaveProperty('props.spellCheck', false);
27+
expect(tree).toHaveProperty('props.suppressContentEditableWarning', true);
28+
});
29+
30+
it('renders <ObjectKey /> render test case', () => {
31+
const component = renderer.create(
32+
<ObjectKey
33+
label="good"
34+
keyName="key"
35+
quotes="'"
36+
render={({ value, keyName, label, parentName, ...props}) => {
37+
return <span {...props}>xx{label}</span>
38+
}}
39+
/>,
40+
);
41+
const tree = component.toJSON();
42+
expect(tree).toHaveProperty('type');
43+
expect(tree).toHaveProperty('props');
44+
expect(tree).toHaveProperty('children', ['xx', 'good']);
45+
expect(tree).toHaveProperty('type', 'span');
46+
expect(tree).toHaveProperty('props.style', {
47+
color: undefined,
48+
fontSize: undefined,
49+
opacity: undefined,
50+
paddingRight: undefined
51+
})
52+
expect(tree).toHaveProperty('props.onClick');
53+
expect(tree).toHaveProperty('props.onFocus');
54+
expect(tree).toHaveProperty('props.onBlur');
55+
expect(tree).toHaveProperty('props.onKeyDown');
56+
expect(tree).toHaveProperty('props.autoFocus', false);
57+
expect(tree).toHaveProperty('props.contentEditable', false);
58+
expect(tree).toHaveProperty('props.spellCheck', false);
59+
expect(tree).toHaveProperty('props.suppressContentEditableWarning', true);
60+
});

core/src/editor/objectKey.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const ObjectKey: FC<ObjectKeyProps<object>>= (props) => {
8282
return render({ className, ...reset, ...spanProps, parentName, label: curentLabel as string, children: editable ? curentLabel : content, ref: $edit });
8383
}
8484

85-
return <Label className={className} {...reset} autoFocus={editable} {...spanProps} ref={$edit} />;
85+
return <Label className={className} {...reset} autoFocus={editable} {...spanProps} ref={$edit} />;
8686
}
8787

8888

0 commit comments

Comments
 (0)