Skip to content

Commit 29c6a40

Browse files
committed
[*] Calendar: Add min/max date property
- Move select month and year to Calendar - Dynamic select month/year data
1 parent 1772a7a commit 29c6a40

File tree

2 files changed

+323
-97
lines changed

2 files changed

+323
-97
lines changed
Lines changed: 7 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use dioxus::prelude::*;
22
use dioxus_i18n::tid;
33
use dioxus_primitives::calendar::{
4-
Calendar, CalendarContext, CalendarGrid, CalendarHeader, CalendarNavigation,
5-
CalendarNextMonthButton, CalendarPreviousMonthButton,
4+
Calendar, CalendarGrid, CalendarHeader, CalendarNavigation, CalendarNextMonthButton,
5+
CalendarPreviousMonthButton, CalendarSelectMonth, CalendarSelectYear,
66
};
77

88
use chrono::{Datelike, Month, NaiveDate, Utc, Weekday};
@@ -30,6 +30,9 @@ pub fn Demo() -> Element {
3030
view_date.set(new_view);
3131
},
3232
on_format_weekday: Callback::new(|weekday: Weekday| tid!(&weekday.to_string())),
33+
on_format_month: Callback::new(|month: Month| tid!(month.name())),
34+
min_date: NaiveDate::from_ymd_opt(1995, 7, 21).unwrap(),
35+
max_date: NaiveDate::from_ymd_opt(2035, 9, 11).unwrap(),
3336
CalendarHeader {
3437
CalendarNavigation {
3538
CalendarPreviousMonthButton {
@@ -40,7 +43,8 @@ pub fn Demo() -> Element {
4043
polyline { points: "15 6 9 12 15 18" }
4144
}
4245
}
43-
MonthTitle {}
46+
CalendarSelectMonth {}
47+
CalendarSelectYear {}
4448
CalendarNextMonthButton {
4549
svg {
4650
class: "calendar-next-month-icon",
@@ -56,73 +60,4 @@ pub fn Demo() -> Element {
5660
}
5761
}
5862
}
59-
}
60-
61-
#[component]
62-
fn MonthTitle() -> Element {
63-
let calendar: CalendarContext = use_context();
64-
let view_date = calendar.view_date();
65-
let month_name = Month::try_from(view_date.month() as u8).unwrap().name();
66-
let year = view_date.year();
67-
let months = (1..=12).map(|month_i| Month::try_from(month_i).unwrap());
68-
69-
rsx! {
70-
span { class: "calendar-month-select-container",
71-
select {
72-
class: "calendar-month-select",
73-
aria_label: "Month",
74-
onchange: move |e| {
75-
let mut view_date = calendar.view_date();
76-
let cur_month = e.value().parse().unwrap_or(view_date.month0());
77-
view_date = view_date.with_month0(cur_month).unwrap_or(view_date);
78-
calendar.set_view_date(view_date);
79-
},
80-
for (i , month) in months.enumerate() {
81-
option {
82-
value: i,
83-
selected: calendar.view_date().month0() == i as u32,
84-
{tid!(month.name())}
85-
}
86-
}
87-
}
88-
span { class: "calendar-month-select-value",
89-
{tid!(month_name)}
90-
svg {
91-
class: "select-expand-icon",
92-
view_box: "0 0 24 24",
93-
xmlns: "http://www.w3.org/2000/svg",
94-
polyline { points: "6 9 12 15 18 9" }
95-
}
96-
}
97-
}
98-
99-
span { class: "calendar-year-select-container",
100-
select {
101-
class: "calendar-year-select",
102-
aria_label: "Year",
103-
onchange: move |e| {
104-
let mut view_date = calendar.view_date();
105-
let year = e.value().parse().unwrap_or(view_date.year());
106-
view_date = view_date.with_year(year).unwrap_or(view_date);
107-
calendar.set_view_date(view_date);
108-
},
109-
for year in 1925..=2050 {
110-
option {
111-
value: year,
112-
selected: calendar.view_date().year() == year,
113-
"{year}"
114-
}
115-
}
116-
}
117-
span { class: "calendar-year-select-value",
118-
"{year}"
119-
svg {
120-
class: "select-expand-icon",
121-
view_box: "0 0 24 24",
122-
xmlns: "http://www.w3.org/2000/svg",
123-
polyline { points: "6 9 12 15 18 9" }
124-
}
125-
}
126-
}
127-
}
12863
}

0 commit comments

Comments
 (0)