1use hypersig::html::{Bindable, El, attr, input};
4use hypersig::style::value::rem;
5use hypersig::{CalendarDate, Form, IntoExpr, Sig, Validate};
6
7use crate::field::{Frame, field_style};
8
9#[diagnostic::on_unimplemented(
14 message = "a date field cannot be bound to a `{Self}` signal",
15 note = "bind a `CalendarDate` or `Option<CalendarDate>` signal; text goes in a `TextField`"
16)]
17pub trait Dated: Bindable + sealed::Sealed {}
18
19impl Dated for CalendarDate {}
20impl Dated for Option<CalendarDate> {}
21
22mod sealed {
23 use hypersig::CalendarDate;
24
25 pub trait Sealed {}
26 impl Sealed for CalendarDate {}
27 impl Sealed for Option<CalendarDate> {}
28}
29
30#[derive(Debug)]
58pub struct DateField<S, T> {
59 value: Sig<S, T>,
60 min: Option<CalendarDate>,
61 max: Option<CalendarDate>,
62 frame: Frame<S>,
63}
64
65impl<S, T: Dated> DateField<S, T> {
66 #[must_use]
68 pub fn new(label: impl Into<String>, value: Sig<S, T>) -> Self {
69 Self {
70 value,
71 min: None,
72 max: None,
73 frame: Frame::new(label.into()),
74 }
75 }
76
77 #[must_use]
83 pub fn in_form<V: Validate>(
84 label: impl Into<String>,
85 form: Sig<S, Form<V>>,
86 field: Sig<V, T>,
87 ) -> Self {
88 let mut out = Self::new(label, form.field(field));
89 out.frame = out.frame.in_form(form, field);
90 out
91 }
92
93 #[must_use]
99 pub fn min(mut self, min: CalendarDate) -> Self {
100 if let Some(max) = self.max {
101 assert!(
102 min <= max,
103 "a date field's earliest date is not after its latest, but {min} is after {max}"
104 );
105 }
106 self.min = Some(min);
107 self
108 }
109
110 #[must_use]
116 pub fn max(mut self, max: CalendarDate) -> Self {
117 if let Some(min) = self.min {
118 assert!(
119 min <= max,
120 "a date field's latest date is not before its earliest, but {max} is before {min}"
121 );
122 }
123 self.max = Some(max);
124 self
125 }
126
127 #[must_use]
129 pub fn description(mut self, text: impl Into<String>) -> Self {
130 self.frame.description = Some(text.into());
131 self
132 }
133
134 #[must_use]
138 pub fn error(mut self, message: impl IntoExpr<S, String>) -> Self {
139 self.frame.error = Some(message.into_expr());
140 self
141 }
142
143 #[must_use]
147 pub fn required(mut self) -> Self {
148 self.frame.required = true;
149 self
150 }
151
152 #[must_use]
154 pub fn disabled(mut self, when: impl IntoExpr<S, bool>) -> Self {
155 self.frame.disabled = Some(when.into_expr());
156 self
157 }
158
159 #[must_use]
161 pub fn render(self) -> El<S> {
162 let mut control = input()
163 .attr(attr::TYPE, "date")
164 .css(field_style().height(rem(2.25)))
165 .bind(self.value);
166 if let Some(min) = self.min {
167 control = control.attr(attr::MIN, min.to_string());
168 }
169 if let Some(max) = self.max {
170 control = control.attr(attr::MAX, max.to_string());
171 }
172 self.frame.render(&crate::id::base(self.value), control)
173 }
174}
175
176impl<S, T: Dated> From<DateField<S, T>> for El<S> {
177 fn from(field: DateField<S, T>) -> Self {
178 field.render()
179 }
180}
181
182impl<S, T: Dated> From<DateField<S, T>> for hypersig::html::Node<S> {
183 fn from(field: DateField<S, T>) -> Self {
184 field.render().into()
185 }
186}