-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Add minTime and maxTime support to TimeInput.tsx
#5819
Add minTime and maxTime support to TimeInput.tsx
#5819
Conversation
minTime={ | ||
_value && minDate && _value.toDateString() === minDate.toDateString() | ||
? minTime != null | ||
? minTime | ||
: undefined | ||
: undefined | ||
} | ||
maxTime={ | ||
_value && maxDate && _value.toDateString() === maxDate.toDateString() | ||
? maxTime != null | ||
? maxTime | ||
: undefined | ||
: undefined | ||
} |
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.
Open to ideas to write more readable
const onTimeChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
if (minTime !== undefined && maxTime !== undefined) { | ||
const val = event.currentTarget.value; | ||
|
||
if (val) { | ||
const [hours, minutes, seconds] = val.split(':').map(Number); | ||
const isValid = | ||
(hours.toString().length === 2 || hours === 0) && | ||
(minutes.toString().length === 2 || minutes === 0) && | ||
(withSeconds ? seconds.toString().length === 2 || seconds === 0 : true); | ||
|
||
if (isValid) { | ||
if (minTime) { | ||
const [minHours, minMinutes, minSeconds] = minTime.split(':').map(Number); | ||
|
||
if ( | ||
hours < minHours || | ||
(hours === minHours && minutes < minMinutes) || | ||
(withSeconds && hours === minHours && minutes === minMinutes && seconds < minSeconds) | ||
) { | ||
event.currentTarget.value = minTime; | ||
} | ||
} | ||
|
||
if (maxTime) { | ||
const [maxHours, maxMinutes, maxSeconds] = maxTime.split(':').map(Number); | ||
|
||
if ( | ||
hours > maxHours || | ||
(hours === maxHours && minutes > maxMinutes) || | ||
(withSeconds && hours === maxHours && minutes === maxMinutes && seconds > maxSeconds) | ||
) { | ||
event.currentTarget.value = maxTime; | ||
} | ||
} | ||
} | ||
} | ||
} |
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.
I'm not 100% sure this is easily readable, the method can be split into small functions if requested.
@rtivital could you please review? |
It does not seem to work 2024-02-26.16.04.07.mov |
|
@rtivital Hi, fixed both tasks and added Min Date & Max Date strings to the component's storybook. |
efbcdaa
to
1060800
Compare
Several other issues: 2024-02-26.21.24.01.mov |
Time value must be clamped after the dropdown is closed |
@rtivital Hi, fixed other issues and tested corner cases, also added a new story to test min & max time for time input |
Thanks! |
Fixes: #4596