-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
110 lines (102 loc) · 2.59 KB
/
index.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
import React, { Component } from "react";
import {
Modal,
Text,
TouchableHighlight,
View,
StyleSheet,
FlatList
} from "react-native";
import PropTypes from "prop-types";
export default class ModalPicker extends Component {
constructor(props) {
super(props);
this.state = {
modalVisible: false
};
}
static getDerivedStateFromProps(props, state) {
if (state.data === props.data) {
return null;
}
return { data: props.data };
}
setModalVisible(visible) {
this.setState({ modalVisible: visible });
}
render() {
return (
<View
style={{
backgroundColor: "transparent",
flex: 1,
position: "absolute"
}}
>
<Modal
animationType="fade"
transparent={true}
visible={this.state.modalVisible}
onRequestClose={() => {
this.setModalVisible(false);
}}
>
<TouchableHighlight
style={styles.container}
onPress={() => this.setModalVisible(false)}
underlayColor={"#333333cc"}
>
<View>
<FlatList
data={this.state.data}
keyExtractor={(_, index) => index.toString()}
renderItem={({ item, index }) => {
return (
<TouchableHighlight
underlayColor={"transparent"}
onPress={() => {
this.setModalVisible(false);
this.props.onValueChange(item[this.props.value], index);
}}
>
{this.props.renderRow ? (
this.props.renderRow(item, index)
) : (
<Text style={styles.itemText}>
{item[this.props.label]}
</Text>
)}
</TouchableHighlight>
);
}}
/>
</View>
</TouchableHighlight>
</Modal>
</View>
);
}
}
ModalPicker.propTypes = {
data: PropTypes.array.isRequired,
value: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
onValueChange: PropTypes.func,
renderRow: PropTypes.func
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
backgroundColor: "#333333cc",
padding: 16
},
itemText: {
backgroundColor: "#fff",
padding: 16,
fontSize: 18,
color: "#222",
borderTopWidth: 1,
borderColor: "#CCC"
}
});