new_durationpicker.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. export default function NewDurationPicker(props: { value?: any, onChange?: any, color?: string }) {
  8. const [items, setItems] = useState<any>([[0]])
  9. const [values, setValues] = useState<any>([0])
  10. const [loaded, setLoaded] = useState(false)
  11. useEffect(() => {
  12. var hours: any = []
  13. var tempValues: any = [props.value/5]
  14. var array:any = []
  15. for (var i=0;i<=12*3;i++){
  16. var count = i*5;
  17. var hour = Math.floor(count/60);
  18. var minute = count%60;
  19. var str = ''
  20. if (hour>0){
  21. str = hour+'小时'
  22. }
  23. if (minute>0){
  24. str += minute+'分钟'
  25. }
  26. if (hour==0 && minute==0){
  27. str = '0分钟'
  28. }
  29. array.push(str)
  30. }
  31. setItems([array])
  32. setValues(tempValues)
  33. setLoaded(true)
  34. }, [])
  35. function onPickerChange(e) {
  36. setValues(e.detail.value)
  37. if (props.onChange) {
  38. var list = e.detail.value
  39. props.onChange(list[0]*5)
  40. }
  41. }
  42. function getColor(i, j) {
  43. if (i == 0 && j == values[0]) {
  44. return true
  45. }
  46. if (i == 1 && j == values[1]) {
  47. return true
  48. }
  49. return false
  50. }
  51. if (!loaded) return <View />
  52. const bgStyle = `background-color: ${props.color}1A !important;`
  53. return <PickerView
  54. value={values}
  55. style={{ color: '#000', height: rpxToPx(340), width: rpxToPx(670) }}
  56. onChange={onPickerChange}
  57. indicatorClass="pick_sel_item"
  58. indicatorStyle={bgStyle}//""
  59. immediateChange={true}
  60. className="picker"
  61. >
  62. {
  63. items.map((item, index) => {
  64. return <PickerViewColumn key={index}>
  65. {item.map((obj, j) => {
  66. return (
  67. <Text key={j} style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', color: getColor(index, j) ? props.color ?? MainColorType.fast : '#000' }}>{obj}</Text>
  68. );
  69. })}
  70. </PickerViewColumn>
  71. })
  72. }
  73. </PickerView >
  74. }