YRModel.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import React from 'react';
  2. import {
  3. Modal,
  4. Text,
  5. TouchableHighlight,
  6. TouchableOpacity,
  7. View,
  8. StyleSheet,
  9. Image,
  10. TextInput
  11. } from 'react-native';
  12. import Picker from 'react-native-picker';
  13. import ModalDropdown from 'react-native-modal-dropdown';
  14. let Dimensions = require('Dimensions');
  15. let screenWidth = Dimensions.get('window').width;
  16. let dialogWidth = screenWidth - 40;
  17. const DEMO_OPTIONS_1 = ['option 1', 'option 2', 'option 3', 'option 4', 'option 5', 'option 6', 'option 7', 'option 8', 'option 9'];
  18. class YRModel extends React.Component {
  19. constructor(props) {
  20. super(props);
  21. this.state = {
  22. name: 'YRModel',
  23. modalVisible: false,
  24. selectTime:""
  25. };
  26. }
  27. setModalVisible(visible) {
  28. this.setState({modalVisible: visible});
  29. }
  30. componentDidMount() {
  31. }
  32. render() {
  33. /*自定义对话框*/
  34. return (<View style={styles.page}>
  35. <Modal animationType={"slide"}
  36. transparent={true}
  37. visible={this.state.modalVisible}
  38. onRequestClose={() => {
  39. this.setModalVisible(false)
  40. }}>
  41. {/* <TouchableOpacity style={{flex:1}} >*/}
  42. <View style={styles.container}>
  43. <View style={styles.innerContainer}>
  44. <View style={styles.header}>
  45. <Image
  46. style={{width:22,height:22}}
  47. source={require('../../assets/images/icon_delete.png')}></Image>
  48. </View>
  49. <View style={styles.content}>
  50. <TextInput
  51. style={styles.inputtext}
  52. placeholder="Type here!"
  53. />
  54. <TouchableHighlight
  55. style={styles.btnSure}
  56. underlayColor="#82D2F8"
  57. onPress={() => {
  58. this._showTimePicker();
  59. }}>
  60. <Text style={styles.textSure}>选择时间:{this.state.selectTime}</Text>
  61. </TouchableHighlight>
  62. <ModalDropdown
  63. defaultValue="请下拉选择选项"
  64. style={styles.dropdown_2}
  65. textStyle={styles.dropdown_2_text}
  66. dropdownStyle={styles.dropdown_2_dropdown}
  67. options={DEMO_OPTIONS_1}
  68. />
  69. </View>
  70. <View style={styles.btnContainer}>
  71. <TouchableHighlight
  72. style={styles.btnClose}
  73. underlayColor="#fff"
  74. onPress={() => {
  75. this.setModalVisible(!this.state.modalVisible)
  76. }}>
  77. <Text style={styles.textClose}>重置</Text>
  78. </TouchableHighlight>
  79. <TouchableHighlight
  80. style={styles.btnSure}
  81. underlayColor="#82D2F8"
  82. onPress={() => {
  83. this.setModalVisible(!this.state.modalVisible)
  84. }}>
  85. <Text style={styles.textSure}>确认</Text>
  86. </TouchableHighlight>
  87. </View>
  88. </View>
  89. </View>
  90. {/* </TouchableOpacity>*/}
  91. </Modal>
  92. <TouchableHighlight
  93. underlayColor="#1FB579"
  94. style={styles.textContainer} onPress={() => {
  95. this.setModalVisible(true)
  96. }}>
  97. <Text style={{ fontSize:22}}>弹出对话框</Text>
  98. </TouchableHighlight>
  99. </View>)
  100. }
  101. _showTimePicker() {
  102. let that=this;
  103. let years = [],
  104. months = [],
  105. days = [],
  106. hours = [],
  107. minutes = [];
  108. for(let i=1;i<51;i++){
  109. years.push(i+1980);
  110. }
  111. for(let i=1;i<13;i++){
  112. months.push(i);
  113. hours.push(i);
  114. }
  115. for(let i=1;i<32;i++){
  116. days.push(i);
  117. }
  118. for(let i=1;i<61;i++){
  119. minutes.push(i);
  120. }
  121. let pickerData = [years, months, days, ['上午', '下午'], hours, minutes];
  122. let date = new Date();
  123. let selectedValue = [
  124. date.getFullYear(),
  125. date.getMonth()+1,
  126. date.getDate(),
  127. date.getHours() > 11 ? '下午' : '上午',
  128. date.getHours() === 12 ? 12 : date.getHours()%12,
  129. date.getMinutes()
  130. ];
  131. Picker.init({
  132. pickerData,
  133. selectedValue,
  134. pickerBg:[255,255,255,1],
  135. pickerCancelBtnText:'取消',
  136. pickerConfirmBtnText:'确定',
  137. pickerTitleText: '时间选择',
  138. wheelFlex: [2, 1, 1, 2, 1, 1],
  139. onPickerConfirm: pickedValue => {
  140. console.log('area', pickedValue);
  141. let selectTime=pickedValue[0]+"年"+pickedValue[1]+"月"+pickedValue[2]+"日"+pickedValue[3]+" "+pickedValue[4]+"时"+pickedValue[5]+"分";
  142. // this.refs.toast.show('pickedValue:'+selectTime,6000);
  143. that.setState({
  144. selectTime:selectTime
  145. })
  146. },
  147. onPickerCancel: pickedValue => {
  148. console.log('area', pickedValue);
  149. },
  150. onPickerSelect: pickedValue => {
  151. let targetValue = [...pickedValue];
  152. if(parseInt(targetValue[1]) === 2){
  153. if(targetValue[0]%4 === 0 && targetValue[2] > 29){
  154. targetValue[2] = 29;
  155. }
  156. else if(targetValue[0]%4 !== 0 && targetValue[2] > 28){
  157. targetValue[2] = 28;
  158. }
  159. }
  160. else if(targetValue[1] in {4:1, 6:1, 9:1, 11:1} && targetValue[2] > 30){
  161. targetValue[2] = 30;
  162. }
  163. // forbidden some value such as some 2.29, 4.31, 6.31...
  164. if(JSON.stringify(targetValue) !== JSON.stringify(pickedValue)){
  165. // android will return String all the time,but we put Number into picker at first
  166. // so we need to convert them to Number again
  167. targetValue.map((v, k) => {
  168. if(k !== 3){
  169. targetValue[k] = parseInt(v);
  170. }
  171. });
  172. Picker.select(targetValue);
  173. pickedValue = targetValue;
  174. }
  175. }
  176. });
  177. Picker.show();
  178. }
  179. }
  180. const styles = StyleSheet.create({
  181. page:{
  182. flex: 1,
  183. justifyContent: 'center',
  184. },
  185. container: {
  186. flex: 1,
  187. justifyContent: 'center',
  188. padding: 20,
  189. backgroundColor: 'rgba(0, 0, 0, 0.5)'
  190. },
  191. innerContainer: {
  192. borderRadius: 3,
  193. alignItems: 'center',
  194. backgroundColor: '#fff',
  195. borderColor:"#fff",
  196. borderWidth:1,
  197. },
  198. header:{
  199. width:dialogWidth,
  200. flexDirection:'row',
  201. justifyContent:'flex-start',
  202. padding:8
  203. },
  204. content:{
  205. borderColor:"#82D2F8",
  206. },
  207. btnContainer:{
  208. flexDirection:"row",
  209. justifyContent:"flex-end",
  210. width:dialogWidth,
  211. borderTopWidth:1,
  212. paddingTop:10,
  213. borderTopColor:'#E7E7E7',
  214. alignItems:'center',
  215. padding:3
  216. },
  217. inputtext:{
  218. width:dialogWidth-20,
  219. margin:10,
  220. },
  221. textSure: {
  222. color:"#fff"
  223. },
  224. textClose:{
  225. color:"#82D2F8"
  226. },
  227. textContainer:{
  228. borderColor:"#fff",
  229. borderWidth:1,
  230. flexDirection:'row',
  231. justifyContent:"center",
  232. alignItems:"center",
  233. },
  234. btnClose:{
  235. borderColor:"#82D2F8",
  236. borderWidth:1,
  237. borderRadius:5,
  238. paddingTop:6,
  239. paddingBottom:6,
  240. paddingLeft:20,
  241. paddingRight:20,
  242. backgroundColor:"#fff"
  243. },
  244. btnSure:{
  245. borderColor:"#82D2F8",
  246. borderWidth:1,
  247. borderRadius:5,
  248. paddingTop:6,
  249. paddingBottom:6,
  250. paddingLeft:20,
  251. paddingRight:20,
  252. margin:10,
  253. backgroundColor:"#82D2F8"
  254. },
  255. dropdown_2: {
  256. alignSelf: 'center',
  257. justifyContent:"center",
  258. width:dialogWidth-20,
  259. height:40,
  260. marginBottom: 32,
  261. borderWidth: 1,
  262. borderRadius: 2,
  263. borderColor:"#E7E7E7"
  264. },
  265. dropdown_2_text: {
  266. marginVertical: 10,
  267. marginHorizontal: 6,
  268. fontSize: 13,
  269. textAlign: 'center',
  270. textAlignVertical: 'center',
  271. },
  272. dropdown_2_dropdown: {
  273. width:dialogWidth-20,
  274. height: 100,
  275. borderColor: '#E7E7E7',
  276. borderWidth: 1,
  277. borderRadius: 3,
  278. }
  279. });
  280. export default YRModel;