Skip to content

Commit f37551f

Browse files
committed
fix: fix CountInfoExtra render issue.
1 parent f8df42f commit f37551f

File tree

6 files changed

+82
-23
lines changed

6 files changed

+82
-23
lines changed

core/src/comps/Ellipsis.tsx

Lines changed: 0 additions & 20 deletions
This file was deleted.

core/src/comps/NestedOpen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Copied } from './Copied';
55
import { CountInfoExtraComps } from '../section/CountInfoExtra';
66
import { CountInfoComp } from '../section/CountInfo';
77
import { Arrow, BracketsOpen, BracketsClose } from '../symbol';
8-
import { Ellipsis } from './Ellipsis';
8+
import { EllipsisComp } from '../section/Ellipsis';
99
import { SetComp, MapComp } from '../types';
1010

1111
export interface NestedOpenProps<T extends object> {
@@ -48,7 +48,7 @@ export const NestedOpen = <T extends object>(props: NestedOpenProps<T>) => {
4848
<SetComp value={initialValue} />
4949
<MapComp value={initialValue} />
5050
<BracketsOpen isBrackets={isArray || isMySet} />
51-
<Ellipsis keyName={keyName!} value={value} isExpanded={isExpanded} />
51+
<EllipsisComp keyName={keyName!} value={value} isExpanded={isExpanded} />
5252
<BracketsClose isVisiable={isExpanded || !showArrow} isBrackets={isArray || isMySet} />
5353
<CountInfoComp value={value} keyName={keyName!} />
5454
<CountInfoExtraComps value={value} keyName={keyName!} />
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { screen, render } from '@testing-library/react';
2+
import JsonView from '../';
3+
4+
const avatar = 'https://i.imgur.com/MK3eW3As.jpg';
5+
const example = {
6+
avatar,
7+
};
8+
9+
it('renders <JsonView.CountInfoExtra /> test case', async () => {
10+
const { container } = render(
11+
<JsonView value={example}>
12+
<JsonView.CountInfoExtra data-testid="countInfo">xx</JsonView.CountInfoExtra>
13+
</JsonView>,
14+
);
15+
expect(container.firstElementChild).toBeInstanceOf(Element);
16+
const countInfo = screen.getByTestId('countInfo');
17+
expect(countInfo.className).toEqual('w-rjv-object-extra');
18+
expect(countInfo.style).toHaveProperty('padding-left', '8px');
19+
});
20+
21+
it('renders <JsonView.CountInfoExtra /> test case', async () => {
22+
const { container, debug } = render(
23+
<JsonView value={example}>
24+
<JsonView.CountInfoExtra
25+
data-testid="countInfo"
26+
render={(props) => {
27+
return <span {...props}>xx</span>;
28+
}}
29+
/>
30+
</JsonView>,
31+
);
32+
expect(container.firstElementChild).toBeInstanceOf(Element);
33+
const countInfo = screen.getByTestId('countInfo');
34+
expect(countInfo.className).toEqual('w-rjv-object-extra');
35+
expect(countInfo.style).toHaveProperty('padding-left', '8px');
36+
expect(countInfo.innerHTML).toEqual('xx');
37+
});

core/src/section/CountInfoExtra.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const CountInfoExtraComps = <T extends object>(
2121
const { value = {}, keyName, ...other } = props;
2222
const { CountInfoExtra: Comp = {} } = useSectionStore();
2323
const { as, render, ...reset } = Comp;
24-
if (!render || !reset.children) return null;
24+
if (!render && !reset.children) return null;
2525
const Elm = as || 'span';
2626
const isRender = render && typeof render === 'function';
2727
const elmProps = { ...reset, ...other };

core/src/section/Ellipsis.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,22 @@ export const Ellipsis = <K extends TagType>(props: SectionElement<K>) => {
99
};
1010

1111
Ellipsis.displayName = 'JVR.Ellipsis';
12+
13+
export interface EllipsisCompProps<T extends object> {
14+
value?: T;
15+
keyName: string | number;
16+
isExpanded: boolean;
17+
}
18+
19+
export const EllipsisComp = <T extends object>({ isExpanded, value, keyName }: EllipsisCompProps<T>) => {
20+
const { Ellipsis: Comp = {} } = useSectionStore();
21+
const { as, render, ...reset } = Comp;
22+
const Elm = as || 'span';
23+
const child =
24+
render && typeof render === 'function' && render({ ...reset, 'data-expanded': isExpanded }, { value, keyName });
25+
if (child) return child;
26+
if (!isExpanded) return null;
27+
return <Elm {...reset} />;
28+
};
29+
30+
EllipsisComp.displayName = 'JVR.EllipsisComp';

core/src/section/KeyName.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { type FC, type PropsWithChildren } from 'react';
12
import { type TagType } from '../store/Types';
23
import { type SectionElement, useSectionStore } from '../store/Section';
34
import { useSectionRender } from '../utils/useRender';
@@ -9,3 +10,25 @@ export const KeyName = <K extends TagType>(props: SectionElement<K>) => {
910
};
1011

1112
KeyName.displayName = 'JVR.KeyName';
13+
14+
type KeyNameCompProps = {
15+
keyName: string | number;
16+
value?: unknown;
17+
};
18+
19+
export const KeyNameComp: FC<PropsWithChildren<KeyNameCompProps>> = (props) => {
20+
const { children, value, keyName } = props;
21+
const isNumber = typeof children === 'number';
22+
const style: React.CSSProperties = {
23+
color: isNumber ? 'var(--w-rjv-key-number, #268bd2)' : 'var(--w-rjv-key-string, #002b36)',
24+
};
25+
const { KeyName: Comp = {} } = useSectionStore();
26+
const { as, render, ...reset } = Comp;
27+
reset.style = { ...reset.style, ...style };
28+
const Elm = as || 'span';
29+
const child = render && typeof render === 'function' && render({ ...reset, children }, { value, keyName });
30+
if (child) return child;
31+
return <Elm {...reset}>{children}</Elm>;
32+
};
33+
34+
KeyNameComp.displayName = 'JVR.KeyNameComp';

0 commit comments

Comments
 (0)