forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LayoutEventsTest.js
196 lines (171 loc) · 4.99 KB
/
LayoutEventsTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
const React = require('react');
const ReactNative = require('react-native');
const {Image, LayoutAnimation, StyleSheet, Text, View} = ReactNative;
const {TestModule} = ReactNative.NativeModules;
import type {ViewStyleProp} from 'StyleSheet';
const deepDiffer = require('deepDiffer');
function debug(...args) {
// console.log.apply(null, arguments);
}
import type {Layout, LayoutEvent} from 'CoreEventTypes';
type Props = $ReadOnly<{||}>;
type State = {
didAnimation: boolean,
extraText?: string,
imageLayout?: Layout,
textLayout?: Layout,
viewLayout?: Layout,
viewStyle?: ViewStyleProp,
containerStyle?: ViewStyleProp,
};
class LayoutEventsTest extends React.Component<Props, State> {
_view: ?React.ElementRef<typeof View>;
_img: ?React.ElementRef<typeof Image>;
_txt: ?React.ElementRef<typeof Text>;
state: State = {
didAnimation: false,
};
animateViewLayout() {
debug('animateViewLayout invoked');
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring, () => {
debug('animateViewLayout done');
this.checkLayout(this.addWrapText);
});
this.setState({viewStyle: {margin: 60}});
}
addWrapText = () => {
debug('addWrapText invoked');
this.setState(
{extraText: ' And a bunch more text to wrap around a few lines.'},
() => this.checkLayout(this.changeContainer),
);
};
changeContainer = () => {
debug('changeContainer invoked');
this.setState({containerStyle: {width: 280}}, () =>
this.checkLayout(TestModule.markTestCompleted),
);
};
checkLayout = (next?: ?() => void) => {
const view = this._view;
const txt = this._txt;
const img = this._img;
if (view == null || txt == null || img == null) {
return;
}
view.measure((x, y, width, height) => {
this.compare(
'view',
{x, y, width, height},
this.state.viewLayout || null,
);
if (typeof next === 'function') {
next();
} else if (!this.state.didAnimation) {
// Trigger first state change after onLayout fires
this.animateViewLayout();
this.state.didAnimation = true;
}
});
txt.measure((x, y, width, height) => {
this.compare('txt', {x, y, width, height}, this.state.textLayout);
});
img.measure((x, y, width, height) => {
this.compare('img', {x, y, width, height}, this.state.imageLayout);
});
};
compare(node: string, measured: Layout, onLayout?: ?Layout): void {
if (deepDiffer(measured, onLayout)) {
const data = {measured, onLayout};
throw new Error(
node +
' onLayout mismatch with measure ' +
JSON.stringify(data, null, ' '),
);
}
}
onViewLayout = (e: LayoutEvent) => {
debug('received view layout event\n', e.nativeEvent);
this.setState({viewLayout: e.nativeEvent.layout}, this.checkLayout);
};
onTextLayout = (e: LayoutEvent) => {
debug('received text layout event\n', e.nativeEvent);
this.setState({textLayout: e.nativeEvent.layout}, this.checkLayout);
};
onImageLayout = (e: LayoutEvent) => {
debug('received image layout event\n', e.nativeEvent);
this.setState({imageLayout: e.nativeEvent.layout}, this.checkLayout);
};
render() {
const viewStyle = [styles.view, this.state.viewStyle];
const textLayout = this.state.textLayout || {width: '?', height: '?'};
const imageLayout = this.state.imageLayout || {x: '?', y: '?'};
debug('viewLayout', this.state.viewLayout);
return (
<View style={[styles.container, this.state.containerStyle]}>
<View
ref={ref => {
this._view = ref;
}}
onLayout={this.onViewLayout}
style={viewStyle}>
<Image
ref={ref => {
this._img = ref;
}}
onLayout={this.onImageLayout}
style={styles.image}
source={{uri: 'uie_thumb_big.png'}}
/>
<Text
ref={ref => {
this._txt = ref;
}}
onLayout={this.onTextLayout}
style={styles.text}>
A simple piece of text.{this.state.extraText}
</Text>
<Text>
{'\n'}
Text w/h: {textLayout.width}/{textLayout.height + '\n'}
Image x/y: {imageLayout.x}/{imageLayout.y}
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
margin: 40,
},
view: {
margin: 20,
padding: 12,
borderColor: 'black',
borderWidth: 0.5,
backgroundColor: 'transparent',
},
text: {
alignSelf: 'flex-start',
borderColor: 'rgba(0, 0, 255, 0.2)',
borderWidth: 0.5,
},
image: {
width: 50,
height: 50,
marginBottom: 10,
alignSelf: 'center',
},
});
module.exports = LayoutEventsTest;