-
Notifications
You must be signed in to change notification settings - Fork 36
/
ListPopover.js
135 lines (120 loc) · 2.92 KB
/
ListPopover.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
/**
* ListPopover - Popover rendered with a selectable list.
*/
"use strict";
import React from 'react';
import PropTypes from 'prop-types';
import {
ListView,
StyleSheet,
Text,
Dimensions,
TouchableOpacity,
View
} from 'react-native';
const SCREEN_HEIGHT = Dimensions.get('window').height;
const noop = () => {};
const ds = new ListView.DataSource({rowHasChanged: (r1,r2)=>(r1!==r2)});
class ListPopover extends React.Component {
constructor(props) {
super(props);
this.state = {
dataSource: ds.cloneWithRows(this.props.list)
};
}
componentWillReceiveProps(nextProps) {
if (nextProps.list !== this.props.list) {
this.setState({dataSource: ds.cloneWithRows(nextProps.list)});
}
}
handleClick(data) {
this.props.onClick(data);
this.props.onClose();
}
renderRow(rowData) {
const separatorStyle = this.props.separatorStyle || DefaultStyles.separator;
const rowTextStyle = this.props.rowText || DefaultStyles.rowText;
let separator = <View style={separatorStyle}/>;
if (rowData === this.props.list[0]) {
separator = null;
}
let row = <Text style={rowTextStyle}>{rowData}</Text>
if (this.props.renderRow) {
row = this.props.renderRow(rowData);
}
return (
<View>
{separator}
<TouchableOpacity onPress={() => this.handleClick(rowData)}>
{row}
</TouchableOpacity>
</View>
);
}
renderList() {
const styles = this.props.style || DefaultStyles;
let maxHeight = {};
if (this.props.list.length > 12) {
maxHeight = {height: SCREEN_HEIGHT * 3/4};
}
return (
<ListView
style={maxHeight}
dataSource={this.state.dataSource}
renderRow={(rowData) => this.renderRow(rowData)}
/>
);
}
render() {
const containerStyle = this.props.containerStyle || DefaultStyles.container;
const popoverStyle = this.props.popoverStyle || DefaultStyles.popover;
if (this.props.isVisible) {
return (
<TouchableOpacity onPress={this.props.onClose} style={containerStyle}>
<View style={popoverStyle}>
{this.renderList()}
</View>
</TouchableOpacity>
);
} else {
return (<View/>);
}
}
}
ListPopover.propTypes = {
list: PropTypes.array.isRequired,
isVisible: PropTypes.bool,
onClick: PropTypes.func,
onClose: PropTypes.func,
};
ListPopover.defaultProps = {
list: [""],
isVisible: false,
onClick: noop,
onClose: noop
};
const DefaultStyles = StyleSheet.create({
container: {
alignItems: 'center',
justifyContent: 'center',
position: 'absolute',
width: '100%',
zIndex: 10,
},
popover: {
backgroundColor: '#fff',
borderRadius: 3,
padding: 3,
width: '96%',
},
rowText: {
padding: 10,
},
separator: {
backgroundColor: '#ccc',
height: 0.5,
marginLeft: 8,
marginRight: 8,
},
});
export default ListPopover;