Skip to content
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

Merged
merged 4 commits into from
Feb 27, 2024

Conversation

snturk
Copy link
Contributor

@snturk snturk commented Feb 24, 2024

Fixes: #4596

Comment on lines +258 to +271
minTime={
_value && minDate && _value.toDateString() === minDate.toDateString()
? minTime != null
? minTime
: undefined
: undefined
}
maxTime={
_value && maxDate && _value.toDateString() === maxDate.toDateString()
? maxTime != null
? maxTime
: undefined
: undefined
}
Copy link
Contributor Author

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

Comment on lines 61 to 98
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;
}
}
}
}
}
Copy link
Contributor Author

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.

@snturk
Copy link
Contributor Author

snturk commented Feb 25, 2024

@rtivital could you please review?

@rtivital
Copy link
Member

It does not seem to work

2024-02-26.16.04.07.mov

@rtivital
Copy link
Member

  1. Time in DatePickerInput is erased when the dropdown is closed. It is not relevant whether the date was in the range of min/max or outside the range
  2. I'm still allowed to submit a date that is outside the range. Instead, my time input should be clamped between min/max date once either TimeInput loses focus or the dropdown is closed.

@rtivital rtivital added the Invalid Incorrect issue or PR label Feb 26, 2024
@snturk
Copy link
Contributor Author

snturk commented Feb 26, 2024

@rtivital Hi, fixed both tasks and added Min Date & Max Date strings to the component's storybook.

@rtivital rtivital removed the Invalid Incorrect issue or PR label Feb 26, 2024
@snturk snturk force-pushed the fix/4596/min_max_timeinput branch from efbcdaa to 1060800 Compare February 26, 2024 17:21
@rtivital
Copy link
Member

Several other issues:

2024-02-26.21.24.01.mov

@rtivital
Copy link
Member

Time value must be clamped after the dropdown is closed

@snturk
Copy link
Contributor Author

snturk commented Feb 26, 2024

@rtivital Hi, fixed other issues and tested corner cases, also added a new story to test min & max time for time input

@rtivital rtivital merged commit 02d287b into mantinedev:master Feb 27, 2024
1 check passed
@rtivital
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DateTimePicker not respecting minDate for the TimeInput
2 participants