BAPickerModal.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React, {Component} from 'react';
  2. import {
  3. Modal,
  4. Text,
  5. TouchableOpacity,
  6. View,
  7. StyleSheet
  8. } from 'react-native';
  9. import BAPicker from './BAPicker';
  10. import PropTypes from 'prop-types';
  11. class BAButton extends Component {
  12. render() {
  13. const {
  14. style,
  15. title,
  16. onPress
  17. } = this.props;
  18. return (
  19. <TouchableOpacity style={[styles.btnStyle, style]} onPress={() => onPress()}>
  20. <Text style={{color: 'white'}}>{title}</Text>
  21. </TouchableOpacity>
  22. )
  23. }
  24. }
  25. BAButton.propTypes = {
  26. style: PropTypes.any,
  27. title: PropTypes.string,
  28. onPress: PropTypes.func
  29. };
  30. export default class BAPickerModal extends Component {
  31. constructor(props) {
  32. super(props);
  33. this.state = {
  34. selectedIndex: this.props.selectedIndex
  35. }
  36. }
  37. render() {
  38. const {
  39. modalVisible,
  40. onClosePress,
  41. list,
  42. onConfirm,
  43. itemStyle,
  44. topBarStyle,
  45. cancelTitle=null,
  46. confirmTitle=null,
  47. title = ""
  48. } = this.props;
  49. return (
  50. <Modal
  51. animationType="none"
  52. transparent={true}
  53. visible={modalVisible}
  54. onRequestClose={() => { onClosePress() }}>
  55. <View style={styles.modalContainer}>
  56. <View style={[styles.topContainer, topBarStyle]}>
  57. <BAButton title={cancelTitle? cancelTitle: "取消"} onPress={() => onClosePress()} />
  58. <View style={{flex: 1, justifyContent: 'center', alignItems:'center'}}>
  59. <Text style={{color: 'white', fontSize: 16}}>{title?title: ""}</Text>
  60. </View>
  61. <BAButton title={confirmTitle?confirmTitle: "确定"} onPress={() => onConfirm({index: this.state.selectedIndex, item: list[this.state.selectedIndex]})} />
  62. </View>
  63. <View style={styles.pickerStyle}>
  64. <BAPicker
  65. style={styles.pickerStyle}
  66. itemStyle={itemStyle}
  67. data={list}
  68. selectedIndex={this.state.selectedIndex}
  69. onValueChange={(resultObj) => { this.setState({selectedIndex: resultObj.index}); }}/>
  70. </View>
  71. </View>
  72. </Modal>
  73. )
  74. }
  75. }
  76. BAPickerModal.propTypes = {
  77. modalVisible: PropTypes.bool,
  78. onClosePress: PropTypes.func,
  79. list: PropTypes.array,
  80. selectedIndex: PropTypes.number,
  81. onConfirm: PropTypes.func,
  82. itemStyle: PropTypes.object,
  83. topBarStyle: PropTypes.object,
  84. cancelTitle: PropTypes.string,
  85. confirmTitle: PropTypes.string,
  86. title: PropTypes.string
  87. };
  88. const styles = StyleSheet.create({
  89. modalContainer: {
  90. width: '100%',
  91. height: '100%',
  92. justifyContent: 'flex-end',
  93. backgroundColor: '#00000033'
  94. },
  95. topContainer: {
  96. width: '100%',
  97. height: 44,
  98. flexDirection: 'row',
  99. backgroundColor: '#58A8F5'
  100. },
  101. pickerStyle: {
  102. flex: 0,
  103. width: '100%',
  104. height: 200,
  105. backgroundColor: 'white',
  106. overflow: "hidden"
  107. },
  108. btnStyle: {
  109. width: 60,
  110. height: '100%',
  111. justifyContent: 'center',
  112. alignItems: 'center'
  113. }
  114. });