TimePicker.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import PickerViews from "@/components/input/PickerViews";
  2. import { PickerView, PickerViewColumn, View } from "@tarojs/components";
  3. import { useEffect, useState } from "react";
  4. export default function Component(props: { time: string, confirm: Function, cancel: Function, title?: string, color?: string }) {
  5. var [hour, minute] = props.time.split(':').map(x => parseInt(x))
  6. const m = Math.round(minute / 5) * 5;
  7. const v = [hour, m / 5];
  8. const [dt, setDt] = useState(v)
  9. useEffect(() => {
  10. var [hour, minute] = props.time.split(':').map(x => parseInt(x))
  11. const m = Math.round(minute / 5) * 5;
  12. const v = [hour, m / 5];
  13. setDt(v)
  14. }, [props.time])
  15. const hours: string[] = [];
  16. for (let i = 0; i <= 23; i++) {
  17. var h = i<10?'0'+i:i
  18. hours.push(h as any);
  19. }
  20. const minutes: string[] = [];
  21. for (let i = 0; i <= 11; i++) {
  22. var ms = i*5
  23. var m1 = ms<10?'0'+ms:ms+''
  24. minutes.push(m1 as any);
  25. }
  26. function onPickerChange(e) {
  27. var h = parseInt( hours[e[0]])
  28. var m = parseInt(minutes[e[1]])
  29. var strHour =h < 10 ? '0' + h : h
  30. var strMinute = m< 10 ? '0' + m : m
  31. props.confirm(strHour + ':' + strMinute)
  32. }
  33. return <PickerViews themeColor={props.color} title={props.title} value={dt} showBtns={true} onChange={onPickerChange} items={[hours, minutes]} onCancel={() => { props.cancel() }} />
  34. }