1mod availability;
5mod builders;
6mod check;
7mod choice;
8mod chrome;
9pub(crate) mod custom;
10pub(crate) mod file;
11mod placement;
12mod read_only;
13mod render;
14#[cfg(feature = "rich-text")]
15mod rich_text;
16mod text;
17
18use std::sync::Arc;
19
20pub(crate) use availability::{Disabled, Hidden};
21pub use builders::{
22 ChoiceField, CustomField, FileField, IntoFormValue, IntoOptions, RepeaterField, TextField,
23};
24pub(crate) use check::Items;
25pub(crate) use choice::{ChoiceControl, Relationship, option_view};
26pub(crate) use chrome::{FieldChrome, error_kind, render_field, render_inline_field};
27pub(crate) use custom::{Control, ControlInput};
28pub use custom::{Switch, Toggle};
29pub(crate) use file::{FileControl, is_linkable, is_rooted, stored_upload};
30pub(crate) use placement::{Placement, Posting};
31pub(crate) use read_only::{read_only, value_cell};
32#[cfg(feature = "rich-text")]
33use rich_text::RichTextControl;
34#[cfg(feature = "rich-text")]
35pub use rich_text::RichTextField;
36pub(crate) use text::{Kind, Pattern, TextControl, password_reveal};
37use toasty::stmt::{List, Path};
38use topcoat::context::Cx;
39
40use super::{condition::Condition, layouts::Span, lenses::Binding, repeater::RepeaterControl};
41use crate::{
42 Text,
43 schema::FormInput,
44 values::{FieldError, FormScalar, FormValue},
45};
46
47pub struct Field {
98 binding: Binding,
99 label: Option<Text>,
101 required: bool,
104 control: ControlKind,
105 condition: Option<Condition>,
107 span: Option<Span>,
109 checkbox: bool,
111 accepted: bool,
113 help: Option<Text>,
115 disabled: Disabled,
117 disabling: Option<Condition>,
119 hidden: Hidden,
122 default: Option<FormValue>,
124}
125
126pub(crate) enum ControlKind {
128 Text(TextControl),
130 #[cfg(feature = "rich-text")]
132 RichText(RichTextControl),
133 Choice(ChoiceControl),
135 File(FileControl),
137 Custom(Arc<dyn Control>),
139 Repeater(RepeaterControl),
141}
142
143impl std::fmt::Debug for Field {
144 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
145 let control = match &self.control {
146 ControlKind::Text(_) => "text",
147 #[cfg(feature = "rich-text")]
148 ControlKind::RichText(_) => "rich text",
149 ControlKind::Choice(_) => "choice",
150 ControlKind::File(_) => "file",
151 ControlKind::Custom(_) => "custom",
152 ControlKind::Repeater(_) => "repeater",
153 };
154 f.debug_struct("Field")
155 .field("name", &self.name())
156 .field("label", &self.label())
157 .field("required", &self.required)
158 .field("control", &control)
159 .field("condition", &self.condition)
160 .field("help", &self.help)
161 .field("disabled", &self.disabled)
162 .field("disabling", &self.disabling)
163 .field("hidden", &self.hidden)
164 .field("default", &self.default)
165 .finish()
166 }
167}
168
169impl Field {
170 fn bound(binding: Binding, control: ControlKind) -> Self {
171 Self {
172 binding,
173 label: None,
174 required: false,
175 control,
176 condition: None,
177 span: None,
178 checkbox: false,
179 accepted: false,
180 help: None,
181 disabled: Disabled::Never,
182 disabling: None,
183 hidden: Hidden::default(),
184 default: None,
185 }
186 }
187
188 pub fn text<M, T>(lens: impl Into<Path<M, T>>) -> TextField
191 where
192 M: toasty::schema::Model,
193 T: FormScalar + toasty::stmt::IntoExpr<T> + 'static,
194 {
195 let path: Path<M, T> = lens.into();
196 let binding = Binding::of(&path);
197 let control =
198 TextControl::new::<M, T>(path, binding.unique(), binding.unique_with().to_vec());
199 TextField::new(Self::bound(binding, ControlKind::Text(control)))
200 }
201
202 #[doc(hidden)]
204 pub fn embedded_leaf<M, T>(path: Path<M, T>) -> TextField
205 where
206 M: toasty::schema::Model,
207 T: FormScalar,
208 {
209 TextField::new(Self::bound(
210 Binding::of(&path),
211 ControlKind::Text(TextControl::leaf::<T>()),
212 ))
213 }
214
215 pub fn choice<M, T>(lens: impl Into<Path<M, T>>) -> ChoiceField
218 where
219 M: toasty::schema::Model,
220 {
221 ChoiceField::new(Self::bound(
222 Binding::of::<M, T>(&lens.into()),
223 ControlKind::Choice(ChoiceControl::default()),
224 ))
225 }
226
227 pub fn file<M>(lens: impl Into<Path<M, String>>) -> FileField
230 where
231 M: toasty::schema::Model,
232 {
233 FileField::new(Self::bound(
234 Binding::of::<M, String>(&lens.into()),
235 ControlKind::File(FileControl::default()),
236 ))
237 }
238
239 pub fn files<M>(lens: impl Into<Path<M, List<String>>>) -> FileField
251 where
252 M: toasty::schema::Model,
253 {
254 FileField::new(Self::bound(
255 Binding::of::<M, List<String>>(&lens.into()),
256 ControlKind::File(FileControl {
257 multiple: true,
258 ..FileControl::default()
259 }),
260 ))
261 }
262
263 pub fn toggle<M>(lens: impl Into<Path<M, bool>>) -> CustomField
265 where
266 M: toasty::schema::Model,
267 {
268 let mut field = Self::custom(lens, Toggle);
269 field.0.checkbox = true;
270 field
271 }
272
273 pub fn custom<M, T>(lens: impl Into<Path<M, T>>, control: impl Control + 'static) -> CustomField
275 where
276 M: toasty::schema::Model,
277 T: FormScalar,
278 {
279 CustomField::new(Self::bound(
280 Binding::of::<M, T>(&lens.into()),
281 ControlKind::Custom(Arc::new(control)),
282 ))
283 }
284
285 pub fn repeater<M, T>(lens: impl Into<Path<M, List<T>>>) -> RepeaterField
293 where
294 M: toasty::schema::Model,
295 T: FormInput,
296 {
297 RepeaterField::new(Self::bound(
298 Binding::of::<M, List<T>>(&lens.into()),
299 ControlKind::Repeater(RepeaterControl::new::<T>()),
300 ))
301 }
302
303 pub fn text_input<T: FormScalar>(name: impl Into<String>) -> TextField {
306 TextField::new(Self::bound(
307 Self::named(name.into()),
308 ControlKind::Text(TextControl::leaf::<T>()),
309 ))
310 }
311
312 pub fn choice_input(name: impl Into<String>) -> ChoiceField {
315 ChoiceField::new(Self::bound(
316 Self::named(name.into()),
317 ControlKind::Choice(ChoiceControl::default()),
318 ))
319 }
320
321 pub fn toggle_input(name: impl Into<String>) -> CustomField {
323 let mut field = Self::bound(
324 Self::named(name.into()),
325 ControlKind::Custom(Arc::new(Toggle)),
326 );
327 field.checkbox = true;
328 CustomField::new(field)
329 }
330
331 fn named(name: String) -> Binding {
333 let label = Text::field(&name);
334 Binding::named(name, label)
335 }
336
337 pub(crate) fn discriminant(binding: Binding, variants: Vec<(String, Text)>) -> Self {
340 let choice = ChoiceControl {
341 discriminant: true,
342 options: variants,
343 ..ChoiceControl::default()
344 };
345 Self::bound(binding, ControlKind::Choice(choice))
346 }
347
348 pub(crate) fn misdeclared(&self) -> Option<crate::DeclarationErrorKind> {
349 self.binding.misdeclared()
350 }
351
352 pub fn name(&self) -> &str {
354 self.binding.name()
355 }
356
357 pub(crate) fn label(&self) -> Text {
359 self.label.clone().unwrap_or_else(|| self.binding.label())
360 }
361
362 pub(crate) fn offers_nothing(&self) -> bool {
364 self.as_choice().is_some_and(ChoiceControl::offers_nothing)
365 }
366
367 pub(crate) fn is_password(&self) -> bool {
369 matches!(&self.control, ControlKind::Text(text) if text.password)
370 }
371
372 pub(crate) fn as_repeater(&self) -> Option<&RepeaterControl> {
373 match &self.control {
374 ControlKind::Repeater(repeater) => Some(repeater),
375 _ => None,
376 }
377 }
378
379 pub(crate) fn prefix(&mut self, prefix: &str) {
381 let label = self.label();
382 self.binding = Binding::named(format!("{prefix}{}", self.name()), label);
383 self.prefix_comparisons(prefix);
384 }
385
386 pub(crate) fn as_choice(&self) -> Option<&ChoiceControl> {
387 match &self.control {
388 ControlKind::Choice(choice) => Some(choice),
389 _ => None,
390 }
391 }
392
393 pub(crate) fn is_file(&self) -> bool {
394 matches!(self.control, ControlKind::File(_))
395 }
396
397 pub(crate) fn is_unique(&self) -> bool {
398 matches!(&self.control, ControlKind::Text(text) if text.unique)
399 }
400
401 pub(crate) fn unique_with(&self) -> Vec<&[String]> {
403 match &self.control {
404 ControlKind::Text(text) if text.unique => text.unique_with(),
405 _ => Vec::new(),
406 }
407 }
408
409 pub(crate) fn condition(&self) -> Option<&Condition> {
411 self.condition.as_ref()
412 }
413
414 pub(crate) fn span(&self) -> Option<Span> {
418 self.span.or_else(|| self.is_tall().then_some(Span::Full))
419 }
420
421 fn is_tall(&self) -> bool {
424 match &self.control {
425 ControlKind::Text(text) => text.rows.is_some(),
426 #[cfg(feature = "rich-text")]
427 ControlKind::RichText(_) => true,
428 ControlKind::Choice(choice) => {
429 choice.multiple && choice.layout != super::fields::choice::Layout::Buttons
430 }
431 ControlKind::File(file) => file.multiple,
432 ControlKind::Repeater(_) => true,
433 ControlKind::Custom(_) => false,
434 }
435 }
436
437 pub(crate) fn set_condition(&mut self, condition: Condition) {
439 self.condition = Some(condition);
440 }
441
442 pub(crate) fn is_checkbox(&self) -> bool {
444 self.checkbox
445 }
446
447 pub(crate) fn can_post(&self, value: &str) -> Option<bool> {
451 if self.checkbox {
452 return Some(value == "true" || value == "false");
453 }
454 match &self.control {
455 ControlKind::Choice(choice) if choice.is_multiple() => Some(false),
457 ControlKind::Choice(choice) if !choice.is_relationship() => {
458 Some(value.is_empty() || choice.label_of(value).is_some())
459 }
460 _ => None,
461 }
462 }
463
464 pub(crate) fn refuses(&self, value: &FormValue) -> bool {
468 match (&self.control, value) {
469 (ControlKind::Choice(choice), FormValue::List(items)) if choice.is_multiple() => {
470 !choice.is_relationship()
471 && !choice.tags
472 && items.iter().any(|item| choice.label_of(item).is_none())
473 }
474 (ControlKind::Choice(choice), _) if choice.is_multiple() => true,
475 (ControlKind::Text(text), FormValue::Text(value)) => {
476 !value.trim().is_empty() && !text.parses(value)
477 }
478 (_, FormValue::Text(value)) => self.can_post(value) == Some(false),
479 _ => true,
480 }
481 }
482
483 pub(crate) fn is_required(&self) -> bool {
485 self.required
486 }
487
488 pub(crate) fn default_value(&self) -> Option<&FormValue> {
490 self.default.as_ref()
491 }
492
493 fn chrome(
495 &self,
496 cx: &Cx,
497 id: String,
498 error: Option<&FieldError>,
499 fallback: Option<String>,
500 ) -> FieldChrome {
501 let mut chrome = FieldChrome::new(cx, id, error, &self.label(), fallback);
502 chrome.help = self.help.clone();
503 chrome
504 }
505
506 pub(crate) fn set_required(&mut self, required: bool) {
508 self.required = required;
509 }
510
511 pub(crate) fn is_nullable(&self) -> bool {
513 self.binding.nullable()
514 }
515
516 pub(crate) fn same_value(&self, a: &str, b: &str) -> bool {
518 match &self.control {
519 ControlKind::Text(text) => text.same_value(a, b),
520 _ => a.trim() == b.trim(),
521 }
522 }
523
524 pub(crate) fn parent_key(&self) -> Option<&str> {
526 self.as_choice().and_then(ChoiceControl::parent_key)
527 }
528
529 pub(crate) async fn validate_exists(
532 &self,
533 cx: &Cx,
534 value: &FormValue,
535 parent: Option<&str>,
536 stored: Option<&FormValue>,
537 ) -> Vec<crate::Text> {
538 match &self.control {
539 ControlKind::Choice(choice) => {
540 choice
541 .validate_exists(cx, &self.label(), value, parent, stored)
542 .await
543 }
544 _ => Vec::new(),
545 }
546 }
547
548 pub(crate) async fn recheck(
551 &self,
552 cx: &Cx,
553 value: &FormValue,
554 parent: Option<&str>,
555 stored: Option<&FormValue>,
556 ex: &mut dyn toasty::Executor,
557 ) -> Vec<crate::Text> {
558 match &self.control {
559 ControlKind::Choice(choice) => {
560 choice
561 .recheck(cx, &self.label(), value, parent, stored, ex)
562 .await
563 }
564 _ => Vec::new(),
565 }
566 }
567
568 pub(crate) async fn keep_unseen(
571 &self,
572 cx: &Cx,
573 value: &FormValue,
574 stored: &FormValue,
575 ex: &mut dyn toasty::Executor,
576 ) -> Option<FormValue> {
577 match &self.control {
578 ControlKind::Choice(choice) => choice.keep_unseen(cx, value, stored, ex).await,
579 _ => None,
580 }
581 }
582}
583
584#[cfg(test)]
585mod test_support {
586 pub(super) use crate::test_support::DummyUser;
587}
588
589#[cfg(test)]
590mod tests;