|
| 1 | +import React, { Component } from "react"; |
| 2 | +import { |
| 3 | + Modal, |
| 4 | + Text, |
| 5 | + TouchableHighlight, |
| 6 | + View, |
| 7 | + StyleSheet, |
| 8 | + ListView |
| 9 | +} from "react-native"; |
| 10 | +import PropTypes from "prop-types"; |
| 11 | + |
| 12 | +export default class ModalPicker extends Component { |
| 13 | + constructor(props) { |
| 14 | + super(props); |
| 15 | + const ds = new ListView.DataSource({ |
| 16 | + rowHasChanged: (r1, r2) => r1 !== r2 |
| 17 | + }); |
| 18 | + this.state = { |
| 19 | + dataSource: ds.cloneWithRows(this.props.data), |
| 20 | + modalVisible: false |
| 21 | + }; |
| 22 | + } |
| 23 | + |
| 24 | + setModalVisible(visible) { |
| 25 | + this.setState({ modalVisible: visible }); |
| 26 | + } |
| 27 | + |
| 28 | + render() { |
| 29 | + return ( |
| 30 | + <View |
| 31 | + style={{ |
| 32 | + backgroundColor: "transparent", |
| 33 | + flex: 1, |
| 34 | + position: "absolute" |
| 35 | + }} |
| 36 | + > |
| 37 | + <Modal |
| 38 | + animationType="fade" |
| 39 | + transparent={true} |
| 40 | + visible={this.state.modalVisible} |
| 41 | + onRequestClose={() => { |
| 42 | + this.setModalVisible(false); |
| 43 | + }} |
| 44 | + > |
| 45 | + <TouchableHighlight |
| 46 | + style={styles.container} |
| 47 | + onPress={() => this.setModalVisible(false)} |
| 48 | + underlayColor={"#333333cc"} |
| 49 | + > |
| 50 | + <View> |
| 51 | + <ListView |
| 52 | + dataSource={this.state.dataSource} |
| 53 | + renderRow={(rowData, sectionID, rowID, higlightRow) => { |
| 54 | + return ( |
| 55 | + <TouchableHighlight |
| 56 | + underlayColor={"transparent"} |
| 57 | + onPress={() => { |
| 58 | + this.setModalVisible(false); |
| 59 | + this.props.onValueChange(rowData[this.props.value], rowID); |
| 60 | + }} |
| 61 | + > |
| 62 | + {this.props.renderRow ? ( |
| 63 | + this.props.renderRow(rowData, rowID) |
| 64 | + ) : ( |
| 65 | + <Text style={styles.itemText}> |
| 66 | + {rowData[this.props.label]} |
| 67 | + </Text> |
| 68 | + )} |
| 69 | + </TouchableHighlight> |
| 70 | + ); |
| 71 | + }} |
| 72 | + /> |
| 73 | + </View> |
| 74 | + </TouchableHighlight> |
| 75 | + </Modal> |
| 76 | + </View> |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +ModalPicker.propTypes = { |
| 82 | + data: PropTypes.array.isRequired, |
| 83 | + value: PropTypes.string.isRequired, |
| 84 | + label: PropTypes.string.isRequired, |
| 85 | + onValueChange: PropTypes.func, |
| 86 | + renderRow: PropTypes.func |
| 87 | +}; |
| 88 | + |
| 89 | +const styles = StyleSheet.create({ |
| 90 | + container: { |
| 91 | + flex: 1, |
| 92 | + justifyContent: "center", |
| 93 | + backgroundColor: "#333333cc", |
| 94 | + padding: 16 |
| 95 | + }, |
| 96 | + itemText: { |
| 97 | + backgroundColor: "#fff", |
| 98 | + padding: 16, |
| 99 | + fontSize: 18, |
| 100 | + color: "#222", |
| 101 | + borderTopWidth: 1, |
| 102 | + borderColor: "#CCC" |
| 103 | + } |
| 104 | +}); |
0 commit comments