Skip to content

Commit 45a6806

Browse files
committed
Time picker fix
1 parent 76971fe commit 45a6806

File tree

2 files changed

+20
-61
lines changed

2 files changed

+20
-61
lines changed

src/app/Home.tsx

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ export default function Home() {
4040
dayjs(endTime, 'HH:mm').isBefore(dayjs(startTime, 'HH:mm')),
4141
);
4242

43-
// for saving the input values on change but only updating them onBlur or onKeyDown+enter
44-
const startTimeChange = useRef<Dayjs | null>(startTime);
45-
const endTimeChange = useRef<Dayjs | null>(endTime);
46-
4743
const [nearby, setNearby] = useState(false);
4844

4945
// only show checkbox if location is possible
@@ -134,7 +130,7 @@ export default function Home() {
134130
label="Start time"
135131
value={startTime}
136132
timeSteps={{ minutes: 15 }}
137-
onChange={(newValue) => (startTimeChange.current = newValue)}
133+
onChange={(newValue) => setStartTime(newValue)}
138134
onAccept={(newValue) =>
139135
setStartTime(newValue == null ? null : snapTime(newValue))
140136
}
@@ -151,32 +147,14 @@ export default function Home() {
151147
},
152148
error: error,
153149
helperText: error && 'Start time must be before end time',
154-
onBlur: () => {
155-
setStartTime(
156-
startTimeChange.current == null ||
157-
!startTimeChange.current.isValid()
158-
? null
159-
: snapTime(startTimeChange.current),
160-
);
161-
},
162-
onKeyDown: (e) => {
163-
if (e.key === 'Enter') {
164-
setStartTime(
165-
startTimeChange.current == null ||
166-
!startTimeChange.current.isValid()
167-
? null
168-
: snapTime(startTimeChange.current),
169-
);
170-
}
171-
},
172150
},
173151
}}
174152
/>
175153
<TimePicker
176154
label="End time"
177155
value={endTime}
178156
timeSteps={{ minutes: 15 }}
179-
onChange={(newValue) => (endTimeChange.current = newValue)}
157+
onChange={(newValue) => setEndTime(newValue)}
180158
onAccept={(newValue) =>
181159
setEndTime(newValue == null ? null : snapTime(newValue))
182160
}
@@ -193,25 +171,6 @@ export default function Home() {
193171
},
194172
error: error,
195173
helperText: error && 'Start time must be before end time',
196-
onBlur: () => {
197-
console.log(endTimeChange.current);
198-
setEndTime(
199-
endTimeChange.current == null ||
200-
!endTimeChange.current.isValid()
201-
? null
202-
: snapTime(endTimeChange.current),
203-
);
204-
},
205-
onKeyDown: (e) => {
206-
if (e.key === 'Enter') {
207-
setEndTime(
208-
endTimeChange.current == null ||
209-
!endTimeChange.current.isValid()
210-
? null
211-
: snapTime(endTimeChange.current),
212-
);
213-
}
214-
},
215174
},
216175
}}
217176
/>

src/components/Filters.tsx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ export default function Filters(props: Props) {
157157

158158
// for saving the input values on change but only updating them onBlur or onKeyDown+enter
159159
const dateChange = useRef<Dayjs | null>(dayjsDate);
160-
const startTimeChange = useRef<Dayjs | null>(
160+
const [startTimeChange, setStartTimeChange] = useState<Dayjs | null>(
161161
dayjs(date + startTime, 'YYYY-MM-DDHH:mm'),
162162
);
163-
const endTimeChange = useRef<Dayjs | null>(
163+
const [endTimeChange, setEndTimeChange] = useState<Dayjs | null>(
164164
dayjs(date + endTime, 'YYYY-MM-DDHH:mm'),
165165
);
166166
const [minCapacityChange, setMinCapacityChange] = useState(minCapacity ?? '');
@@ -255,8 +255,8 @@ export default function Filters(props: Props) {
255255
timeSteps={{ minutes: 15 }}
256256
label="Start time"
257257
className="w-full"
258-
value={startTime ? dayjs(startTime, 'HH:mm') : null}
259-
onChange={(newValue) => (startTimeChange.current = newValue)}
258+
value={startTimeChange}
259+
onChange={(newValue) => setStartTimeChange(newValue)}
260260
onAccept={setStartTime}
261261
slotProps={{
262262
actionBar: {
@@ -268,19 +268,19 @@ export default function Filters(props: Props) {
268268
helperText: error && 'Start time must be before end time',
269269
onBlur: () => {
270270
setStartTime(
271-
startTimeChange.current == null ||
272-
!startTimeChange.current.isValid()
271+
startTimeChange == null ||
272+
!startTimeChange.isValid()
273273
? null
274-
: startTimeChange.current,
274+
: startTimeChange,
275275
);
276276
},
277277
onKeyDown: (e) => {
278278
if (e.key === 'Enter') {
279279
setStartTime(
280-
startTimeChange.current == null ||
281-
!startTimeChange.current.isValid()
280+
startTimeChange == null ||
281+
!startTimeChange.isValid()
282282
? null
283-
: startTimeChange.current,
283+
: startTimeChange,
284284
);
285285
}
286286
},
@@ -295,8 +295,8 @@ export default function Filters(props: Props) {
295295
timeSteps={{ minutes: 15 }}
296296
label="End time"
297297
className="w-full"
298-
value={endTime ? dayjs(endTime, 'HH:mm') : null}
299-
onChange={(newValue) => (endTimeChange.current = newValue)}
298+
value={endTimeChange}
299+
onChange={(newValue) => setEndTimeChange(newValue)}
300300
onAccept={setEndTime}
301301
slotProps={{
302302
actionBar: {
@@ -308,19 +308,19 @@ export default function Filters(props: Props) {
308308
helperText: error && 'Start time must be before end time',
309309
onBlur: () => {
310310
setEndTime(
311-
endTimeChange.current == null ||
312-
!endTimeChange.current.isValid()
311+
endTimeChange == null ||
312+
!endTimeChange.isValid()
313313
? null
314-
: endTimeChange.current,
314+
: endTimeChange,
315315
);
316316
},
317317
onKeyDown: (e) => {
318318
if (e.key === 'Enter') {
319319
setEndTime(
320-
endTimeChange.current == null ||
321-
!endTimeChange.current.isValid()
320+
endTimeChange == null ||
321+
!endTimeChange.isValid()
322322
? null
323-
: endTimeChange.current,
323+
: endTimeChange,
324324
);
325325
}
326326
},

0 commit comments

Comments
 (0)