Skip to content

Commit 69ca202

Browse files
committed
[*] DatePicker: updates
- Alignment between the days of the week and the calendar grid - Simple title variant in demo - Fix 'aria_label' - Update time crate version (support PartialOrd for Month) - Fix view day on next/previous button click
1 parent d70652e commit 69ca202

File tree

7 files changed

+25
-50
lines changed

7 files changed

+25
-50
lines changed

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

preview/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dioxus-i18n = { git = "https://github.com/ealmloff/dioxus-i18n", branch = "bump-
1111
unic-langid = { version = "0.9", features = ["macros"] }
1212
strum = { version = "0.27.2", features = ["derive"] }
1313
tracing.workspace = true
14-
time = { version = "0.3.41", features = ["std", "macros"] }
14+
time = { version = "0.3.44", features = ["std", "macros"] }
1515

1616
[build-dependencies]
1717
syntect = "5.2"

preview/src/components/calendar/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
}
7878

7979
.calendar-grid-day-header {
80-
width: 2rem;
80+
flex: 1;
8181
color: var(--secondary-color-5);
8282
font-size: 12px;
8383
font-weight: 300;

preview/src/components/calendar/variants/multi_month/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ pub fn Demo() -> Element {
2727
CalendarHeader {
2828
CalendarNavigation {
2929
CalendarPreviousMonthButton {}
30-
CalendarSelectMonth {}
31-
CalendarSelectYear {}
30+
CalendarMonthTitle {}
3231
CalendarNextMonthButton {}
3332
}
3433
}

primitives/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ repository = "https://github.com/DioxusLabs/components"
1414
[dependencies]
1515
dioxus.workspace = true
1616
dioxus-time = { git = "https://github.com/ealmloff/dioxus-std", branch = "0.7" }
17-
time = { version = "0.3.41", features = ["std", "macros", "parsing"] }
17+
time = { version = "0.3.44", features = ["std", "macros", "parsing"] }
1818
num-integer = "0.1.46"
1919
tracing.workspace = true
2020

primitives/src/calendar.rs

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -154,32 +154,13 @@ fn replace_month(date: Date, month: Month) -> Date {
154154
.expect("invalid or out-of-range date")
155155
}
156156

157-
fn gt(lhs_month: Month, rhs_month: Month) -> bool {
158-
let lhs = lhs_month as u8;
159-
let rhs = rhs_month as u8;
160-
lhs > rhs
161-
}
162-
163-
fn nth_month_prev(date: Date, n: u8) -> Option<Date> {
164-
match n {
165-
0 => Some(date),
166-
n => {
167-
let month = date.month();
168-
let nth_month: Month = month.nth_prev(n);
169-
let year = date.year() - if gt(month, nth_month) { 0 } else { 1 };
170-
let max_day = nth_month.length(year);
171-
Date::from_calendar_date(year, nth_month, date.day().min(max_day)).ok()
172-
}
173-
}
174-
}
175-
176157
fn nth_month_next(date: Date, n: u8) -> Option<Date> {
177158
match n {
178159
0 => Some(date),
179160
n => {
180161
let month = date.month();
181162
let nth_month = month.nth_next(n);
182-
let year = date.year() + if gt(month, nth_month) { 1 } else { 0 };
163+
let year = date.year() + if month > nth_month { 1 } else { 0 };
183164
let max_day = nth_month.length(year);
184165
Date::from_calendar_date(year, nth_month, date.day().min(max_day)).ok()
185166
}
@@ -328,6 +309,7 @@ pub struct BaseCalendarContext {
328309
today: Date,
329310
first_day_of_week: Weekday,
330311
enabled_date_range: DateRange,
312+
month_count: u8
331313
}
332314

333315
impl BaseCalendarContext {
@@ -524,6 +506,7 @@ pub fn Calendar(props: CalendarProps) -> Element {
524506
today: props.today,
525507
first_day_of_week: props.first_day_of_week,
526508
enabled_date_range: DateRange::new(props.min_date, props.max_date),
509+
month_count: props.month_count,
527510
});
528511
// Create Calendar context provider for child components
529512
use_context_provider(|| CalendarContext {
@@ -781,6 +764,7 @@ pub fn RangeCalendar(props: RangeCalendarProps) -> Element {
781764
today: props.today,
782765
first_day_of_week: props.first_day_of_week,
783766
enabled_date_range: DateRange::new(props.min_date, props.max_date),
767+
month_count: props.month_count,
784768
});
785769

786770
// Create RangeCalendar context provider for child components
@@ -890,7 +874,7 @@ struct CalendarViewProps {
890874
pub children: Element,
891875
}
892876

893-
#[derive(Clone, PartialEq)]
877+
#[derive(Copy, Clone, PartialEq)]
894878
struct CalendarViewContext {
895879
offset: u8,
896880
view_date: Signal<Date>,
@@ -902,25 +886,17 @@ impl CalendarViewContext {
902886
self.view_date.set(new_date);
903887
}
904888

905-
fn sub_months(&self, date: Date) -> Option<Date> {
906-
nth_month_prev(date, self.offset.max(1))
907-
}
908-
909-
fn add_months(&self, date: Date) -> Option<Date> {
910-
nth_month_next(date, self.offset.max(1))
911-
}
912-
913889
fn replace_year(&self, date: Date, year: i32) -> Date {
914890
let month = date.month();
915891
let view_month = (self.view_date)().month();
916-
let year = year - if gt(month, view_month) { 1 } else { 0 };
892+
let year = year - if month > view_month { 1 } else { 0 };
917893
date.replace_year(year).unwrap_or(date)
918894
}
919895

920896
fn replace_month(&self, date: Date, month: Month) -> Date {
921897
let view_date = (self.view_date)();
922898
let new_month = month.nth_prev(self.offset);
923-
let year = view_date.year() - if gt(month, view_date.month()) { 1 } else { 0 };
899+
let year = view_date.year() - if month > view_date.month() { 1 } else { 0 };
924900
Date::from_calendar_date(year, new_month, 1).unwrap_or(date)
925901
}
926902

@@ -1168,7 +1144,7 @@ pub fn CalendarPreviousMonthButton(props: CalendarPreviousMonthButtonProps) -> E
11681144
let handle_prev_month = move |e: Event<MouseData>| {
11691145
e.prevent_default();
11701146
let current_view = (ctx.view_date)();
1171-
if let Some(date) = view_ctx.sub_months(current_view) {
1147+
if let Some(date) = previous_month(current_view) {
11721148
ctx.set_view_date.call(date)
11731149
}
11741150
};
@@ -1178,6 +1154,7 @@ pub fn CalendarPreviousMonthButton(props: CalendarPreviousMonthButtonProps) -> E
11781154
class: "calendar-nav-prev",
11791155
aria_label: "Previous month",
11801156
r#type: "button",
1157+
visibility: if view_ctx.offset == 0 { "visible" } else { "hidden" },
11811158
onclick: handle_prev_month,
11821159
disabled: (ctx.disabled)() || button_disabled() || navigate_disabled(),
11831160
..props.attributes,
@@ -1273,7 +1250,7 @@ pub fn CalendarNextMonthButton(props: CalendarNextMonthButtonProps) -> Element {
12731250
let handle_next_month = move |e: Event<MouseData>| {
12741251
e.prevent_default();
12751252
let current_view = (ctx.view_date)();
1276-
if let Some(date) = view_ctx.add_months(current_view) {
1253+
if let Some(date) = next_month(current_view) {
12771254
ctx.set_view_date.call(date)
12781255
}
12791256
};
@@ -1283,6 +1260,7 @@ pub fn CalendarNextMonthButton(props: CalendarNextMonthButtonProps) -> Element {
12831260
class: "calendar-nav-next",
12841261
aria_label: "Next month",
12851262
r#type: "button",
1263+
visibility: if view_ctx.offset + 1 == ctx.month_count { "visible" } else { "hidden" },
12861264
onclick: handle_next_month,
12871265
disabled: (ctx.disabled)() || button_disabled() || navigate_disabled(),
12881266
..props.attributes,
@@ -1890,9 +1868,7 @@ fn relative_calendar_month(
18901868
} else if date > base_ctx.enabled_date_range.end {
18911869
RelativeMonth::Next
18921870
} else {
1893-
let lhs = date.month() as u8;
1894-
let rhs = current_month as u8;
1895-
match lhs.cmp(&rhs) {
1871+
match date.month().cmp(&current_month) {
18961872
std::cmp::Ordering::Less => RelativeMonth::Last,
18971873
std::cmp::Ordering::Equal => RelativeMonth::Current,
18981874
std::cmp::Ordering::Greater => RelativeMonth::Next,

primitives/src/date_picker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ pub fn DateRangePicker(props: DateRangePickerProps) -> Element {
303303
rsx! {
304304
div {
305305
role: "group",
306-
aria_label: "DateRange",
306+
aria_label: "Date Range",
307307
"data-disabled": (props.disabled)(),
308308
..props.attributes,
309309
{props.children}

0 commit comments

Comments
 (0)