Skip to main content

yew_bootstrap/component/form/
form_type.rs

1use yew::prelude::*;
2
3
4/// # Type of form control, to be used with [crate::component::form::FormControl].
5#[derive(Clone, PartialEq)]
6
7pub enum FormControlType {
8    /// Simple text control
9    Text,
10    /// Textarea with optional rows and columns
11    TextArea { cols: Option<u32>, rows: Option<u32> },
12    /// Email with optional pattern (regexp, for example ".+@example\.com")
13    Email { pattern: Option<AttrValue> },
14    /// Password whose value is obscured
15    Password,
16    /// Url with optional pattern (regexp, for example "https://.+")
17    Url { pattern: Option<AttrValue>},
18    /// Number with optional min and max
19    Number { min: Option<u32>, max: Option<u32> },
20    /// Range selection from min to max, with optional step
21    Range { min: i32, max: i32, step: Option<u32> },
22
23    /// Select to select one of more options. It typically contains [crate::component::form::SelectOption]
24    /// or [crate::component::form::SelectOptgroup] children
25    Select,
26    /// Checkbox
27    Checkbox,
28    /// Radio button
29    Radio,
30
31    /// Date picker, format YYYY-mm-dd
32    Date,
33    /// Date picker with optional min and max boundaries (Format "YYYY-mm-dd")
34    DateMinMax { min: Option<AttrValue>, max: Option<AttrValue> },
35    /// Date and time (Local), format YYYY-mm-ddTHH:MM
36    Datetime,
37    /// Date and time (Local) with min and max boundaries (Format "YYYY-mm-ddTHH:MM")
38    DatetimeMinMax { min: Option<AttrValue>, max: Option<AttrValue> },
39    /// Time, format HH:MM
40    Time,
41    /// Time with min and max values (format "HH:MM")
42    TimeMinMax { min: Option<AttrValue>, max: Option<AttrValue> },
43
44    /// Color picker
45    Color,
46    /// File input. Accept is a vector of formats, like "image/png", ".docx"
47    File { accept: Vec<AttrValue> },
48    /// Hidden input, used to transmit data
49    Hidden
50}
51
52impl FormControlType {
53    /// Convert enum to HTML type string
54    pub fn to_str(&self) -> AttrValue {
55        let value = match &self {
56            Self::Checkbox => "checkbox",
57            Self::Color => "color",
58            Self::Date | Self::DateMinMax { .. } => "date",
59            Self::Datetime | Self::DatetimeMinMax { .. } => "datetime-local",
60            Self::Email { .. } => "email",
61            Self::File { .. } => "file",
62            Self::Hidden => "hidden",
63            Self::Number { .. } => "number",
64            Self::Password { .. } => "password",
65            Self::Radio => "radio",
66            Self::Range { .. } => "range",
67            Self::Select => "select",
68            Self::Text => "text",
69            Self::TextArea { .. } => "",
70            Self::Time | Self::TimeMinMax { .. } => "time",
71            Self::Url { .. } => "url",
72        };
73
74        AttrValue::from(value)
75    }
76}