Skip to content

Commit aba0045

Browse files
committed
[*] DatePicker: implement new functionality
- Add popover with calendar - Pass Calendar as child - Correct input validation - Split from preview - Fix add Calendar empty cells - Localization
1 parent 4d11d3b commit aba0045

File tree

10 files changed

+910
-810
lines changed

10 files changed

+910
-810
lines changed

Cargo.lock

Lines changed: 555 additions & 531 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use crate::components::calendar::component::*;
2+
use dioxus::prelude::*;
3+
use dioxus_primitives::calendar::CalendarProps;
4+
use dioxus_primitives::date_picker::{
5+
self, DatePickerInputProps, DatePickerProps, DatePickerTriggerProps,
6+
};
7+
8+
use time::UtcDateTime;
9+
10+
#[component]
11+
pub fn DatePicker(props: DatePickerProps) -> Element {
12+
rsx! {
13+
document::Link {
14+
rel: "stylesheet",
15+
href: asset!("/src/components/date_picker/style.css"),
16+
}
17+
div {
18+
date_picker::DatePicker {
19+
class: "date-picker",
20+
value: props.value,
21+
on_value_change: props.on_value_change,
22+
selected_date: props.selected_date,
23+
disabled: props.disabled,
24+
read_only: props.read_only,
25+
separator: props.separator,
26+
on_format_placeholder: props.on_format_placeholder,
27+
attributes: props.attributes,
28+
{props.children}
29+
}
30+
}
31+
}
32+
}
33+
34+
#[component]
35+
pub fn DatePickerInput(props: DatePickerInputProps) -> Element {
36+
rsx! {
37+
date_picker::DatePickerInput {
38+
class: "date-picker-input",
39+
attributes: props.attributes,
40+
{props.children}
41+
}
42+
}
43+
}
44+
45+
#[component]
46+
pub fn DatePickerTrigger(props: DatePickerTriggerProps) -> Element {
47+
rsx! {
48+
date_picker::DatePickerTrigger {
49+
class: "date-picker-trigger",
50+
attributes: props.attributes,
51+
{props.children}
52+
}
53+
}
54+
}
55+
56+
#[component]
57+
pub fn DatePickerCalendar(props: CalendarProps) -> Element {
58+
let mut view_date = use_signal(|| UtcDateTime::now().date());
59+
60+
rsx! {
61+
Calendar {
62+
selected_date: props.selected_date,
63+
on_date_change: props.on_date_change,
64+
on_format_weekday: props.on_format_weekday,
65+
on_format_month: props.on_format_month,
66+
view_date: view_date(),
67+
today: props.today,
68+
on_view_change: move |date| view_date.set(date),
69+
disabled: props.disabled,
70+
first_day_of_week: props.first_day_of_week,
71+
min_date: props.min_date,
72+
max_date: props.max_date,
73+
attributes: props.attributes,
74+
CalendarHeader {
75+
CalendarNavigation {
76+
CalendarPreviousMonthButton {}
77+
CalendarSelectMonth {}
78+
CalendarSelectYear {}
79+
CalendarNextMonthButton {}
80+
}
81+
}
82+
CalendarGrid {}
83+
}
84+
}
85+
}

preview/src/components/date_picker/variants/main/style.css renamed to preview/src/components/date_picker/style.css

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,16 @@
2222
box-shadow: inset 0 0 0 1px var(--light, var(--primary-color-6))
2323
var(--dark, var(--primary-color-7));
2424
color: var(--secondary-color-4);
25-
cursor: text;
2625
gap: 0.25rem;
2726
transition: background-color 100ms ease-out;
2827
}
2928

3029
.date-picker-trigger {
31-
position: absolute;
32-
right: 0;
3330
border: none;
3431
background-color: transparent;
3532
cursor: pointer;
36-
padding: 0.25rem;
37-
padding: 8px 12px;
33+
position: relative;
34+
margin-left: -35px;
3835
}
3936

