-
Notifications
You must be signed in to change notification settings - Fork 3
/
TimeSelector.tsx
148 lines (128 loc) · 4.64 KB
/
TimeSelector.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Selector for departure time.
* @module
*/
import React, {MutableRefObject, useCallback, useEffect, useRef, useState} from 'react';
import '../../styles/timeSelector.scss';
import * as Types from "../../App";
import { isContext } from 'vm';
import { useFormContext } from 'react-hook-form';
/**
* State of the component.
* @field hour Hour part of the time selector
* @field setHour Setter for hour
* @field minutes Minute part of the time selector
* @field setMinutes Setter for minutes
*/
export interface TimeSelectorProps {
settings: Types.SessionType;
setSettings: React.Dispatch<React.SetStateAction<Types.SessionType>>,
}
export default function TimeSelector() {
const context = useFormContext();
const hrUpRef = useRef() as MutableRefObject<HTMLDivElement>;
const hrDownRef = useRef() as MutableRefObject<HTMLDivElement>;
const minUpRef = useRef() as MutableRefObject<HTMLDivElement>;
const minDownRef = useRef() as MutableRefObject<HTMLDivElement>;
const hourInputRef = useRef() as MutableRefObject<HTMLInputElement>;
const minutesInputRef = useRef() as MutableRefObject<HTMLInputElement>;
const [time, setTime] = useState({hour: 17, minutes: 30});
useEffect(() => {
let d = new Date();
d.setHours(time.hour);
d.setMinutes(time.minutes);
context.setValue("departure", d);
}, [context, time, setTime]);
const hourUp = useCallback(() => {
// setSettings({
// ...settings,
// hour: ((((settings.hour + 1) % 24) + 24) % 24),
// })
setTime({
...time,
hour: ((((time.hour + 1) % 24) + 24) % 24),
})
}, [time, setTime])
const hourDown = useCallback(() => {
// setSettings({
// ...settings,
// hour: ((((settings.hour - 1) % 24) + 24) % 24),
// })
setTime({
...time,
hour: ((((time.hour - 1) % 24) + 24) % 24),
})
}, [time, setTime])
const minutesUp = useCallback(() => {
// setSettings({
// ...settings,
// minutes: ((((settings.minutes + 15) % 60) + 60) % 60),
// })
setTime({
...time,
minutes: ((((time.minutes + 15) % 60) + 60) % 60),
})
}, [time, setTime])
const minutesDown = useCallback(() => {
// setSettings({
// ...settings,
// minutes: ((((settings.minutes - 15) % 60) + 60) % 60),
// })
setTime({
...time,
minutes: ((((time.minutes - 15) % 60) + 60) % 60),
})
}, [time, setTime])
useEffect(() => {
let hrUpRefCurrent = hrUpRef.current;
let hrDownRefCurrent = hrDownRef.current;
let minUpRefCurrent = minUpRef.current;
let minDownRefCurrent = minDownRef.current;
hrUpRefCurrent.addEventListener('click', hourUp);
hrDownRefCurrent.addEventListener('click', hourDown);
minUpRefCurrent.addEventListener('click', minutesUp);
minDownRefCurrent.addEventListener('click', minutesDown);
return () => {
hrUpRefCurrent.removeEventListener('click', hourUp)
hrDownRefCurrent.removeEventListener('click', hourDown);
minUpRefCurrent.removeEventListener('click', minutesUp);
minDownRefCurrent.removeEventListener('click', minutesDown);
};
});
useEffect(() => {
hourInputRef.current.value = formatTime(time.hour);
},[time.hour, hourInputRef])
useEffect(() => {
minutesInputRef.current.value = formatTime(time.minutes)
}, [time.minutes, minutesInputRef])
const formatTime = (time: number): string => {
if(time < 10){
return '0' + time.toString();
}
return time.toString();
};
// Body of the component
return (
<div className="time-picker">
<div className="hour">
<div ref={hrUpRef} className="hr-up"/>
<input disabled
ref={hourInputRef}
type="number"
className="hr"
value={time.hour}/>
<div ref={hrDownRef} className="hr-down"/>
</div>
<div className="separator">:</div>
<div className="minute">
<div ref={minUpRef} className="min-up"/>
<input disabled
ref={minutesInputRef}
type="number"
className="min"
value={time.minutes}/>
<div ref={minDownRef} className="min-down"/>
</div>
</div>
);
}