Skip to content

Commit 686bfb0

Browse files
committed
update react to 19
1 parent f2b327f commit 686bfb0

File tree

5 files changed

+41
-36
lines changed

5 files changed

+41
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ lib
2626
es
2727
yarn.lock
2828
package-lock.json
29+
pnpm-lock.yaml
2930
coverage/
3031
.doc
3132
# dumi

package.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,19 @@
4141
"now-build": "npm run build"
4242
},
4343
"dependencies": {
44-
"classnames": "^2.2.1",
45-
"@rc-component/util": "^1.2.0"
44+
"@rc-component/util": "^1.2.0",
45+
"classnames": "^2.2.1"
4646
},
4747
"devDependencies": {
4848
"@rc-component/father-plugin": "^2.0.2",
49-
"@testing-library/react": "^12.1.5",
49+
"@rc-component/np": "^1.0.0",
50+
"@testing-library/react": "^16.3.5",
5051
"@types/jest": "^29.5.10",
51-
"@types/react-dom": "^18.0.11",
52-
"@types/react": "^18.0.28",
52+
"@types/node": "^22.15.19",
53+
"@types/react": "^19.1.4",
54+
"@types/react-dom": "^19.1.5",
5355
"@umijs/fabric": "^2.0.9",
56+
"cheerio": "1.0.0-rc.12",
5457
"coveralls": "^3.0.6",
5558
"cross-env": "^7.0.2",
5659
"dumi": "^2.0.0",
@@ -61,18 +64,16 @@
6164
"gh-pages": "^6.1.0",
6265
"glob": "^7.1.6",
6366
"less": "^4.1.3",
64-
"@rc-component/np": "^1.0.0",
6567
"prettier": "^3.2.5",
6668
"pretty-quick": "^4.0.0",
6769
"rc-test": "^7.0.15",
68-
"react": "^16.0.0",
69-
"react-dom": "^16.0.0",
70-
"cheerio": "1.0.0-rc.12",
70+
"react": "^19.1.0",
71+
"react-dom": "^19.1.0",
7172
"regenerator-runtime": "^0.14.0"
7273
},
7374
"peerDependencies": {
74-
"react": ">=16.9.0",
75-
"react-dom": ">=16.9.0"
75+
"react": ">=18.0.0",
76+
"react-dom": ">=18.0.0"
7677
},
7778
"cnpm": {
7879
"mode": "npm"

src/Collection.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ export interface ResizeInfo {
1414
export interface CollectionProps {
1515
/** Trigger when some children ResizeObserver changed. Collect by frame render level */
1616
onBatchResize?: (resizeInfo: ResizeInfo[]) => void;
17-
children?: React.ReactNode;
1817
}
1918

2019
/**
2120
* Collect all the resize event from children ResizeObserver
2221
*/
23-
export function Collection({ children, onBatchResize }: CollectionProps) {
22+
export const Collection: React.FC<React.PropsWithChildren<CollectionProps>> = ({
23+
children,
24+
onBatchResize,
25+
}) => {
2426
const resizeIdRef = React.useRef(0);
2527
const resizeInfosRef = React.useRef<ResizeInfo[]>([]);
2628

@@ -51,4 +53,4 @@ export function Collection({ children, onBatchResize }: CollectionProps) {
5153
);
5254

5355
return <CollectionContext.Provider value={onResize}>{children}</CollectionContext.Provider>;
54-
}
56+
};

src/SingleObserver/index.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import { CollectionContext } from '../Collection';
66
import { observe, unobserve } from '../utils/observerUtil';
77

88
export interface SingleObserverProps extends ResizeObserverProps {
9-
children: React.ReactElement | ((ref: React.RefObject<Element>) => React.ReactElement);
9+
//
1010
}
1111

12-
function SingleObserver(props: SingleObserverProps, ref: React.Ref<HTMLElement>) {
12+
const SingleObserver: React.ForwardRefRenderFunction<HTMLElement, SingleObserverProps> = (
13+
props,
14+
ref,
15+
) => {
1316
const { children, disabled } = props;
17+
1418
const elementRef = React.useRef<Element>(null);
1519

1620
const onCollectionResize = React.useContext(CollectionContext);
@@ -29,15 +33,15 @@ function SingleObserver(props: SingleObserverProps, ref: React.Ref<HTMLElement>)
2933

3034
// ============================= Ref ==============================
3135
const canRef =
32-
!isRenderProps && React.isValidElement(mergedChildren) && supportRef(mergedChildren);
33-
const originRef: React.Ref<Element> = canRef ? getNodeRef(mergedChildren) : null;
36+
!isRenderProps && React.isValidElement<any>(mergedChildren) && supportRef(mergedChildren);
37+
38+
const originRef = canRef ? getNodeRef(mergedChildren as any) : null;
3439

3540
const mergedRef = useComposeRef(originRef, elementRef);
3641

3742
const getDomElement = () => {
38-
return getDOM( elementRef.current ) as HTMLElement
39-
}
40-
43+
return getDOM(elementRef.current) as HTMLElement;
44+
};
4145

4246
React.useImperativeHandle(ref, () => getDomElement());
4347

@@ -93,7 +97,7 @@ function SingleObserver(props: SingleObserverProps, ref: React.Ref<HTMLElement>)
9397

9498
// Dynamic observe
9599
React.useEffect(() => {
96-
const currentElement: HTMLElement = getDomElement();
100+
const currentElement = getDomElement();
97101

98102
if (currentElement && !disabled) {
99103
observe(currentElement, onInternalResize);
@@ -103,14 +107,8 @@ function SingleObserver(props: SingleObserverProps, ref: React.Ref<HTMLElement>)
103107
}, [elementRef.current, disabled]);
104108

105109
// ============================ Render ============================
106-
return (
107-
canRef
108-
? React.cloneElement(mergedChildren as any, {
109-
ref: mergedRef,
110-
})
111-
: mergedChildren
112-
);
113-
}
110+
return canRef ? React.cloneElement<any>(mergedChildren, { ref: mergedRef }) : mergedChildren;
111+
};
114112

115113
const RefSingleObserver = React.forwardRef(SingleObserver);
116114

src/index.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ export type OnResize = (size: SizeInfo, element: HTMLElement) => void;
2424
export interface ResizeObserverProps {
2525
/** Pass to ResizeObserver.Collection with additional data */
2626
data?: any;
27-
children: React.ReactNode | ((ref: React.RefObject<any>) => React.ReactElement);
27+
children: React.ReactNode | ((ref: React.RefObject<any>) => React.ReactElement<any>);
2828
disabled?: boolean;
2929
/** Trigger if element resized. Will always trigger when first time render. */
3030
onResize?: OnResize;
3131
}
3232

33-
function ResizeObserver(props: ResizeObserverProps, ref: React.Ref<HTMLElement>) {
33+
const ResizeObserver: React.ForwardRefRenderFunction<HTMLElement, ResizeObserverProps> = (
34+
props,
35+
ref,
36+
) => {
3437
const { children } = props;
35-
const childNodes = typeof children === 'function' ? [children] : toArray(children);
38+
const childNodes = typeof children === 'function' ? [children] : toArray(children as any);
3639

3740
if (process.env.NODE_ENV !== 'production') {
3841
if (childNodes.length > 1) {
@@ -45,15 +48,15 @@ function ResizeObserver(props: ResizeObserverProps, ref: React.Ref<HTMLElement>)
4548
}
4649
}
4750

48-
return childNodes.map((child, index) => {
51+
return childNodes.map<React.ReactElement<any>>((child, index) => {
4952
const key = child?.key || `${INTERNAL_PREFIX_KEY}-${index}`;
5053
return (
5154
<SingleObserver {...props} key={key} ref={index === 0 ? ref : undefined}>
5255
{child}
5356
</SingleObserver>
5457
);
55-
}) as any as React.ReactElement;
56-
}
58+
});
59+
};
5760

5861
const RefResizeObserver = React.forwardRef(ResizeObserver) as React.ForwardRefExoticComponent<
5962
React.PropsWithoutRef<ResizeObserverProps> & React.RefAttributes<any>

0 commit comments

Comments
 (0)