selectTime.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import React, { Component } from 'react'
  2. import {
  3. StyleSheet,
  4. View,
  5. Text,
  6. Dimensions,
  7. TouchableOpacity,
  8. Platform,
  9. DatePickerAndroid,
  10. } from 'react-native'
  11. import { connect } from 'react-redux'
  12. import Collapsible from 'react-native-collapsible'
  13. import SelectTime from '../../containers/common/SelectTime'
  14. import { setNowFormatDate } from '../../utils/utils'
  15. import moment from 'moment'
  16. const { width, height } = Dimensions.get('window')
  17. @connect(({ theme }) => ({
  18. ...theme,
  19. }))
  20. class SelectTimeCom extends Component {
  21. constructor(props) {
  22. const date = new Date()
  23. super(props)
  24. this.state = {
  25. // 开始时间
  26. startTime: setNowFormatDate(true),
  27. // date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
  28. // 结束时间
  29. endTime: setNowFormatDate(),
  30. // date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()
  31. }
  32. }
  33. async TimeAndroid(mark) {
  34. const { action, year, month, day } = await DatePickerAndroid.open({
  35. date: new Date(),
  36. // minDate: new Date(),
  37. mode: 'spinner',
  38. })
  39. if (action !== DatePickerAndroid.dismissedAction) {
  40. let newDate = `${year}-${(month + 1)<10?"0"+month:month}-${day}`
  41. newDate = moment(newDate, 'YYYY-MM-DD').format('YYYY-MM-DD')
  42. if (mark == 'start') {
  43. if (newDate <= this.state.endTime) {
  44. this.setState({
  45. startTime: newDate,
  46. })
  47. }
  48. } else if (mark == 'end') {
  49. if (this.state.startTime <= newDate) {
  50. this.setState({
  51. endTime: newDate,
  52. })
  53. }
  54. }
  55. // 这里开始可以处理用户选好的年月日三个参数:year, month (0-11), day
  56. }
  57. }
  58. render() {
  59. return (
  60. <View
  61. style={{
  62. position: 'absolute',
  63. top: 60,
  64. left: 0,
  65. width: width,
  66. backgroundColor: '#FFF',
  67. }}
  68. >
  69. <Collapsible collapsed={this.props.dateActive}>
  70. <View>
  71. <View
  72. style={{
  73. flexDirection: 'row',
  74. justifyContent: 'space-between',
  75. padding: 20,
  76. marginBottom: 20,
  77. }}
  78. >
  79. <View>
  80. <Text
  81. style={{
  82. fontSize: 13,
  83. lineHeight: 18,
  84. letterSpacing: 0.16,
  85. color: '#333',
  86. alignSelf: 'center',
  87. }}
  88. >
  89. 开始时间
  90. </Text>
  91. <TouchableOpacity
  92. onPress={() =>
  93. Platform.OS == 'ios'
  94. ? this.showTime._openModal('start')
  95. : this.TimeAndroid('start')
  96. }
  97. style={{
  98. paddingHorizontal: 40,
  99. paddingVertical: 7,
  100. marginTop: 16,
  101. borderColor: '#DDD',
  102. borderWidth: 1,
  103. borderRadius: 100,
  104. }}
  105. >
  106. <Text
  107. style={{
  108. fontSize: 13,
  109. lineHeight: 18,
  110. letterSpacing: 0.16,
  111. color: '#333',
  112. }}
  113. >
  114. {this.state.startTime}
  115. </Text>
  116. </TouchableOpacity>
  117. </View>
  118. <View style={{ paddingTop: 37 }}>
  119. <Text
  120. style={{
  121. fontSize: 18,
  122. lineHeight: 25,
  123. letterSpacing: 0.22,
  124. color: '#333',
  125. }}
  126. >
  127. ~
  128. </Text>
  129. </View>
  130. <View>
  131. <Text
  132. style={{
  133. fontSize: 13,
  134. lineHeight: 18,
  135. letterSpacing: 0.16,
  136. color: '#333',
  137. alignSelf: 'center',
  138. }}
  139. >
  140. 结束时间
  141. </Text>
  142. <TouchableOpacity
  143. onPress={() =>
  144. Platform.OS == 'ios'
  145. ? this.showTime._openModal('end')
  146. : this.TimeAndroid('end')
  147. }
  148. style={{
  149. paddingHorizontal: 40,
  150. paddingVertical: 7,
  151. marginTop: 16,
  152. borderColor: '#DDD',
  153. borderWidth: 1,
  154. borderRadius: 100,
  155. }}
  156. >
  157. <Text
  158. style={{
  159. fontSize: 13,
  160. lineHeight: 18,
  161. letterSpacing: 0.16,
  162. color: '#333',
  163. }}
  164. >
  165. {this.state.endTime}
  166. </Text>
  167. </TouchableOpacity>
  168. </View>
  169. </View>
  170. <View style={{ height: 40, flexDirection: 'row' }}>
  171. <TouchableOpacity
  172. onPress={() => {
  173. data = { fn: 'cancel' }
  174. this.props.dateCb(data)
  175. }}
  176. style={{
  177. width: width / 2,
  178. backgroundColor: '#FFF',
  179. justifyContent: 'center',
  180. }}
  181. >
  182. <Text
  183. style={{
  184. fontSize: 14,
  185. lineHeight: 22,
  186. letterSpacing: 0.21,
  187. color: '#333',
  188. alignSelf: 'center',
  189. }}
  190. >
  191. 取消
  192. </Text>
  193. </TouchableOpacity>
  194. <TouchableOpacity
  195. onPress={() => {
  196. data = {
  197. startTime: this.state.startTime,
  198. endTime: this.state.endTime,
  199. }
  200. this.props.dateCb(data)
  201. }}
  202. style={{
  203. width: width / 2,
  204. backgroundColor: '#E70013',
  205. justifyContent: 'center',
  206. }}
  207. >
  208. <Text
  209. style={{
  210. fontSize: 14,
  211. lineHeight: 22,
  212. letterSpacing: 0.21,
  213. color: '#FFF',
  214. alignSelf: 'center',
  215. }}
  216. >
  217. 确定
  218. </Text>
  219. </TouchableOpacity>
  220. </View>
  221. </View>
  222. </Collapsible>
  223. {/* 时间弹窗 */}
  224. <SelectTime
  225. ref={showTime => (this.showTime = showTime)}
  226. cb={(date, mark) => {
  227. let newDate = moment(date).format('YYYY-MM-DD')
  228. if (mark == 'start') {
  229. if (newDate <= this.state.endTime) {
  230. this.setState({
  231. startTime: newDate,
  232. })
  233. }
  234. } else if (mark == 'end') {
  235. if (this.state.startTime <= newDate) {
  236. this.setState({
  237. endTime: newDate,
  238. })
  239. }
  240. }
  241. }}
  242. />
  243. </View>
  244. )
  245. }
  246. }
  247. const styles = StyleSheet.create({
  248. container: {
  249. flex: 1,
  250. },
  251. footer: {
  252. flexDirection: 'row',
  253. height: 24,
  254. justifyContent: 'center',
  255. alignItems: 'center',
  256. marginBottom: 10,
  257. marginTop: 10,
  258. },
  259. })
  260. export default SelectTimeCom