Updated TimePicker to disable future times and allow only current or past selections

This commit is contained in:
Pramod Mahajan 2025-04-29 00:51:31 +05:30
parent 5b88c86df5
commit 054ef8fd17

View File

@ -27,17 +27,38 @@ const TimePicker = ({ label, onChange, interval = 10, value }) => {
return `${hoursFormatted}:${minutesFormatted} ${period}`; return `${hoursFormatted}:${minutesFormatted} ${period}`;
}; };
// all slots
// const generateTimeSlots = () => {
// const slots = [];
// let currentTime = new Date(getCurrentTime());
// for (let i = 0; i < 144; i++) { // 24 hours with custom interval
// slots.push(formatTime(currentTime));
// currentTime.setMinutes(currentTime.getMinutes() + interval);
// }
// return slots;
// };
// imit slots to current time:
const generateTimeSlots = () => { const generateTimeSlots = () => {
const slots = []; const slots = [];
let currentTime = new Date(getCurrentTime()); let currentTime = new Date();
for (let i = 0; i < 144; i++) { // 24 hours with custom interval currentTime.setSeconds(0);
slots.push(formatTime(currentTime)); currentTime.setMilliseconds(0);
currentTime.setMinutes(currentTime.getMinutes() + interval);
const endTime = new Date(currentTime); // current time as upper limit
let slotTime = new Date();
slotTime.setHours(0, 0, 0, 0); // start from 00:00
while (slotTime <= endTime) {
slots.push(formatTime(new Date(slotTime)));
slotTime.setMinutes(slotTime.getMinutes() + interval);
} }
return slots; return slots;
}; };
const handleSelect = (selectedTime) => { const handleSelect = (selectedTime) => {
setTime(selectedTime); setTime(selectedTime);
if (onChange) onChange(selectedTime); if (onChange) onChange(selectedTime);