new_durationpicker.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { rpxToPx } from "@/utils/tools";
  2. import { PickerView, PickerViewColumn, Text, View } from "@tarojs/components";
  3. import { useEffect, useState } from "react";
  4. import './new_timepicker.scss'
  5. import dayjs from "dayjs";
  6. import { MainColorType } from "@/context/themes/color";
  7. import { TimeFormatter } from "@/utils/time_format";
  8. export enum DurationPickerType {
  9. moment = 'moment',
  10. normal = 'normal',
  11. long = 'long'
  12. }
  13. export default function NewDurationPicker(props: { value?: any, onChange?: any, color?: string, type?: DurationPickerType }) {
  14. const [items, setItems] = useState<any>([[0]])
  15. const [values, setValues] = useState<any>([0])
  16. const [loaded, setLoaded] = useState(false)
  17. useEffect(() => {
  18. switch (props.type) {
  19. case DurationPickerType.normal:
  20. setItems(itemDatas())
  21. setValues(itemValues())
  22. break;
  23. case DurationPickerType.long:
  24. setItems(longDatas())
  25. setValues(longValues())
  26. break;
  27. case DurationPickerType.moment:
  28. default:
  29. {
  30. var hours: any = []
  31. var tempValues: any = [props.value / 5]
  32. var array: any = []
  33. for (var i = 0; i <= 12 * 3; i++) {
  34. var count = i * 5;
  35. var hour = Math.floor(count / 60);
  36. var minute = count % 60;
  37. var str = ''
  38. if (hour > 0) {
  39. str = hour + '小时'
  40. }
  41. if (minute > 0) {
  42. str += minute + '分钟'
  43. }
  44. if (hour == 0 && minute == 0) {
  45. str = '0分钟'
  46. }
  47. array.push(str)
  48. }
  49. setItems([array])
  50. setValues(tempValues)
  51. }
  52. break;
  53. }
  54. setLoaded(true)
  55. }, [])
  56. function itemDatas() {
  57. var min: number = 1
  58. var max: number = 23
  59. var step: number = 5
  60. // if (common.duration) {
  61. min = 0//common.duration.min
  62. max = 23//common.duration.max
  63. step = 5//common.duration.step
  64. // }
  65. var minutes: string[] = []
  66. for (let i = 0; i < 60; i += step) {
  67. minutes.push(i + TimeFormatter.getMinutesUnit(i))
  68. }
  69. var hours: string[] = []
  70. for (let i = min; i <= max; i++) {
  71. hours.push(i + TimeFormatter.getHoursUnit(i))
  72. }
  73. return [hours, minutes]
  74. }
  75. function itemValues() {
  76. const seconds = props.value / 1000
  77. const hours = Math.floor(seconds / 3600)
  78. const minutes = Math.floor((seconds % 3600) / 60)
  79. return [hours, Math.floor(minutes / 5)]
  80. }
  81. function longDatas() {
  82. var min = 1
  83. var max = 99
  84. var days: string[] = []
  85. for (let i = min; i <= max; i++) {
  86. days.push(i + TimeFormatter.getDaysUnit(i))
  87. }
  88. var hours: string[] = []
  89. for (var i = 0; i < 24; i++) {
  90. hours.push(i + TimeFormatter.getHoursUnit(i))
  91. }
  92. return [days, hours]
  93. }
  94. function longValues() {
  95. if (props.value){
  96. var day = Math.floor(props.value/24)
  97. var hour = props.value%24
  98. return [day-1,hour]
  99. }
  100. return [0, 0]
  101. }
  102. function onPickerChange(e) {
  103. setValues(e.detail.value)
  104. if (props.onChange) {
  105. var list = e.detail.value
  106. switch (props.type) {
  107. case DurationPickerType.normal:
  108. props.onChange(list[0] * 3600 * 1000 + list[1] * 5 * 60 * 1000)
  109. break
  110. case DurationPickerType.long:
  111. props.onChange(list[0]*24+list[1]+24)
  112. break
  113. case DurationPickerType.moment:
  114. default:
  115. props.onChange(list[0] * 5)
  116. break
  117. }
  118. }
  119. }
  120. function getColor(i, j) {
  121. if (i == 0 && j == values[0]) {
  122. return true
  123. }
  124. if (i == 1 && j == values[1]) {
  125. return true
  126. }
  127. return false
  128. }
  129. if (!loaded) return <View />
  130. const bgStyle = `background-color: ${props.color}1A !important;`
  131. return <PickerView
  132. value={values}
  133. style={{ color: '#000', height: rpxToPx(340), width: rpxToPx(618) }}
  134. onChange={onPickerChange}
  135. indicatorClass="pick_sel_item"
  136. indicatorStyle={bgStyle}//""
  137. immediateChange={true}
  138. className="picker"
  139. >
  140. {
  141. items.map((item, index) => {
  142. return <PickerViewColumn key={index}>
  143. {item.map((obj, j) => {
  144. return (
  145. <Text key={j} style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', color: getColor(index, j) ? props.color ?? MainColorType.fast : '#000' }}>{obj}</Text>
  146. );
  147. })}
  148. </PickerViewColumn>
  149. })
  150. }
  151. </PickerView >
  152. }