-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathRow.js
93 lines (85 loc) · 2.12 KB
/
Row.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
/* @flow weak */
/**
* mSupply Mobile
* Sustainable Solutions (NZ) Ltd. 2016
*/
import React from 'react';
import PropTypes from 'prop-types';
import {
View,
ViewPropTypes,
StyleSheet,
TouchableOpacity,
} from 'react-native';
export class Row extends React.Component {
constructor(props) {
super(props);
this.state = {
isExpanded: props.isExpanded,
};
this.onPress = this.onPress.bind(this);
}
componentWillReceiveProps(nextProps) {
if (nextProps.isExpanded !== this.state.isExpanded) {
this.setState({ isExpanded: nextProps.isExpanded });
}
}
onPress() {
this.setState({ isExpanded: !this.state.isExpanded });
this.props.onPress();
}
render() {
const { style, expandedRowStyle, children, renderExpansion, onPress, ...touchableOpacityProps } = this.props;
const rowStyle = this.state.isExpanded && expandedRowStyle ? expandedRowStyle : style;
if (renderExpansion) {
return (
<TouchableOpacity
{...touchableOpacityProps}
style={[defaultStyles.row, rowStyle]}
onPress={this.onPress}
>
<View style={defaultStyles.cellContainer}>
{children}
</View>
{this.state.isExpanded && renderExpansion()}
</TouchableOpacity>
);
}
if (onPress) {
return (
<TouchableOpacity
{...touchableOpacityProps}
style={[defaultStyles.row, { flexDirection: 'row' }, rowStyle]}
onPress={this.onPress}
>
{children}
</TouchableOpacity>
);
}
return (
<View style={[defaultStyles.row, { flexDirection: 'row' }, rowStyle]}>
{children}
</View>
);
}
}
Row.propTypes = {
style: ViewPropTypes.style,
children: PropTypes.any,
onPress: PropTypes.func,
isExpanded: PropTypes.bool,
renderExpansion: PropTypes.func,
};
const defaultStyles = StyleSheet.create({
row: {
flexDirection: 'column',
flexWrap: 'nowrap',
justifyContent: 'flex-start',
alignItems: 'stretch',
backgroundColor: '#d6f3ff',
},
cellContainer: {
flex: 1,
flexDirection: 'row',
},
});