From 9108f98ca72c1d72d843145f0f1fb8b010be1d35 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Fri, 7 Jul 2017 03:01:47 -0700 Subject: [PATCH] Add type for onLayout Reviewed By: sahrens Differential Revision: D5364203 fbshipit-source-id: ad87179422b0e595fc78db21a3108d50ba31564c --- .../Keyboard/KeyboardAvoidingView.js | 16 +++--------- Libraries/Components/View/ViewPropTypes.js | 14 +++++++++++ RNTester/js/LayoutEventsExample.js | 25 ++++++------------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/Libraries/Components/Keyboard/KeyboardAvoidingView.js b/Libraries/Components/Keyboard/KeyboardAvoidingView.js index b8dba6281..85fccbf33 100644 --- a/Libraries/Components/Keyboard/KeyboardAvoidingView.js +++ b/Libraries/Components/Keyboard/KeyboardAvoidingView.js @@ -21,13 +21,8 @@ const View = require('View'); const ViewPropTypes = require('ViewPropTypes'); import type EmitterSubscription from 'EmitterSubscription'; +import type {ViewLayout, ViewLayoutEvent} from 'ViewPropTypes'; -type Rect = { - x: number, - y: number, - width: number, - height: number, -}; type ScreenRect = { screenX: number, screenY: number, @@ -40,11 +35,6 @@ type KeyboardChangeEvent = { duration?: number, easing?: string, }; -type LayoutEvent = { - nativeEvent: { - layout: Rect, - } -}; const viewRef = 'VIEW'; @@ -85,7 +75,7 @@ const KeyboardAvoidingView = React.createClass({ }, subscriptions: ([]: Array), - frame: (null: ?Rect), + frame: (null: ?ViewLayout), relativeKeyboardHeight(keyboardFrame: ScreenRect): number { const frame = this.frame; @@ -121,7 +111,7 @@ const KeyboardAvoidingView = React.createClass({ this.setState({bottom: height}); }, - onLayout(event: LayoutEvent) { + onLayout(event: ViewLayoutEvent) { this.frame = event.nativeEvent.layout; }, diff --git a/Libraries/Components/View/ViewPropTypes.js b/Libraries/Components/View/ViewPropTypes.js index ef15ab552..6e332f561 100644 --- a/Libraries/Components/View/ViewPropTypes.js +++ b/Libraries/Components/View/ViewPropTypes.js @@ -36,6 +36,19 @@ import type {TVViewProps} from 'TVViewPropTypes'; 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): // so we must include TVViewProps export type ViewProps = { @@ -50,6 +63,7 @@ export type ViewProps = { onMagicTap?: Function, testID?: string, nativeID?: string, + onLayout?: (event: ViewLayoutEvent) => void, onResponderGrant?: Function, onResponderMove?: Function, onResponderReject?: Function, diff --git a/RNTester/js/LayoutEventsExample.js b/RNTester/js/LayoutEventsExample.js index e2b868df1..230bd3190 100644 --- a/RNTester/js/LayoutEventsExample.js +++ b/RNTester/js/LayoutEventsExample.js @@ -21,25 +21,14 @@ var { View, } = ReactNative; -type Layout = { - x: number; - y: number; - width: number; - height: number; -}; - -type LayoutEvent = { - nativeEvent: { - layout: Layout, - }; -}; +import type {ViewLayout, ViewLayoutEvent} from 'ViewPropTypes'; type State = { containerStyle?: { width: number }, extraText?: string, - imageLayout?: Layout, - textLayout?: Layout, - viewLayout?: Layout, + imageLayout?: ViewLayout, + textLayout?: ViewLayout, + viewLayout?: ViewLayout, viewStyle: { margin: number }, }; @@ -76,17 +65,17 @@ class LayoutEventExample extends React.Component { this.setState({containerStyle: {width: 280}}); }; - onViewLayout = (e: LayoutEvent) => { + onViewLayout = (e: ViewLayoutEvent) => { console.log('received view layout event\n', e.nativeEvent); this.setState({viewLayout: e.nativeEvent.layout}); }; - onTextLayout = (e: LayoutEvent) => { + onTextLayout = (e: ViewLayoutEvent) => { console.log('received text layout event\n', e.nativeEvent); this.setState({textLayout: e.nativeEvent.layout}); }; - onImageLayout = (e: LayoutEvent) => { + onImageLayout = (e: ViewLayoutEvent) => { console.log('received image layout event\n', e.nativeEvent); this.setState({imageLayout: e.nativeEvent.layout}); };