Skip to content

Commit bfb6dc7

Browse files
committed
fix: fix ref issue.
1 parent e8f0d44 commit bfb6dc7

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

core/src/Container.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import userEvent from '@testing-library/user-event';
2+
import { screen, render, waitFor } from '@testing-library/react';
3+
import JsonView from './';
4+
import React from 'react';
5+
6+
const avatar = 'https://i.imgur.com/MK3eW3As.jpg';
7+
const example = {
8+
avatar,
9+
};
10+
11+
it('renders <JsonView /> Container test case', async () => {
12+
const user = userEvent.setup();
13+
const divref = React.createRef<HTMLDivElement>();
14+
const { container } = render(
15+
<JsonView value={example} ref={divref}>
16+
<JsonView.Copied data-testid="copied" />
17+
<JsonView.CountInfoExtra data-testid="infoExtra" />
18+
</JsonView>,
19+
);
20+
expect(container.firstElementChild).toBeInstanceOf(Element);
21+
await user.hover(container.lastElementChild!);
22+
const copied = screen.getByTestId('copied');
23+
expect(copied.style).toHaveProperty('height', '1em');
24+
expect(copied.style).toHaveProperty('width', '1em');
25+
expect(copied.style).toHaveProperty('cursor', 'pointer');
26+
expect(copied.style).toHaveProperty('vertical-align', 'middle');
27+
expect(copied.style).toHaveProperty('margin-left', '5px');
28+
await user.unhover(container.lastElementChild!);
29+
const uncopied = screen.getByTestId('infoExtra');
30+
expect(uncopied.nextElementSibling).toBeNull();
31+
await waitFor(() => {
32+
expect(divref.current instanceof HTMLDivElement).toBeTruthy();
33+
});
34+
});

core/src/Container.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef, useId, useImperativeHandle, useRef } from 'react';
1+
import React, { forwardRef, useId } from 'react';
22
import { NestedClose } from './comps/NestedClose';
33
import { NestedOpen } from './comps/NestedOpen';
44
import { KeyValues } from './comps/KeyValues';
@@ -14,17 +14,15 @@ export interface ContainerProps<T extends object> extends React.HTMLAttributes<H
1414
}
1515
export const Container = forwardRef(<T extends object>(props: ContainerProps<T>, ref: React.Ref<HTMLDivElement>) => {
1616
const { className = '', children, parentValue, keyid, level = 1, value, initialValue, keyName, ...elmProps } = props;
17-
const $ref = useRef<HTMLDivElement>(null);
1817
const dispatch = useShowToolsDispatch();
19-
useImperativeHandle<HTMLDivElement | null, HTMLDivElement | null>(ref, () => $ref.current, [$ref]);
2018
const subkeyid = useId();
2119
const defaultClassNames = [className, 'w-rjv-inner'].filter(Boolean).join(' ');
2220
const reset: React.HTMLAttributes<HTMLDivElement> = {
2321
onMouseEnter: () => dispatch({ [subkeyid]: true }),
2422
onMouseLeave: () => dispatch({ [subkeyid]: false }),
2523
};
2624
return (
27-
<div className={defaultClassNames} {...elmProps} {...reset}>
25+
<div className={defaultClassNames} ref={ref} {...elmProps} {...reset}>
2826
<NestedOpen expandKey={subkeyid} value={value} level={level} keyName={keyName} initialValue={initialValue} />
2927
<KeyValues expandKey={subkeyid} value={value} level={level} />
3028
<NestedClose expandKey={subkeyid} value={value} level={level} />

0 commit comments

Comments
 (0)