choose_date_time.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { View, Image } from "@tarojs/components"
  2. import './choose_date_time.scss'
  3. import { rpxToPx, vibrate } from "@/utils/tools"
  4. import NewButton, { NewButtonType } from "../base/new_button"
  5. import { useEffect, useState } from "react"
  6. import NewDatePicker, { NewDatePickerType } from "../base/new_date_picker"
  7. import NewTimePicker from "../base/new_timepicker"
  8. import { IconCalendar } from "@/components/basic/Icons"
  9. import dayjs from "dayjs"
  10. import NewDateTimePicker from "../base/new_date_time_picker"
  11. import { TimeFormatter } from "@/utils/time_format"
  12. import { useTranslation } from "react-i18next"
  13. import { MainColorType } from "@/context/themes/color"
  14. export default function ChooseDateTime(props: {
  15. title?: any,
  16. disable?: boolean,
  17. showError?: boolean,
  18. showLine?: boolean,
  19. minTimestamp?: number,
  20. maxTimestamp?: number,
  21. hideFooter?: boolean,
  22. footerTitle?: string,
  23. tapFooter?: any,
  24. count?: number,
  25. color: string,
  26. date: string,
  27. time: string,
  28. expand: boolean,
  29. choose?: any,
  30. timeChange?: any,
  31. dateChange?: any,
  32. change?: any,
  33. enterTimestamp?: number,
  34. targetTimestamp?: number
  35. }) {
  36. const [chooseDate, setChooseDate] = useState(false)
  37. const { t } = useTranslation()
  38. const [count, setCount] = useState(0)
  39. useEffect(() => {
  40. console.log('sss')
  41. setCount(count => count + 1)
  42. }, [props.count])
  43. function dateTitle() {
  44. if (props.date == '') return global.language == 'en' ? 'Date' : '日期'
  45. var date = dayjs(props.date)
  46. if (TimeFormatter.isToday(date.unix() * 1000)) {
  47. return global.language == 'en' ? 'Today' : '今天'
  48. }
  49. if (TimeFormatter.isYesterday(date.unix() * 1000)) {
  50. return global.language == 'en' ? 'Yesterday' : '昨天'
  51. }
  52. else if (TimeFormatter.isTimestampInThisWeek(date.unix() * 1000)) {
  53. return date.format('dddd')
  54. }
  55. else {
  56. return global.language == 'en' ? date.format('MMM D') : date.format('MMMD日')
  57. }
  58. }
  59. function timeTitle() {
  60. if (props.time == '')
  61. return global.language == 'en' ? 'Time' : '时间'
  62. return props.time
  63. }
  64. function reset() {
  65. props.change([
  66. dayjs(props.enterTimestamp).format('YYYY-MM-DD'),
  67. dayjs(props.enterTimestamp).format('HH:mm')
  68. ])
  69. }
  70. function tapTarget() {
  71. props.change([
  72. dayjs(props.targetTimestamp).format('YYYY-MM-DD'),
  73. dayjs(props.targetTimestamp).format('HH:mm')
  74. ])
  75. }
  76. function targetDate(){
  77. if (TimeFormatter.isToday(props.targetTimestamp!)){
  78. return TimeFormatter.getTodayUnit()+' '
  79. }
  80. if (TimeFormatter.isYesterday(props.targetTimestamp!)){
  81. return TimeFormatter.getYesterdayUnit()+' '
  82. }
  83. if (TimeFormatter.isTomorrowday(props.targetTimestamp!)){
  84. return TimeFormatter.getTomorrowUnit()+' '
  85. }
  86. return global.language=='en'?dayjs(props.targetTimestamp).format('MMM D '):dayjs(props.targetTimestamp).format('MMMD日')
  87. }
  88. function footerTitle() {
  89. if (dayjs(props.targetTimestamp!).format('YYYY-MM-DD HH:mm') == props.date + ' ' + props.time) {
  90. return <View style={{ display: 'flex', flexDirection: 'row' }} onClick={reset}>
  91. <View className="h26 g02">{t('health.select_schedule')}</View>
  92. <View className="h26" style={{ color: MainColorType.link, marginLeft: rpxToPx(8) }}>{t('health.reset')}</View>
  93. </View>
  94. }
  95. if (new Date().getTime() > props.targetTimestamp!) {
  96. return <View style={{ display: 'flex', flexDirection: 'row' }} onClick={tapTarget}>
  97. <View className="h26 g02">{t('health.schedule_for', { time: targetDate()+dayjs(props.targetTimestamp).format('HH:mm') })}</View>
  98. <View className="h26" style={{ color: MainColorType.link, marginLeft: rpxToPx(8) }}>{t('health.select')}</View>
  99. </View>
  100. }
  101. else {
  102. return <View onClick={(e)=>{
  103. vibrate()
  104. }}>
  105. <View className="h26 g02">{t('health.schedule_for', { time: targetDate()+dayjs(props.targetTimestamp).format('HH:mm') })}</View>
  106. </View>
  107. }
  108. }
  109. return <View style={{ position: 'relative' }}>
  110. <View className="card_header">
  111. {
  112. props.title ? <View className="h34" style={{ flex: 1, color: '#000' }}>{props.title}</View> :
  113. <View style={{ flex: 1 }} />
  114. }
  115. {/* <View className="h34" style={{color:props.color}}>{dateTitle()} {timeTitle()}</View> */}
  116. {
  117. !props.title ? <View className="h44" style={{ color: props.color }}
  118. onClick={() => {
  119. // setChooseDate(false)
  120. if (props.choose) {
  121. props.choose()
  122. }
  123. }}
  124. >{dateTitle()} {timeTitle()}</View> :
  125. <View style={{
  126. borderColor: props.showError ? 'red' : (props.expand && !chooseDate) ? props.color : 'transparent',
  127. borderWidth: rpxToPx(2),
  128. borderRadius: rpxToPx(88 / 4),
  129. borderStyle: 'solid'
  130. }}>
  131. <NewButton
  132. type={(props.expand && !chooseDate && !props.showError) ? NewButtonType.alpha : NewButtonType.gray}
  133. color={props.color}
  134. title={dateTitle() + ' ' + timeTitle()}
  135. fontSize={rpxToPx(34)}
  136. width={rpxToPx(296)}
  137. height={rpxToPx(84)}
  138. disable={props.disable}
  139. fontNormal
  140. onClick={() => {
  141. setChooseDate(false)
  142. if (props.choose) {
  143. props.choose()
  144. }
  145. }}
  146. />
  147. </View>
  148. }
  149. {
  150. !props.title && <View style={{ flex: 1 }} />
  151. }
  152. <View className='border_footer_line' style={{ left: rpxToPx(40),right:rpxToPx(40) }} />
  153. </View>
  154. {
  155. props.expand && !chooseDate && <View style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', width: rpxToPx(698), height: rpxToPx(340) }}>
  156. {/* <NewTimePicker time={props.time} onChange={(e) => {
  157. props.timeChange(e)
  158. }} color={props.color} /> */}
  159. <NewDateTimePicker date={props.date} count={props.count} time={props.time}
  160. minTimestamp={props.minTimestamp ? props.minTimestamp : new Date().getTime() - 24 * 3600 * 1000}
  161. maxTimestamp={props.maxTimestamp ?? new Date().getTime()}
  162. onChange={(e) => {
  163. if (props.dateChange)
  164. props.dateChange(e[0])
  165. if (props.timeChange)
  166. props.timeChange(e[1])
  167. props.change(e)
  168. }} color={props.color} />
  169. </View>
  170. }
  171. {
  172. props.expand && !chooseDate && !props.hideFooter && <View className='card_footer'>
  173. {
  174. props.footerTitle
  175. }
  176. {
  177. props.targetTimestamp && footerTitle()
  178. }
  179. {/* <NewButton type={NewButtonType.link}
  180. title={props.footerTitle}
  181. onClick={props.tapFooter}
  182. >
  183. <View style={{ marginRight: rpxToPx(12), display: 'flex' }}>
  184. <IconCalendar width={rpxToPx(24)} color='#5C7099' />
  185. </View>
  186. </NewButton> */}
  187. </View>
  188. }
  189. {
  190. props.showLine && <View className="border_footer_line" style={{ left: rpxToPx(66) }} />
  191. }
  192. </View>
  193. }