Skip to content

Commit d265801

Browse files
committed
fix: add more api for refMeasure and fix it
1 parent 3b189bd commit d265801

2 files changed

Lines changed: 41 additions & 24 deletions

File tree

src/components/Helpers/RefMeasure.tsx

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import * as React from 'react';
2-
import { findNodeHandle, LayoutChangeEvent, UIManager } from 'react-native';
2+
import {
3+
findNodeHandle,
4+
LayoutChangeEvent,
5+
LayoutRectangle,
6+
UIManager,
7+
} from 'react-native';
38

49
export interface Measurements {
510
height: number;
@@ -14,12 +19,14 @@ export interface RefMeasureChildrenProps {
1419
measurements: Measurements;
1520
forwardRef: React.RefObject<any>;
1621
onLayout: (e: LayoutChangeEvent) => void;
22+
measure: (layout?: LayoutRectangle) => void;
1723
}
1824
export type RefMeasureRenderPropType = (
1925
props: RefMeasureChildrenProps,
2026
) => React.ReactNode;
2127

2228
export interface RefMeasureProps {
29+
onMeasure?: (props: Measurements) => void;
2330
children: RefMeasureRenderPropType;
2431
}
2532

@@ -43,30 +50,43 @@ class RefMeasure extends React.Component<RefMeasureProps, Measurements> {
4350
};
4451
}
4552

53+
public handleLayout = (e: LayoutChangeEvent) => {
54+
// Use the value from here, isntead of inside UIManager.measure callback
55+
// Async behavior will nullify nativeEvent
56+
const layout = e.nativeEvent.layout;
57+
this.handleMeasure(layout);
58+
};
59+
60+
public handleMeasure = (layout?: LayoutRectangle) => {
61+
const { onMeasure } = this.props;
62+
63+
UIManager.measure(
64+
findNodeHandle(this.container.current)!,
65+
(x, y, width, height, pageX, pageY) => {
66+
const nodeMeasurements = {
67+
...this.state,
68+
...layout,
69+
pageX,
70+
pageY,
71+
};
72+
73+
if (onMeasure) {
74+
onMeasure(nodeMeasurements);
75+
}
76+
this.setState(() => nodeMeasurements);
77+
},
78+
);
79+
};
80+
4681
public render() {
4782
const { children } = this.props;
4883
const measurements = this.state;
4984

5085
return children({
5186
forwardRef: this.container,
87+
measure: this.handleMeasure,
5288
measurements,
53-
onLayout: e => {
54-
// Use the value from here, isntead of inside UIManager.measure callback
55-
// Async behavior will nullify nativeEvent
56-
const layout = e.nativeEvent.layout;
57-
58-
UIManager.measure(
59-
findNodeHandle(this.container.current)!,
60-
(x, y, width, height, pageX, pageY) => {
61-
const nodeMeasurements = {
62-
...layout,
63-
pageX,
64-
pageY,
65-
};
66-
this.setState(() => nodeMeasurements);
67-
},
68-
);
69-
},
89+
onLayout: this.handleLayout,
7090
});
7191
}
7292
}

src/components/Helpers/ViewMeasure.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@ export interface ViewMeasureProps extends ViewProps {
1515
*/
1616

1717
const ViewMeasure = (props: ViewMeasureProps) => {
18-
const { onMeasure, children } = props;
18+
const { onMeasure, children, ...viewProps } = props;
1919
const isRenderProp = typeof children === 'function';
2020

2121
return (
22-
<RefMeasure>
22+
<RefMeasure onMeasure={onMeasure}>
2323
{({ forwardRef, onLayout, measurements }) => {
24-
if (onMeasure) {
25-
onMeasure(measurements);
26-
}
27-
2824
return (
2925
<View
3026
ref={forwardRef}
3127
onLayout={onLayout}
3228
// @ts-ignore
3329
children={isRenderProp ? children(measurements) : children}
30+
{...viewProps}
3431
/>
3532
);
3633
}}

0 commit comments

Comments
 (0)