Skip to content

Commit ecbd1ce

Browse files
committed
[*] DatePicker: updates
- Alignment between the days of the week and the Calendar grid - Simple title variant in Calendar demo - Add css for Calendar title - Update time crate version (support PartialOrd for Month) - Fix view day on next/previous button click - Fix DateRangePickerContext (sync data)
1 parent d70652e commit ecbd1ce

File tree

7 files changed

+42
-55
lines changed

7 files changed

+42
-55
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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@
5959
cursor: not-allowed;
6060
}
6161

62+
.calendar-month-title {
63+
display: flex;
64+
width: 100%;
65+
height: 1.75rem;
66+
align-items: center;
67+
justify-content: center;
68+
}
69+
6270
/* Calendar Grid */
6371
.calendar-div {
6472
display: flex;
@@ -77,7 +85,7 @@
7785
}
7886

7987
.calendar-grid-day-header {
80-
width: 2rem;
88+
flex: 1;
8189
color: var(--secondary-color-5);
8290
font-size: 12px;
8391
font-weight: 300;
@@ -271,4 +279,4 @@ td:has(.calendar-grid-cell[data-selection-end="true"]) {
271279
stroke-linecap: round;
272280
stroke-linejoin: round;
273281
stroke-width: 2;
274-
}
282+
}

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: 18 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

@@ -1147,6 +1123,10 @@ pub fn CalendarPreviousMonthButton(props: CalendarPreviousMonthButtonProps) -> E
11471123
let ctx: BaseCalendarContext = use_context();
11481124
let view_ctx: CalendarViewContext = use_context();
11491125

1126+
if view_ctx.offset != 0 {
1127+
return rsx! {};
1128+
}
1129+
11501130
// disable previous button when we reach the limit
11511131
let button_disabled = use_memo(move || {
11521132
// Get the current view date from context
@@ -1168,7 +1148,7 @@ pub fn CalendarPreviousMonthButton(props: CalendarPreviousMonthButtonProps) -> E
11681148
let handle_prev_month = move |e: Event<MouseData>| {
11691149
e.prevent_default();
11701150
let current_view = (ctx.view_date)();
1171-
if let Some(date) = view_ctx.sub_months(current_view) {
1151+
if let Some(date) = previous_month(current_view) {
11721152
ctx.set_view_date.call(date)
11731153
}
11741154
};
@@ -1248,6 +1228,10 @@ pub fn CalendarNextMonthButton(props: CalendarNextMonthButtonProps) -> Element {
12481228
let ctx: BaseCalendarContext = use_context();
12491229
let view_ctx: CalendarViewContext = use_context();
12501230

1231+
if view_ctx.offset + 1 != ctx.month_count {
1232+
return rsx! {};
1233+
}
1234+
12511235
// disable next button when we reach the limit
12521236
let button_disabled = use_memo(move || {
12531237
// Get the current view date from context
@@ -1273,7 +1257,7 @@ pub fn CalendarNextMonthButton(props: CalendarNextMonthButtonProps) -> Element {
12731257
let handle_next_month = move |e: Event<MouseData>| {
12741258
e.prevent_default();
12751259
let current_view = (ctx.view_date)();
1276-
if let Some(date) = view_ctx.add_months(current_view) {
1260+
if let Some(date) = next_month(current_view) {
12771261
ctx.set_view_date.call(date)
12781262
}
12791263
};
@@ -1890,9 +1874,7 @@ fn relative_calendar_month(
18901874
} else if date > base_ctx.enabled_date_range.end {
18911875
RelativeMonth::Next
18921876
} else {
1893-
let lhs = date.month() as u8;
1894-
let rhs = current_month as u8;
1895-
match lhs.cmp(&rhs) {
1877+
match date.month().cmp(&current_month) {
18961878
std::cmp::Ordering::Less => RelativeMonth::Last,
18971879
std::cmp::Ordering::Equal => RelativeMonth::Current,
18981880
std::cmp::Ordering::Greater => RelativeMonth::Next,

primitives/src/date_picker.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,14 @@ pub fn DatePicker(props: DatePickerProps) -> Element {
173173
#[derive(Copy, Clone)]
174174
pub struct DateRangePickerContext {
175175
// Currently selected date range
176-
date_range: Signal<Option<DateRange>>,
176+
date_range: ReadSignal<Option<DateRange>>,
177177
set_selected_range: Callback<Option<DateRange>>,
178178
}
179179

180180
impl DateRangePickerContext {
181181
/// Set the selected date
182182
pub fn set_range(&mut self, range: Option<DateRange>) {
183183
if (self.date_range)() != range {
184-
self.date_range.set(range);
185184
self.set_selected_range.call(range);
186185
}
187186
}
@@ -294,16 +293,15 @@ pub fn DateRangePicker(props: DateRangePickerProps) -> Element {
294293
month_count: props.month_count,
295294
});
296295

297-
let date_range = use_signal(|| (props.selected_range)());
298296
use_context_provider(|| DateRangePickerContext {
299-
date_range,
297+
date_range: props.selected_range,
300298
set_selected_range: props.on_range_change,
301299
});
302300

303301
rsx! {
304302
div {
305303
role: "group",
306-
aria_label: "DateRange",
304+
aria_label: "Date Range",
307305
"data-disabled": (props.disabled)(),
308306
..props.attributes,
309307
{props.children}

0 commit comments

Comments
 (0)