choose_date_time.tsx 8.1 KB

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