next_rust_ui/
date_picker.rs1use next_rust_view::{Element, Node, View};
4
5use crate::icons::{self, icon};
6use crate::input::{Field, Frame, field_methods, icon_button};
7use crate::{apply, attr, flag};
8
9#[derive(Default)]
19pub struct DatePicker {
20 field: Field,
21 min: Option<String>,
22 max: Option<String>,
23 first_day: Option<u8>,
24 locale: Option<String>,
25}
26
27impl DatePicker {
28 pub fn new() -> Self {
29 Self::default()
30 }
31 field_methods!();
32
33 pub fn with(mut self, part: impl next_rust_view::Part) -> Self {
35 self.field.extra.push(part);
36 self
37 }
38 pub fn min(mut self, v: impl Into<String>) -> Self {
40 self.min = Some(v.into());
41 self
42 }
43 pub fn max(mut self, v: impl Into<String>) -> Self {
45 self.max = Some(v.into());
46 self
47 }
48 pub fn first_day_of_week(mut self, v: u8) -> Self {
50 self.first_day = Some(v % 7);
51 self
52 }
53 pub fn locale(mut self, v: impl Into<String>) -> Self {
55 self.locale = Some(v.into());
56 self
57 }
58}
59
60impl View for DatePicker {
61 fn into_node(mut self) -> Node {
62 let (class, attrs, _) = std::mem::take(&mut self.field.extra).split();
63 if let Some(class) = class {
64 self.field.extra.push(class);
65 }
66 let floats = self.field.placeholder.is_none();
67 let mut input = Element::new_void("input")
68 .with(attr(
69 "class",
70 match &self.field.input_class {
71 Some(c) => format!("nr-field-input nr-datepicker-input {c}"),
72 None => "nr-field-input nr-datepicker-input".to_owned(),
73 },
74 ))
75 .with(attr("type", "date"))
76 .with(attr("placeholder", self.field.placeholder.clone().unwrap_or_else(|| " ".into())));
77 apply(&mut input, self.field.control_attrs());
78 if let Some(v) = &self.field.value {
79 input.set_attr(attr("value", v.clone()));
80 }
81 if let Some(v) = &self.min {
82 input.set_attr(attr("min", v.clone()));
83 }
84 if let Some(v) = &self.max {
85 input.set_attr(attr("max", v.clone()));
86 }
87 input.set_attr(flag("readonly", self.field.readonly));
88 apply(&mut input, attrs);
89
90 let mut root_attrs = vec![attr("data-nr-ui", "datepicker")];
91 if let Some(d) = self.first_day {
92 root_attrs.push(attr("data-first-day", d.to_string()));
93 }
94 if let Some(l) = &self.locale {
95 root_attrs.push(attr("data-locale", l.clone()));
96 }
97 let open = icon_button("nr-datepicker-toggle", "Choose date", "open", icon(icons::calendar()));
98 let calendar = Element::new("div")
99 .with(attr("class", "nr-calendar"))
100 .with(attr("role", "dialog"))
101 .with(attr("aria-label", "Choose date"))
102 .with(flag("hidden", true))
103 .into_node();
104 self.field.render(
105 input.into_node(),
106 Frame { kind: "nr-datepicker", floats, after_box: vec![calendar], root_attrs, end: vec![open] },
107 )
108 }
109}