Skip to content

Commit 87523de

Browse files
authored
fix(time-picker): period switching on hour change from keyboard navigation (#194)
* fix(time-picker): preserve period when navigating hour column - Fixed bug where PM/AM period would switch to AM when navigating hours with arrow keys or clicking hours in dropdown - Period now properly derives from existing 24-hour value instead of always defaulting to AM - When no value exists, defaults to current time's period for better UX - Matches native HTML time input behavior * chore: rebuild registry
1 parent 75ced68 commit 87523de

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

docs/public/r/styles/default/time-picker.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

docs/registry/default/ui/time-picker.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,11 @@ function TimePickerInput(props: TimePickerInputProps) {
718718
case "second":
719719
if (timeValue.second === undefined) return segmentPlaceholder.second;
720720
return timeValue.second.toString().padStart(2, "0");
721-
case "period":
721+
case "period": {
722722
if (!timeValue || timeValue.hour === undefined)
723723
return segmentPlaceholder.period;
724724
return to12Hour(timeValue.hour).period;
725+
}
725726
default:
726727
return "";
727728
}
@@ -755,8 +756,17 @@ function TimePickerInput(props: TimePickerInputProps) {
755756
if (!Number.isNaN(displayHour)) {
756757
if (is12Hour) {
757758
const clampedHour = clamp(displayHour, 1, 12);
758-
const currentPeriod = timeValue?.period || "AM";
759-
newTime.hour = to24Hour(clampedHour, currentPeriod);
759+
let currentPeriod: Period;
760+
if (timeValue?.period !== undefined) {
761+
currentPeriod = timeValue.period;
762+
} else if (timeValue?.hour !== undefined) {
763+
currentPeriod = to12Hour(timeValue.hour).period;
764+
} else {
765+
const now = new Date();
766+
currentPeriod = to12Hour(now.getHours()).period;
767+
}
768+
const hour24 = to24Hour(clampedHour, currentPeriod);
769+
newTime.hour = hour24;
760770
if (timeValue?.period !== undefined) {
761771
newTime.period = timeValue.period;
762772
}
@@ -1711,7 +1721,14 @@ function TimePickerHour(props: TimePickerHourProps) {
17111721

17121722
let hour24 = displayHour;
17131723
if (is12Hour) {
1714-
const currentPeriod = timeValue?.period || "AM";
1724+
let currentPeriod: Period;
1725+
if (timeValue?.period !== undefined) {
1726+
currentPeriod = timeValue.period;
1727+
} else if (timeValue?.hour !== undefined) {
1728+
currentPeriod = to12Hour(timeValue.hour).period;
1729+
} else {
1730+
currentPeriod = to12Hour(now.getHours()).period;
1731+
}
17151732
hour24 = to24Hour(displayHour, currentPeriod);
17161733
}
17171734

0 commit comments

Comments
 (0)