New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exclude out of bounds times #1718
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1718 +/- ##
==========================================
- Coverage 90.29% 89.84% -0.45%
==========================================
Files 17 17
Lines 1205 1211 +6
Branches 208 208
==========================================
Hits 1088 1088
- Misses 10 16 +6
Partials 107 107
Continue to review full report at Codecov.
|
ebbe988
to
ab62cf3
Compare
ab62cf3
to
239e47d
Compare
I guess "excludeOutOfBoundsTimes" is not implemented now in the official package. That's why it's not working for me. |
Great, I need this, when can it be merged and released? |
We need to get this merged, it makes so much sense to have it. |
@@ -379,6 +379,16 @@ export function isTimeInDisabledRange(time, { minTime, maxTime }) { | |||
return valid; | |||
} | |||
|
|||
export function isTimeInExcludedRange(time, { minDate, maxDate }) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function could be simplified to
return (isDate(minDate) && getDate(minDate) === getDate(time) && getTime(minDate) > getTime(time))
|| (isDate(maxDate) && getDate(maxDate) === getDate(time) && getTime(maxDate) < getTime(time))
Any updates on this? |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
It would be really useful to have this merged. I can't block times selection for the current day without blocking them for every other day. |
Hi @martijnrusschen , @CruseCtrl , Is there some reason to block this PR beside the only comment made by @CruseCtrl about a small simplification? Thanks |
I don't have any problem with it being merged |
Please merge |
PLEASE merge it... |
Also looking for this feature to enforce min/max datetime. Anything holding this up? @PaulRBerg |
Hi @khiner, I am neither the author nor a maintainer of this library. I implemented the PR because I needed it for my own purposes at the time. @Hacker0x01 is the one to talk to. |
I made it work this way: const [startDate, setStartDate] = useState(new Date());
const isSelectedDateToday = new Date().getDate() === startDate.getDate();
let minTimeHour = new Date().getHours();
if (!isSelectedDateToday) minTimeHour = 0;
return (
<DatePicker
showTimeSelect
selected={startDate}
onChange={date => setStartDate(date)}
minDate={new Date()}
minTime={new Date(new Date().setHours(currentHour, 0, 0, 0))}
maxTime={new Date(new Date().setHours(23, 59, 0, 0))}
placeholderText="Select date and time"
/>
); |
Please merge this... Thanks @ajaxtown for the makeshift solution. Assuming you mean minTimeHour instead of currentHour in the minTime parameter. It will also set the min time as the hour you're currently in, so its still possible to set a time in the past. |
This is extremely helpful, thank you. As mentioned in the comment below, the problem is that if you are within the same hour for example it still shows the time when it shouldn't. So, if it is 17:23 it is still allowing me to select 17:00 which it shouldn't. Any suggestions on how to fix that? |
I have asked ajaxTown if they have a solution for the being able to set a time in the past but also asking you in case you had a solution? |
@ericvanular @Jonofat you can manage the time this way: const [startDate, setStartDate] = useState(new Date());
const isSelectedDateToday = new Date().getDate() === startDate.getDate();
const isSelectedDateInFuture = +startDate > +new Date();
let minTimeHour = new Date().getHours();
if (!isSelectedDateToday) minTimeHour = 0;
const date = new Date();
let currentMins = date.getMinutes();
let currentHour = date.getHours();
if (isSelectedDateInFuture) {
currentHour = 0;
currentMins = 0;
}
return (
<DatePicker
showTimeSelect
selected={startDate}
onChange={date => setStartDate(date)}
minDate={new Date()}
minTime={new Date(new Date().setHours(currentHour, currentMins, 0, 0))}
maxTime={new Date(new Date().setHours(23, 59, 0, 0))}
placeholderText="Select date and time"
/>
); Edit: @Jonofat pointed out that this would not work for the future date. Because the time is still being treat as if its today. I have changed the solution to tackle that. |
@ajaxtown, thank you! This looks pretty good and is working for the current date. However if I choose tomorrow or any day in the future it still limits the time to what it is now eg: If it is 3:30pm it shows from 4pm. So I can't pick anything before 3:00pm for tomorrow which I should be able to. I have had to change my code unfortunately before you posted your answer:
|
@Jonofat ah yes, I missed that one out. Thanks for testing and letting me know. I have edited my answer to tackle that scenario as well. |
@ajaxtown, thanks once again! I have edited my code above with your changes but it seems to still be doing the same thing ie: issue not resolved. I am trying to troubleshoot... EDIT: Okay, found my problem. Now seems to work really nicely :) Thanks so much! |
@ajaxtown Ah, I spoke too soon! One last issue I think. If I have my max time is 5pm (ie: I should not be able to choose a time past 5pm on any given day ) and it is now 5:37 pm, it should show no times for today but it is showing everything from 00:00 to 23:00. It was working 100% when it was before 5pm today. |
@Jonofat can this be a timezone issue ? |
@ajaxtown, I don't think it is a time zone issue ( I am working on my localhost). I have only allowed times between 8am and 5pm on Monday - Friday and 8am - 1pm on Saturday. But it is showing times for a whole 24 hours and ignoring my allowed times so I think it might be something else?
|
Description
Implements #1135. Use it by setting the
excludeOutOfBoundsTimes
prop on theDatePicker
component. Example:How it works:
minDate
or amaxDate
, all times before that date's time will be deselected (if the active date matches theminDate
ormaxDate
)minDate
ormaxDate
, that time will not be highlighted. I accomplished this by going to therenderTimes
function of theTime
component and change thebase
to beactiveTime
instead ofnew Date()
- I hope there won't be any unexpected side effects.Caveat: I didn't have time to add tests, but I added examples in the documentation site.
Result
Min Date
Max Date