WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit dcb1c77

Browse files
committed
[*] DatePicker: fix arrow navigation
1 parent ecbd1ce commit dcb1c77

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

primitives/src/calendar.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,18 @@ pub fn Calendar(props: CalendarProps) -> Element {
525525
return;
526526
};
527527
let mut set_focused_date = |new_date: Option<Date>| {
528-
let mut view_date = (base_ctx.view_date)();
529528
if let Some(date) = new_date {
530-
if date.month() != view_date.month() {
531-
view_date = date.replace_day(1).unwrap();
529+
let min_date = (base_ctx.view_date)().replace_day(1).unwrap();
530+
if date < min_date {
531+
let view_date = previous_month(min_date).unwrap_or(min_date);
532532
(base_ctx.set_view_date)(view_date);
533+
} else {
534+
let max_date = nth_month_next(min_date, props.month_count)
535+
.unwrap_or(min_date);
536+
if date >= max_date {
537+
let view_date = next_month(min_date).unwrap_or(min_date);
538+
(base_ctx.set_view_date)(view_date);
539+
}
533540
}
534541
}
535542
match new_date {
@@ -574,12 +581,8 @@ pub fn Calendar(props: CalendarProps) -> Element {
574581
}
575582
},
576583
for offset in 0..props.month_count {
577-
CalendarView {
578-
offset,
579-
div {
580-
..props.attributes.clone(),
581-
{props.children.clone()}
582-
}
584+
CalendarView { offset,
585+
div { ..props.attributes.clone(),{props.children.clone()} }
583586
}
584587
}
585588
}
@@ -795,11 +798,18 @@ pub fn RangeCalendar(props: RangeCalendarProps) -> Element {
795798
}
796799
}
797800
let mut set_focused_date = |new_date: Option<Date>| {
798-
let mut view_date = (base_ctx.view_date)();
799801
if let Some(date) = new_date {
800-
if date.month() != view_date.month() {
801-
view_date = date.replace_day(1).unwrap();
802+
let min_date = (base_ctx.view_date)().replace_day(1).unwrap();
803+
if date < min_date {
804+
let view_date = previous_month(min_date).unwrap_or(min_date);
802805
(base_ctx.set_view_date)(view_date);
806+
} else {
807+
let max_date = nth_month_next(min_date, props.month_count)
808+
.unwrap_or(min_date);
809+
if date >= max_date {
810+
let view_date = next_month(min_date).unwrap_or(min_date);
811+
(base_ctx.set_view_date)(view_date);
812+
}
803813
}
804814
}
805815
match new_date {
@@ -852,12 +862,8 @@ pub fn RangeCalendar(props: RangeCalendarProps) -> Element {
852862
}
853863
},
854864
for offset in 0..props.month_count {
855-
CalendarView {
856-
offset,
857-
div {
858-
..props.attributes.clone(),
859-
{props.children.clone()}
860-
}
865+
CalendarView { offset,
866+
div { ..props.attributes.clone(),{props.children.clone()} }
861867
}
862868
}
863869
}
@@ -1495,7 +1501,7 @@ pub fn CalendarGrid(props: CalendarGridProps) -> Element {
14951501
// Day name headers
14961502
for (weekday, label) in weekday_headers() {
14971503
th {
1498-
key: "{weekday:?}", // Add key for efficient diffing
1504+
key: "{weekday:?}", // Add key for efficient diffing
14991505
class: "calendar-grid-day-header",
15001506
{label}
15011507
}
@@ -1919,7 +1925,7 @@ fn SingleCalendarDay(props: CalendarDayProps) -> Element {
19191925
let view_date = (view_ctx.view_date)();
19201926
let month = relative_calendar_month(date, &base_ctx, view_date.month());
19211927
let in_current_month = month.current_month();
1922-
let is_focused = move || base_ctx.is_focused(date);
1928+
let is_focused = move || in_current_month && base_ctx.is_focused(date);
19231929
let is_today = date == base_ctx.today;
19241930
let is_unavailable = base_ctx.is_unavailable(date);
19251931

@@ -1993,7 +1999,7 @@ fn RangeCalendarDay(props: CalendarDayProps) -> Element {
19931999
let view_date = (view_ctx.view_date)();
19942000
let month = relative_calendar_month(date, &base_ctx, view_date.month());
19952001
let in_current_month = month.current_month();
1996-
let is_focused = move || base_ctx.is_focused(date);
2002+
let is_focused = move || in_current_month && base_ctx.is_focused(date);
19972003
let is_today = date == base_ctx.today;
19982004
let is_unavailable = base_ctx.is_unavailable(date);
19992005

0 commit comments

Comments
 (0)