Add type for onLayout

Reviewed By: sahrens

Differential Revision: D5364203

fbshipit-source-id: ad87179422b0e595fc78db21a3108d50ba31564c
This commit is contained in:
Pieter De Baets 2017-07-07 03:01:47 -07:00 committed by Facebook Github Bot
parent 6e13adbf56
commit 9108f98ca7
3 changed files with 24 additions and 31 deletions

View File

@ -21,13 +21,8 @@ const View = require('View');
const ViewPropTypes = require('ViewPropTypes'); const ViewPropTypes = require('ViewPropTypes');
import type EmitterSubscription from 'EmitterSubscription'; import type EmitterSubscription from 'EmitterSubscription';
import type {ViewLayout, ViewLayoutEvent} from 'ViewPropTypes';
type Rect = {
x: number,
y: number,
width: number,
height: number,
};
type ScreenRect = { type ScreenRect = {
screenX: number, screenX: number,
screenY: number, screenY: number,
@ -40,11 +35,6 @@ type KeyboardChangeEvent = {
duration?: number, duration?: number,
easing?: string, easing?: string,
}; };
type LayoutEvent = {
nativeEvent: {
layout: Rect,
}
};
const viewRef = 'VIEW'; const viewRef = 'VIEW';
@ -85,7 +75,7 @@ const KeyboardAvoidingView = React.createClass({
}, },
subscriptions: ([]: Array<EmitterSubscription>), subscriptions: ([]: Array<EmitterSubscription>),
frame: (null: ?Rect), frame: (null: ?ViewLayout),
relativeKeyboardHeight(keyboardFrame: ScreenRect): number { relativeKeyboardHeight(keyboardFrame: ScreenRect): number {
const frame = this.frame; const frame = this.frame;
@ -121,7 +111,7 @@ const KeyboardAvoidingView = React.createClass({
this.setState({bottom: height}); this.setState({bottom: height});
}, },
onLayout(event: LayoutEvent) { onLayout(event: ViewLayoutEvent) {
this.frame = event.nativeEvent.layout; this.frame = event.nativeEvent.layout;
}, },

View File

@ -36,6 +36,19 @@ import type {TVViewProps} from 'TVViewPropTypes';
const stylePropType = StyleSheetPropType(ViewStylePropTypes); const stylePropType = StyleSheetPropType(ViewStylePropTypes);
export type ViewLayout = {
x: number,
y: number,
width: number,
height: number,
}
export type ViewLayoutEvent = {
nativeEvent: {
layout: ViewLayout,
}
}
// There's no easy way to create a different type if(Platform.isTVOS): // There's no easy way to create a different type if(Platform.isTVOS):
// so we must include TVViewProps // so we must include TVViewProps
export type ViewProps = { export type ViewProps = {
@ -50,6 +63,7 @@ export type ViewProps = {
onMagicTap?: Function, onMagicTap?: Function,
testID?: string, testID?: string,
nativeID?: string, nativeID?: string,
onLayout?: (event: ViewLayoutEvent) => void,
onResponderGrant?: Function, onResponderGrant?: Function,
onResponderMove?: Function, onResponderMove?: Function,
onResponderReject?: Function, onResponderReject?: Function,

View File

@ -21,25 +21,14 @@ var {
View, View,
} = ReactNative; } = ReactNative;
type Layout = { import type {ViewLayout, ViewLayoutEvent} from 'ViewPropTypes';
x: number;
y: number;
width: number;
height: number;
};
type LayoutEvent = {
nativeEvent: {
layout: Layout,
};
};
type State = { type State = {
containerStyle?: { width: number }, containerStyle?: { width: number },
extraText?: string, extraText?: string,
imageLayout?: Layout, imageLayout?: ViewLayout,
textLayout?: Layout, textLayout?: ViewLayout,
viewLayout?: Layout, viewLayout?: ViewLayout,
viewStyle: { margin: number }, viewStyle: { margin: number },
}; };
@ -76,17 +65,17 @@ class LayoutEventExample extends React.Component {
this.setState({containerStyle: {width: 280}}); this.setState({containerStyle: {width: 280}});
}; };
onViewLayout = (e: LayoutEvent) => { onViewLayout = (e: ViewLayoutEvent) => {
console.log('received view layout event\n', e.nativeEvent); console.log('received view layout event\n', e.nativeEvent);
this.setState({viewLayout: e.nativeEvent.layout}); this.setState({viewLayout: e.nativeEvent.layout});
}; };
onTextLayout = (e: LayoutEvent) => { onTextLayout = (e: ViewLayoutEvent) => {
console.log('received text layout event\n', e.nativeEvent); console.log('received text layout event\n', e.nativeEvent);
this.setState({textLayout: e.nativeEvent.layout}); this.setState({textLayout: e.nativeEvent.layout});
}; };
onImageLayout = (e: LayoutEvent) => { onImageLayout = (e: ViewLayoutEvent) => {
console.log('received image layout event\n', e.nativeEvent); console.log('received image layout event\n', e.nativeEvent);
this.setState({imageLayout: e.nativeEvent.layout}); this.setState({imageLayout: e.nativeEvent.layout});
}; };