vertigo_forms/form/data/
data_field.rs

1use std::{collections::HashMap, rc::Rc};
2use vertigo::{Computed, Context, DomNode, DropFileItem, Value};
3
4use crate::DropImageFileParams;
5
6use super::form_export::FieldExport;
7
8/// Value of a field in form section.
9#[derive(Clone)]
10pub enum DataFieldValue {
11    /// Regular string field.
12    String(StringValue),
13    /// Textarea string field.
14    TextArea(TextAreaValue),
15    /// String field with options.
16    List(ListValue),
17    /// Integer (foreign key) field with labels for each integer.
18    Dict(DictValue),
19    /// Array of integers (foreign key) field with labels for each integer.
20    Multi(MultiValue),
21    /// Checkbox
22    Bool(BoolValue),
23    /// Image (bytes) field.
24    Image(ImageValue),
25    /// Custom field
26    Custom(CustomValue),
27    /// Custom component without value
28    StaticCustom(Rc<dyn Fn() -> DomNode>),
29}
30
31impl DataFieldValue {
32    pub fn export(&self, ctx: &Context) -> FieldExport {
33        match self {
34            Self::String(val) => FieldExport::String(val.value.get(ctx)),
35            Self::TextArea(val) => FieldExport::String(val.value.get(ctx)),
36            Self::List(val) => FieldExport::List(val.value.get(ctx)),
37            Self::Dict(val) => FieldExport::Dict(val.value.get(ctx)),
38            Self::Multi(val) => {
39                FieldExport::Multi(val.value.get(ctx).iter().map(|v| v.get(ctx)).collect())
40            }
41            Self::Bool(val) => FieldExport::Bool(val.value.get(ctx)),
42            Self::Image(val) => FieldExport::Image((val.original_link.clone(), val.value.get(ctx))),
43            Self::Custom(val) => FieldExport::String(val.value.get(ctx)),
44            Self::StaticCustom(_) => FieldExport::String("".to_string()),
45        }
46    }
47}
48
49#[derive(Clone)]
50pub struct StringValue {
51    pub value: Value<String>,
52    pub original_value: Rc<String>,
53}
54
55#[derive(Clone)]
56pub struct TextAreaValue {
57    pub value: Value<String>,
58    pub original_value: Option<Rc<String>>,
59    pub rows: Option<i32>,
60    pub cols: Option<i32>,
61}
62
63#[derive(Clone)]
64pub struct ListValue {
65    pub value: Value<String>,
66    pub original_value: Option<Rc<String>>,
67    pub options: Computed<Vec<String>>,
68}
69
70#[derive(Clone)]
71pub struct DictValue {
72    pub value: Value<i64>,
73    pub original_value: Option<Rc<i64>>,
74    pub options: Computed<Vec<(i64, String)>>,
75}
76
77#[derive(Clone)]
78pub struct MultiValue {
79    pub value: Value<Vec<Value<i64>>>,
80    pub original_value: Rc<Vec<i64>>,
81    pub options: Computed<HashMap<i64, String>>,
82    pub add_label: Rc<String>,
83}
84
85#[derive(Clone)]
86pub struct BoolValue {
87    pub value: Value<bool>,
88    pub original_value: Option<Rc<bool>>,
89}
90
91#[derive(Clone)]
92pub struct ImageValue {
93    pub value: Value<Option<DropFileItem>>,
94    pub original_link: Option<Rc<String>>,
95    pub component_params: Option<DropImageFileParams>,
96}
97
98#[derive(Clone)]
99pub struct CustomValue {
100    pub value: Value<String>,
101    pub original_value: Option<Rc<String>>,
102    pub render: Rc<dyn Fn() -> DomNode>,
103}