-
Notifications
You must be signed in to change notification settings - Fork 5
/
react-native-square-view.js
47 lines (41 loc) · 1.04 KB
/
react-native-square-view.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
'use strict';
var React = require('react-native');
var {
View
} = React;
var SquareView = React.createClass({
getInitialState: function() {
return {
width: 0,
height: 0,
direction: 'row' // 'column' and 'row'
};
},
render: function() {
var square = (
<View
{...this.props}
style={
[this.props.style,
{width: this.state.width, height: this.state.height}]
}
onLayout={event => {
var {width, height} = event.nativeEvent.layout;
var sideLength = Math.max(width, height);
if (sideLength) {
this.setState({width: sideLength, height: sideLength});
} else {
this.setState({direction: 'column'});
}
}}>
{this.props.children}
</View>
);
switch (this.state.direction) {
case 'column': return square;
case 'row': return (<View style={{backgroundColor: 'transparent'}}>{square}</View>);
default: return null;
}
}
});
module.exports = SquareView;