Skip to content

Commit 795c486

Browse files
committed
feat: Remove findDOMNode
1 parent fb18ff2 commit 795c486

File tree

6 files changed

+19
-40
lines changed

6 files changed

+19
-40
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"dependencies": {
4444
"@babel/runtime": "^7.20.7",
4545
"classnames": "^2.2.1",
46-
"rc-util": "^5.44.1",
46+
"@rc-component/util": "^1.2.0",
4747
"resize-observer-polyfill": "^1.5.1"
4848
},
4949
"devDependencies": {

src/SingleObserver/DomWrapper.tsx

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

src/SingleObserver/index.tsx

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
2-
import { supportRef, useComposeRef, getNodeRef } from 'rc-util/lib/ref';
1+
import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode';
2+
import { supportRef, useComposeRef, getNodeRef } from '@rc-component/util/lib/ref';
33
import * as React from 'react';
44
import type { ResizeObserverProps } from '..';
55
import { CollectionContext } from '../Collection';
66
import { observe, unobserve } from '../utils/observerUtil';
7-
import DomWrapper from './DomWrapper';
87

98
export interface SingleObserverProps extends ResizeObserverProps {
109
children: React.ReactElement | ((ref: React.RefObject<Element>) => React.ReactElement);
@@ -13,7 +12,6 @@ export interface SingleObserverProps extends ResizeObserverProps {
1312
function SingleObserver(props: SingleObserverProps, ref: React.Ref<HTMLElement>) {
1413
const { children, disabled } = props;
1514
const elementRef = React.useRef<Element>(null);
16-
const wrapperRef = React.useRef<DomWrapper>(null);
1715

1816
const onCollectionResize = React.useContext(CollectionContext);
1917

@@ -36,15 +34,12 @@ function SingleObserver(props: SingleObserverProps, ref: React.Ref<HTMLElement>)
3634

3735
const mergedRef = useComposeRef(originRef, elementRef);
3836

39-
const getDom = () =>
40-
findDOMNode<HTMLElement>(elementRef.current) ||
41-
// Support `nativeElement` format
42-
(elementRef.current && typeof elementRef.current === 'object'
43-
? findDOMNode<HTMLElement>((elementRef.current as any)?.nativeElement)
44-
: null) ||
45-
findDOMNode<HTMLElement>(wrapperRef.current);
37+
const getDomElement = () => {
38+
return getDOM( elementRef.current ) as HTMLElement
39+
}
40+
4641

47-
React.useImperativeHandle(ref, () => getDom());
42+
React.useImperativeHandle(ref, () => getDomElement());
4843

4944
// =========================== Observe ============================
5045
const propsRef = React.useRef<SingleObserverProps>(props);
@@ -98,7 +93,7 @@ function SingleObserver(props: SingleObserverProps, ref: React.Ref<HTMLElement>)
9893

9994
// Dynamic observe
10095
React.useEffect(() => {
101-
const currentElement: HTMLElement = getDom();
96+
const currentElement: HTMLElement = getDomElement();
10297

10398
if (currentElement && !disabled) {
10499
observe(currentElement, onInternalResize);
@@ -109,13 +104,11 @@ function SingleObserver(props: SingleObserverProps, ref: React.Ref<HTMLElement>)
109104

110105
// ============================ Render ============================
111106
return (
112-
<DomWrapper ref={wrapperRef}>
113-
{canRef
107+
canRef
114108
? React.cloneElement(mergedChildren as any, {
115109
ref: mergedRef,
116110
})
117-
: mergedChildren}
118-
</DomWrapper>
111+
: mergedChildren
119112
);
120113
}
121114

src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
2-
import toArray from 'rc-util/lib/Children/toArray';
3-
import { warning } from 'rc-util/lib/warning';
2+
import toArray from '@rc-component/util/lib/Children/toArray';
3+
import { warning } from '@rc-component/util/lib/warning';
44
import SingleObserver from './SingleObserver';
55
import { Collection } from './Collection';
66

tests/index.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ describe('ResizeObserver', () => {
9898

9999
wrapper.triggerResize();
100100
await Promise.resolve();
101-
expect(wrapper.exists('DomWrapper')).toBeTruthy(); // Dom exist
101+
expect(wrapper.exists('DomWrapper')).toBeFalsy(); // Dom exist
102102
expect(onResize).toHaveBeenCalled();
103103
});
104104

@@ -220,7 +220,7 @@ describe('ResizeObserver', () => {
220220
});
221221
});
222222

223-
it('should listen even not ref-able', async () => {
223+
it('should not listen even not ref-able', async () => {
224224
const Wrapper = props => <>{props.children}</>;
225225
const onResize = jest.fn();
226226

@@ -235,7 +235,7 @@ describe('ResizeObserver', () => {
235235
wrapper.triggerResize();
236236
await Promise.resolve();
237237

238-
expect(onResize).toHaveBeenCalled();
238+
expect(onResize).not.toHaveBeenCalled();
239239
});
240240

241241
it('support renderProps', () => {

tests/ref.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('ResizeObserver.ref', () => {
2525
expect(resizeRef.current).toEqual(container.querySelector('.little'));
2626
});
2727

28-
it('ignore invalidate forward', () => {
28+
it('should return null when forward ref is invalid', () => {
2929
const My = React.forwardRef((_, ref) => {
3030
React.useImperativeHandle(ref, () => 233);
3131

@@ -34,12 +34,12 @@ describe('ResizeObserver.ref', () => {
3434

3535
const resizeRef = React.createRef<HTMLDivElement>();
3636

37-
const { container } = render(
37+
render(
3838
<ResizeObserver ref={resizeRef}>
3939
<My />
4040
</ResizeObserver>,
4141
);
4242

43-
expect(resizeRef.current).toEqual(container.querySelector('.little'));
43+
expect(resizeRef.current).toBeNull();
4444
});
4545
});

0 commit comments

Comments
 (0)