Skip to content

Commit 088b147

Browse files
committed
feat: improve <ResizeProvider /> compatibility
better dom node finder
1 parent 40a96fb commit 088b147

File tree

5 files changed

+39
-50
lines changed

5 files changed

+39
-50
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
"@types/lodash": "^4.14.115",
2929
"@types/prop-types": "^15.5.4",
3030
"@types/react": "^16.4.7",
31+
"@types/react-dom": "^16.0.7",
3132
"@types/shallowequal": "^0.2.3",
33+
"prop-types": "^15.6.2",
34+
"react": "^16.4.2",
35+
"react-dom": "^16.4.2",
3236
"tslib": "^1.9.3",
3337
"tslint": "^5.11.0",
3438
"tslint-eslint-rules": "^5.3.1",
@@ -43,6 +47,7 @@
4347
},
4448
"peerDependencies": {
4549
"prop-types": "^15.6.2",
46-
"react": "^16.3.0"
50+
"react": "^16.3.0",
51+
"react-dom": "^16.4.2"
4752
}
4853
}

src/ResizeProvider.tsx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
11
import * as React from 'react';
2+
import { findDOMNode } from 'react-dom';
23

34
import { Provider } from './context';
45
import Listener from './Listener';
5-
import { getElement, makeSureChildrenHasRef } from './methods';
6-
7-
import { ResizeChildren } from './types';
8-
import { ReactNode } from 'react';
96

107
interface Props {
11-
children?: ReactNode;
8+
children: React.ReactNode;
129
}
1310

1411
interface State {
15-
children: ResizeChildren | null;
1612
element: HTMLElement | null;
1713
}
1814

1915
export default class ResizeProvider extends React.PureComponent<Props, State> {
20-
public static getDerivedStateFromProps(props: Props) {
21-
return {
22-
children: makeSureChildrenHasRef(props.children),
23-
};
24-
}
25-
2616
public state: State = {
27-
children: null,
2817
element: null,
2918
};
3019

@@ -43,13 +32,18 @@ export default class ResizeProvider extends React.PureComponent<Props, State> {
4332
public render() {
4433
return (
4534
<Provider value={{ listenElement: this.state.element }}>
46-
{this.state.children}
35+
{this.props.children}
4736
</Provider>
4837
);
4938
}
5039

40+
private getElement() {
41+
const element = findDOMNode(this);
42+
return element instanceof HTMLElement ? element : null;
43+
}
44+
5145
private updateListenElement() {
52-
const element = getElement(this.state.children);
46+
const element = this.getElement();
5347

5448
if (element !== this.state.element) {
5549
this.removeListenElement();

src/methods.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import * as React from 'react';
21
import * as shallowequal from 'shallowequal';
32

4-
import { ResizeChildren, Size } from './types';
5-
import { ReactNode, Ref } from 'react';
3+
import { Size } from './types';
64

75
export function getSize(element: HTMLElement): Size {
86
return {
@@ -15,28 +13,6 @@ export function isRefObject(ref: any): boolean {
1513
return ref && typeof ref === 'object' && 'current' in ref;
1614
}
1715

18-
export function getElement(children?: ResizeChildren | null): HTMLElement | null {
19-
const element =
20-
children &&
21-
children.ref &&
22-
children.ref.current;
23-
return element || null;
24-
}
25-
26-
export function makeSureChildrenHasRef(
27-
children: ReactNode
28-
): ResizeChildren | null {
29-
if (React.isValidElement(children)) {
30-
if (isRefObject((children as ResizeChildren).ref)) {
31-
return children;
32-
} else {
33-
return React.cloneElement<{ ref?: Ref<HTMLElement> }>(
34-
children, { ref: React.createRef() }
35-
) as ResizeChildren;
36-
}
37-
} else return null;
38-
}
39-
4016
function compareDataset(
4117
currentValue: any,
4218
nextValue: any,

src/types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
import { ReactElement, RefObject } from 'react';
2-
31
export interface Size {
42
width: number;
53
height: number;
64
}
75

8-
export interface ResizeChildren extends ReactElement<any> {
9-
ref?: RefObject<HTMLElement>;
10-
}
11-
126
export type OnResizeCallBack = (size: Size) => void;

yarn.lock

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,23 @@
1010
version "4.14.115"
1111
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.115.tgz#54d171b2ce12c058742443b5f6754760f701b8f9"
1212

13+
"@types/node@*":
14+
version "10.9.4"
15+
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.9.4.tgz#0f4cb2dc7c1de6096055357f70179043c33e9897"
16+
1317
"@types/prop-types@^15.5.4":
1418
version "15.5.4"
1519
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.5.4.tgz#9e6199bad131786e24c2baa2a82705a02139fbf8"
1620
dependencies:
1721
"@types/react" "*"
1822

23+
"@types/react-dom@^16.0.7":
24+
version "16.0.7"
25+
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-16.0.7.tgz#54d0f867a76b90597e8432030d297982f25c20ba"
26+
dependencies:
27+
"@types/node" "*"
28+
"@types/react" "*"
29+
1930
"@types/react@*", "@types/react@^16.4.7":
2031
version "16.4.7"
2132
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.4.7.tgz#f33f6d759a7e1833befa15224d68942d178a5a3f"
@@ -302,9 +313,18 @@ prop-types@^15.6.0, prop-types@^15.6.2:
302313
loose-envify "^1.3.1"
303314
object-assign "^4.1.1"
304315

305-
react@^16.3.0:
306-
version "16.4.1"
307-
resolved "https://registry.yarnpkg.com/react/-/react-16.4.1.tgz#de51ba5764b5dbcd1f9079037b862bd26b82fe32"
316+
react-dom@^16.4.2:
317+
version "16.4.2"
318+
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.4.2.tgz#4afed569689f2c561d2b8da0b819669c38a0bda4"
319+
dependencies:
320+
fbjs "^0.8.16"
321+
loose-envify "^1.1.0"
322+
object-assign "^4.1.1"
323+
prop-types "^15.6.0"
324+
325+
react@^16.4.2:
326+
version "16.4.2"
327+
resolved "https://registry.yarnpkg.com/react/-/react-16.4.2.tgz#2cd90154e3a9d9dd8da2991149fdca3c260e129f"
308328
dependencies:
309329
fbjs "^0.8.16"
310330
loose-envify "^1.1.0"

0 commit comments

Comments
 (0)