|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * @providesModule LayoutEventsTest |
| 10 | + * @flow |
| 11 | + */ |
| 12 | +'use strict'; |
| 13 | + |
| 14 | +var React = require('react-native'); |
| 15 | +var { |
| 16 | + Image, |
| 17 | + LayoutAnimation, |
| 18 | + NativeModules, |
| 19 | + StyleSheet, |
| 20 | + Text, |
| 21 | + View, |
| 22 | +} = React; |
| 23 | +var TestModule = NativeModules.TestModule || NativeModules.SnapshotTestManager; |
| 24 | + |
| 25 | +var deepDiffer = require('deepDiffer'); |
| 26 | + |
| 27 | +function debug() { |
| 28 | + //console.log.apply(null, arguments); |
| 29 | +} |
| 30 | + |
| 31 | +type LayoutEvent = { |
| 32 | + nativeEvent: { |
| 33 | + layout: { |
| 34 | + x: number; |
| 35 | + y: number; |
| 36 | + width: number; |
| 37 | + height: number; |
| 38 | + }; |
| 39 | + }; |
| 40 | +}; |
| 41 | + |
| 42 | +var LayoutEventsTest = React.createClass({ |
| 43 | + getInitialState: function() { |
| 44 | + return { |
| 45 | + didAnimation: false, |
| 46 | + }; |
| 47 | + }, |
| 48 | + animateViewLayout: function() { |
| 49 | + LayoutAnimation.configureNext( |
| 50 | + LayoutAnimation.Presets.spring, |
| 51 | + () => { |
| 52 | + debug('layout animation done.'); |
| 53 | + this.checkLayout(this.addWrapText); |
| 54 | + }, |
| 55 | + (error) => { throw new Error(JSON.stringify(error)); } |
| 56 | + ); |
| 57 | + this.setState({viewStyle: {margin: 60}}); |
| 58 | + }, |
| 59 | + addWrapText: function() { |
| 60 | + this.setState( |
| 61 | + {extraText: ' And a bunch more text to wrap around a few lines.'}, |
| 62 | + () => this.checkLayout(this.changeContainer) |
| 63 | + ); |
| 64 | + }, |
| 65 | + changeContainer: function() { |
| 66 | + this.setState( |
| 67 | + {containerStyle: {width: 280}}, |
| 68 | + () => this.checkLayout(TestModule.markTestCompleted) |
| 69 | + ); |
| 70 | + }, |
| 71 | + checkLayout: function(next?: ?Function) { |
| 72 | + if (!this.isMounted()) { |
| 73 | + return; |
| 74 | + } |
| 75 | + this.refs.view.measure((x, y, width, height) => { |
| 76 | + this.compare('view', {x, y, width, height}, this.state.viewLayout); |
| 77 | + if (typeof next === 'function') { |
| 78 | + next(); |
| 79 | + } else if (!this.state.didAnimation) { |
| 80 | + // Trigger first state change after onLayout fires |
| 81 | + this.animateViewLayout(); |
| 82 | + this.state.didAnimation = true; |
| 83 | + } |
| 84 | + }); |
| 85 | + this.refs.txt.measure((x, y, width, height) => { |
| 86 | + this.compare('txt', {x, y, width, height}, this.state.textLayout); |
| 87 | + }); |
| 88 | + this.refs.img.measure((x, y, width, height) => { |
| 89 | + this.compare('img', {x, y, width, height}, this.state.imageLayout); |
| 90 | + }); |
| 91 | + }, |
| 92 | + compare: function(node: string, measured: any, onLayout: any): void { |
| 93 | + if (deepDiffer(measured, onLayout)) { |
| 94 | + var data = {measured, onLayout}; |
| 95 | + throw new Error( |
| 96 | + node + ' onLayout mismatch with measure ' + |
| 97 | + JSON.stringify(data, null, ' ') |
| 98 | + ); |
| 99 | + } |
| 100 | + }, |
| 101 | + onViewLayout: function(e: LayoutEvent) { |
| 102 | + debug('received view layout event\n', e.nativeEvent); |
| 103 | + this.setState({viewLayout: e.nativeEvent.layout}, this.checkLayout); |
| 104 | + }, |
| 105 | + onTextLayout: function(e: LayoutEvent) { |
| 106 | + debug('received text layout event\n', e.nativeEvent); |
| 107 | + this.setState({textLayout: e.nativeEvent.layout}, this.checkLayout); |
| 108 | + }, |
| 109 | + onImageLayout: function(e: LayoutEvent) { |
| 110 | + debug('received image layout event\n', e.nativeEvent); |
| 111 | + this.setState({imageLayout: e.nativeEvent.layout}, this.checkLayout); |
| 112 | + }, |
| 113 | + render: function() { |
| 114 | + var viewStyle = [styles.view, this.state.viewStyle]; |
| 115 | + var textLayout = this.state.textLayout || {width: '?', height: '?'}; |
| 116 | + var imageLayout = this.state.imageLayout || {x: '?', y: '?'}; |
| 117 | + return ( |
| 118 | + <View style={[styles.container, this.state.containerStyle]}> |
| 119 | + <View ref="view" onLayout={this.onViewLayout} style={viewStyle}> |
| 120 | + <Image |
| 121 | + ref="img" |
| 122 | + onLayout={this.onImageLayout} |
| 123 | + style={styles.image} |
| 124 | + source={{uri: 'https://fbcdn-dragon-a.akamaihd.net/hphotos-ak-prn1/t39.1997/p128x128/851561_767334496626293_1958532586_n.png'}} |
| 125 | + /> |
| 126 | + <Text> |
| 127 | + ViewLayout: {JSON.stringify(this.state.viewLayout, null, ' ') + '\n\n'} |
| 128 | + </Text> |
| 129 | + <Text ref="txt" onLayout={this.onTextLayout} style={styles.text}> |
| 130 | + A simple piece of text.{this.state.extraText} |
| 131 | + </Text> |
| 132 | + <Text> |
| 133 | + {'\n'} |
| 134 | + Text w/h: {textLayout.width}/{textLayout.height + '\n'} |
| 135 | + Image x/y: {imageLayout.x}/{imageLayout.y} |
| 136 | + </Text> |
| 137 | + </View> |
| 138 | + </View> |
| 139 | + ); |
| 140 | + } |
| 141 | +}); |
| 142 | + |
| 143 | +var styles = StyleSheet.create({ |
| 144 | + container: { |
| 145 | + margin: 40, |
| 146 | + }, |
| 147 | + view: { |
| 148 | + margin: 20, |
| 149 | + padding: 12, |
| 150 | + borderColor: 'black', |
| 151 | + borderWidth: 0.5, |
| 152 | + backgroundColor: 'transparent', |
| 153 | + }, |
| 154 | + text: { |
| 155 | + alignSelf: 'flex-start', |
| 156 | + borderColor: 'rgba(0, 0, 255, 0.2)', |
| 157 | + borderWidth: 0.5, |
| 158 | + }, |
| 159 | + image: { |
| 160 | + width: 50, |
| 161 | + height: 50, |
| 162 | + marginBottom: 10, |
| 163 | + alignSelf: 'center', |
| 164 | + }, |
| 165 | +}); |
| 166 | + |
| 167 | +module.exports = LayoutEventsTest; |
0 commit comments