Skip to content

Commit 9c2d3e1

Browse files
authored
refactor: Update rc-util ref func to resolve React 19 warning (#24)
* chore: init * test: test driven * fix: ref warning
1 parent a518b19 commit 9c2d3e1

File tree

7 files changed

+41
-38
lines changed

7 files changed

+41
-38
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ open http://localhost:8000
3232
## Usage
3333

3434
```js | pure
35-
import Portal from 'rc-portal';
35+
import Portal from '@rc-component/portal';
3636

3737
const Demo = () => {
3838
return <Portal open>Hello World</Portal>;

package.json

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rc-component/portal",
3-
"version": "1.1.2",
3+
"version": "2.0.0",
44
"description": "React Portal Component",
55
"keywords": [
66
"react",
@@ -27,29 +27,29 @@
2727
],
2828
"scripts": {
2929
"compile": "father build",
30-
"prepare": "dumi setup",
3130
"deploy": "npm run docs:build && npm run docs:deploy",
3231
"docs:build": "dumi build",
3332
"docs:deploy": "gh-pages -d docs-dist",
3433
"lint": "eslint src/ --ext .tsx,.ts",
3534
"lint:tsc": "tsc -p tsconfig.json --noEmit",
3635
"now-build": "npm run docs:build",
37-
"prepublishOnly": "npm run compile && np --no-cleanup --yolo --no-publish",
36+
"prepare": "dumi setup",
37+
"prepublishOnly": "npm run compile && rc-np",
3838
"prettier": "prettier --write \"**/*.{js,jsx,tsx,ts,less,md,json}\"",
3939
"start": "dumi dev",
4040
"test": "npm run test:server && npm run test:coverage",
41-
"test:client": "umi-test --testPathIgnorePatterns=ssr.test.tsx --testPathIgnorePatterns=ssr.test.tsx",
41+
"test:client": "rc-test --testPathIgnorePatterns=ssr.test.tsx --testPathIgnorePatterns=ssr.test.tsx",
4242
"test:coverage": "npm run test:client -- --coverage",
43-
"test:server": "umi-test --env=node -- tests/ssr.test.tsx",
43+
"test:server": "rc-test --env=node -- tests/ssr.test.tsx",
4444
"watch": "father dev"
4545
},
4646
"dependencies": {
47-
"@babel/runtime": "^7.18.0",
48-
"classnames": "^2.3.2",
49-
"rc-util": "^5.24.4"
47+
"@rc-component/util": "^1.2.1",
48+
"classnames": "^2.3.2"
5049
},
5150
"devDependencies": {
52-
"@rc-component/father-plugin": "^1.0.0",
51+
"@rc-component/father-plugin": "^2.0.2",
52+
"@rc-component/np": "^1.0.3",
5353
"@testing-library/jest-dom": "^5.16.4",
5454
"@testing-library/react": "^13.0.0",
5555
"@types/jest": "^26.0.20",
@@ -62,19 +62,17 @@
6262
"father": "^4.0.0",
6363
"gh-pages": "^6.1.1",
6464
"glob": "^10.0.0",
65-
"np": "^5.0.3",
6665
"prettier": "^2.1.2",
67-
"react": "^18.0.0",
68-
"react-dom": "^18.0.0",
69-
"typescript": "^5.0.0",
70-
"cheerio": "1.0.0-rc.12",
71-
"umi-test": "^1.9.7"
66+
"rc-test": "^7.1.2",
67+
"react": "^19.0.0",
68+
"react-dom": "^19.0.0",
69+
"typescript": "^5.0.0"
7270
},
7371
"peerDependencies": {
74-
"react": ">=16.9.0",
75-
"react-dom": ">=16.9.0"
72+
"react": ">=18.0.0",
73+
"react-dom": ">=18.0.0"
7674
},
7775
"engines": {
78-
"node": ">=8.x"
76+
"node": ">=12.x"
7977
}
8078
}

src/Portal.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import canUseDom from '@rc-component/util/lib/Dom/canUseDom';
2+
import {
3+
getNodeRef,
4+
supportRef,
5+
useComposeRef,
6+
} from '@rc-component/util/lib/ref';
7+
import warning from '@rc-component/util/lib/warning';
18
import * as React from 'react';
29
import { createPortal } from 'react-dom';
3-
import canUseDom from 'rc-util/lib/Dom/canUseDom';
4-
import warning from 'rc-util/lib/warning';
5-
import { supportRef, useComposeRef } from 'rc-util/lib/ref';
610
import OrderContext from './Context';
11+
import { inlineMock } from './mock';
712
import useDom from './useDom';
813
import useScrollLocker from './useScrollLocker';
9-
import { inlineMock } from './mock';
1014

1115
export type ContainerType = Element | DocumentFragment;
1216

@@ -109,7 +113,7 @@ const Portal = React.forwardRef<any, PortalProps>((props, ref) => {
109113
let childRef: React.Ref<any> = null;
110114

111115
if (children && supportRef(children) && ref) {
112-
({ ref: childRef } = children as any);
116+
childRef = getNodeRef(children);
113117
}
114118

115119
const mergedRef = useComposeRef(childRef, ref);

src/useDom.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 useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
3-
import canUseDom from 'rc-util/lib/Dom/canUseDom';
2+
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
3+
import canUseDom from '@rc-component/util/lib/Dom/canUseDom';
44
import OrderContext from './Context';
55
import type { QueueCreate } from './Context';
66

src/useScrollLocker.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
2-
import { updateCSS, removeCSS } from 'rc-util/lib/Dom/dynamicCSS';
3-
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
4-
import { getTargetScrollBarSize } from 'rc-util/lib/getScrollBarSize';
2+
import { updateCSS, removeCSS } from '@rc-component/util/lib/Dom/dynamicCSS';
3+
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
4+
import { getTargetScrollBarSize } from '@rc-component/util/lib/getScrollBarSize';
55
import { isBodyOverflowing } from './util';
66

77
const UNIQUE_ID = `rc-util-locker-${Date.now()}`;

tests/index.test.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React from 'react';
21
import { render } from '@testing-library/react';
2+
import React from 'react';
33
import Portal from '../src';
44

55
global.isOverflow = true;
@@ -13,7 +13,7 @@ jest.mock('../src/util', () => {
1313
});
1414

1515
// Revert `useLayoutEffect` back to real one since we should keep order for test
16-
jest.mock('rc-util/lib/hooks/useLayoutEffect', () => {
16+
jest.mock('@rc-component/util/lib/hooks/useLayoutEffect', () => {
1717
const origin = jest.requireActual('react');
1818
return origin.useLayoutEffect;
1919
});
@@ -210,7 +210,8 @@ describe('Portal', () => {
210210
expect(portalRef.current).toBeFalsy();
211211
});
212212

213-
it('not support FC', () => {
213+
it('no warning for React 19 ref', () => {
214+
const errSpy = jest.spyOn(console, 'error');
214215
const elementRef = React.createRef<HTMLParagraphElement>();
215216
const portalRef = React.createRef();
216217

@@ -222,8 +223,9 @@ describe('Portal', () => {
222223
</Portal>,
223224
);
224225

225-
expect(elementRef.current).toBeFalsy();
226-
expect(portalRef.current).toBeFalsy();
226+
expect(errSpy).not.toHaveBeenCalled();
227+
228+
errSpy.mockRestore();
227229
});
228230
});
229231

tests/webkit.test.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react';
22
import { render } from '@testing-library/react';
33
import Portal from '../src';
4-
import { updateCSS } from 'rc-util/lib/Dom/dynamicCSS';
54

65
jest.mock('../src/util', () => {
76
const origin = jest.requireActual('../src/util');
@@ -12,14 +11,14 @@ jest.mock('../src/util', () => {
1211
});
1312

1413
// Revert `useLayoutEffect` back to real one since we should keep order for test
15-
jest.mock('rc-util/lib/hooks/useLayoutEffect', () => {
14+
jest.mock('@rc-component/util/lib/hooks/useLayoutEffect', () => {
1615
const origin = jest.requireActual('react');
1716
return origin.useLayoutEffect;
1817
});
1918

2019
// Revert `useLayoutEffect` back to real one since we should keep order for test
21-
jest.mock('rc-util/lib/getScrollBarSize', () => {
22-
const origin = jest.requireActual('rc-util/lib/getScrollBarSize');
20+
jest.mock('@rc-component/util/lib/getScrollBarSize', () => {
21+
const origin = jest.requireActual('@rc-component/util/lib/getScrollBarSize');
2322
return {
2423
...origin,
2524

0 commit comments

Comments
 (0)