4037
.date-picker-input input::placeholder {

preview/src/components/date_picker/variants/main/mod.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
1+
use super::super::component::*;
12
use dioxus::prelude::*;
2-
use dioxus_primitives::date_picker::{
3-
DatePicker, DatePickerCalendar, DatePickerInput, DatePickerTrigger, DatePickerValue,
4-
};
3+
4+
use dioxus_i18n::tid;
5+
use dioxus_primitives::date_picker::DatePickerValue;
6+
7+
use time::{Date, Month, Weekday};
58

69
#[component]
710
pub fn Demo() -> Element {
8-
let mut value = use_signal(|| None::<DatePickerValue>);
11+
let v = DatePickerValue::range_none();
12+
let mut value = use_signal(|| v);
13+
14+
let mut selected_date = use_signal(|| None::<Date>);
915

1016
rsx! {
11-
document::Link {
12-
rel: "stylesheet",
13-
href: asset!("/src/components/date_picker/variants/main/style.css"),
14-
}
1517
div {
1618
DatePicker {
17-
class: "date-picker",
1819
value: value(),
19-
on_value_change: move |date| {
20-
tracing::info!("Selected date: {:?}", date);
21-
value.set(date);
20+
selected_date: selected_date(),
21+
on_value_change: move |v| {
22+
tracing::info!("Selected: {v}");
23+
value.set(v);
2224
},
25+
on_format_placeholder: || tid!("YMD"),
26+
read_only: true,
2327
DatePickerInput {
24-
class: "date-picker-input",
2528
DatePickerTrigger {
26-
class: "date-picker-trigger",
27-
aria_label: "DatePicker Trigger",
28-
svg {
29-
class: "date-picker-expand-icon",
30-
view_box: "0 0 24 24",
31-
xmlns: "http://www.w3.org/2000/svg",
32-
polyline { points: "6 9 12 15 18 9" }
29+
aria_label: "DatePicker Trigger",
30+
DatePickerCalendar {
31+
selected_date: selected_date(),
32+
on_date_change: move |date| selected_date.set(date),
33+
on_format_weekday: |weekday: Weekday| tid!(&weekday.to_string()),
34+
on_format_month: |month: Month| tid!(&month.to_string()),
35+
}
3336
}
3437
}
35-
}
36-
DatePickerCalendar { class: "date-picker-calendar" }
3738
}
3839
}
3940
}

preview/src/i18n/de-DE.ftl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ Wednesday = Mi
1919
Thursday = Do
2020
Friday = Fr
2121
Saturday = Sa
22-
Sunday = So
22+
Sunday = So
23+
24+
## Date
25+
YMD = JJJJ-MM-TT

preview/src/i18n/en-US.ftl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ Wednesday = We
1919
Thursday = Th
2020
Friday = Fr
2121
Saturday = Sa
22-
Sunday = Su
22+
Sunday = Su
23+
24+
## Date
25+
YMD = YYYY-MM-DD

preview/src/i18n/es-ES.ftl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ Wednesday = Mi
1919
Thursday = Ju
2020
Friday = Vi
2121
Saturday = Sa
22-
Sunday = Do
22+
Sunday = Do
23+
24+
## Date
25+
YMD = YYYY-MM-DD

preview/src/i18n/fr-FR.ftl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ Wednesday = Me
1919
Thursday = Je
2020
Friday = Ve
2121
Saturday = Sa
22-
Sunday = Di
22+
Sunday = Di
23+
24+
## Date
25+
YMD = AAAA-MM-JJ

primitives/src/calendar.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ pub struct CalendarProps {
230230
pub on_format_month: Callback<Month, String>,
231231

232232
/// The month being viewed
233+
#[props(default = ReadOnlySignal::new(Signal::new(UtcDateTime::now().date())))]
233234
pub view_date: ReadOnlySignal<Date>,
234235

235236
/// The current date (used for highlighting today)
@@ -896,24 +897,22 @@ pub fn CalendarGrid(props: CalendarGridProps) -> Element {
896897
date = date.next_day().expect("invalid or out-of-range date");
897898
}
898899

900+
let mut date = view_date;
899901
// Add days of the month
900902
let num_days_in_month = view_date.month().length(view_date.year());
901903
for day in 1..=num_days_in_month {
902-
grid.push(
903-
view_date
904-
.replace_day(day)
905-
.expect("invalid or out-of-range date"),
906-
);
904+
date = view_date
905+
.replace_day(day)
906+
.expect("invalid or out-of-range date");
907+
grid.push(date);
907908
}
908909

909910
// Add empty cells to complete the grid (for a clean layout)
910911
let remainder = grid.len() % 7;
911912
if remainder > 0 {
912-
if let Some(mut date) = next_month(view_date) {
913-
for _ in 1..=(7 - remainder) {
914-
grid.push(date);
915-
date = date.next_day().expect("invalid or out-of-range date");
916-
}
913+
for _ in 1..=(7 - remainder) {
914+
date = date.next_day().expect("invalid or out-of-range date");
915+
grid.push(date);
917916
}
918917
}
919918

0 commit comments

Comments
 (0)