Skip to content

Commit ecc169b

Browse files
authored
feat: compatible for nativeElement (#191)
* chore: update test * test: add test case * fix: lint * test: add test case
1 parent fe320b1 commit ecc169b

6 files changed

Lines changed: 81 additions & 30 deletions

File tree

.fatherrc.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
export default {
2-
cjs: 'babel',
3-
esm: { type: 'babel', importLibToEs: true },
4-
preCommit: {
5-
eslint: true,
6-
prettier: true,
7-
},
8-
runtimeHelpers: true,
9-
};
1+
import { defineConfig } from 'father';
2+
3+
export default defineConfig({
4+
plugins: ['@rc-component/father-plugin'],
5+
});

.github/workflows/react-component-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515

1616
- uses: actions/setup-node@v1
1717
with:
18-
node-version: '12'
18+
node-version: '16'
1919

2020
- name: cache package-lock.json
2121
uses: actions/cache@v2

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@
3636
"postpublish": "npm run gh-pages",
3737
"lint": "eslint src/ --ext .ts,.tsx,.jsx,.js,.md",
3838
"prettier": "prettier --write \"**/*.{ts,tsx,js,jsx,json,md}\"",
39-
"test": "father test",
40-
"coverage": "father test --coverage",
39+
"test": "rc-test",
40+
"coverage": "rc-test --coverage",
4141
"now-build": "npm run build"
4242
},
4343
"dependencies": {
4444
"@babel/runtime": "^7.20.7",
4545
"classnames": "^2.2.1",
46-
"rc-util": "^5.27.0",
46+
"rc-util": "^5.38.0",
4747
"resize-observer-polyfill": "^1.5.1"
4848
},
4949
"devDependencies": {
50+
"@rc-component/father-plugin": "^1.0.0",
51+
"@testing-library/react": "^12.1.5",
5052
"@types/classnames": "^2.2.9",
5153
"@types/jest": "^26.0.6",
5254
"@types/react": "^16.9.2",
@@ -56,18 +58,18 @@
5658
"cross-env": "^7.0.2",
5759
"dumi": "^1.1.12",
5860
"enzyme": "^3.0.0",
59-
"enzyme-adapter-react-16": "^1.0.1",
61+
"enzyme-adapter-react-16": "^1.15.6",
6062
"enzyme-to-json": "^3.4.0",
61-
"father": "^2.13.4",
63+
"father": "^4.0.0",
6264
"gh-pages": "^3.1.0",
6365
"glob": "^7.1.6",
6466
"less": "^3.10.3",
6567
"np": "^6.2.4",
6668
"prettier": "^2.1.1",
6769
"pretty-quick": "^3.0.1",
70+
"rc-test": "^7.0.15",
6871
"react": "^16.0.0",
6972
"react-dom": "^16.0.0",
70-
"react-test-renderer": "^16.0.0",
7173
"regenerator-runtime": "^0.13.7"
7274
},
7375
"peerDependencies": {

src/SingleObserver/index.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { composeRef, supportRef } from 'rc-util/lib/ref';
2-
import * as React from 'react';
31
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
4-
import { observe, unobserve } from '../utils/observerUtil';
2+
import { supportRef, useComposeRef } from 'rc-util/lib/ref';
3+
import * as React from 'react';
54
import type { ResizeObserverProps } from '..';
6-
import DomWrapper from './DomWrapper';
75
import { CollectionContext } from '../Collection';
6+
import { observe, unobserve } from '../utils/observerUtil';
7+
import DomWrapper from './DomWrapper';
88

99
export interface SingleObserverProps extends ResizeObserverProps {
1010
children: React.ReactElement | ((ref: React.RefObject<Element>) => React.ReactElement);
@@ -34,13 +34,15 @@ function SingleObserver(props: SingleObserverProps, ref: React.Ref<HTMLElement>)
3434
!isRenderProps && React.isValidElement(mergedChildren) && supportRef(mergedChildren);
3535
const originRef: React.Ref<Element> = canRef ? (mergedChildren as any).ref : null;
3636

37-
const mergedRef = React.useMemo(
38-
() => composeRef<Element>(originRef, elementRef),
39-
[originRef, elementRef],
40-
);
37+
const mergedRef = useComposeRef(originRef, elementRef);
4138

4239
const getDom = () =>
43-
findDOMNode<HTMLElement>(elementRef.current) || findDOMNode<HTMLElement>(wrapperRef.current);
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);
4446

4547
React.useImperativeHandle(ref, () => getDom());
4648

tests/ref.spec.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { render } from '@testing-library/react';
2+
import React from 'react';
3+
import ResizeObserver from '../src';
4+
5+
describe('ResizeObserver.ref', () => {
6+
it('support nativeElement', () => {
7+
const My = React.forwardRef((_, ref) => {
8+
const divRef = React.useRef<HTMLDivElement>(null);
9+
10+
React.useImperativeHandle(ref, () => ({
11+
nativeElement: divRef.current,
12+
}));
13+
14+
return <div ref={divRef} className="little" />;
15+
});
16+
17+
const resizeRef = React.createRef<HTMLDivElement>();
18+
19+
const { container } = render(
20+
<ResizeObserver ref={resizeRef}>
21+
<My />
22+
</ResizeObserver>,
23+
);
24+
25+
expect(resizeRef.current).toEqual(container.querySelector('.little'));
26+
});
27+
28+
it('ignore invalidate forward', () => {
29+
const My = React.forwardRef((_, ref) => {
30+
React.useImperativeHandle(ref, () => 233);
31+
32+
return <div className="little" />;
33+
});
34+
35+
const resizeRef = React.createRef<HTMLDivElement>();
36+
37+
const { container } = render(
38+
<ResizeObserver ref={resizeRef}>
39+
<My />
40+
</ResizeObserver>,
41+
);
42+
43+
expect(resizeRef.current).toEqual(container.querySelector('.little'));
44+
});
45+
});

tsconfig.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
"target": "esnext",
44
"moduleResolution": "node",
55
"baseUrl": "./",
6-
"jsx": "preserve",
6+
"jsx": "react",
77
"declaration": true,
88
"skipLibCheck": true,
99
"esModuleInterop": true,
1010
"paths": {
11-
"@/*": ["src/*"],
12-
"@@/*": ["src/.umi/*"],
13-
"rc-resize-observer": ["src/index.tsx"]
11+
"@/*": [
12+
"src/*"
13+
],
14+
"@@/*": [
15+
"src/.umi/*"
16+
],
17+
"rc-resize-observer": [
18+
"src/index.tsx"
19+
]
1420
}
1521
}
16-
}
22+
}

0 commit comments

Comments
 (0)