Skip to main content

ez_tui/components/concrete/forms/inputs/
core.rs

1use crate::components::concrete::forms::inputs::check_input::CheckboxInputCpt;
2use crate::components::concrete::forms::inputs::date_input::DateTimeInputCpt;
3use crate::components::concrete::forms::inputs::number_input::NumberInputCpt;
4use crate::components::concrete::forms::inputs::range_input::RangeInputCpt;
5use crate::components::concrete::forms::inputs::select_input::SelectInputCpt;
6use crate::components::concrete::forms::inputs::text_input::TextInputCpt;
7pub(crate) use crate::components::concrete::forms::result::FormFieldValue;
8use crate::{Component, EzArgs, EzCptIds, EzMsg, EzState, FormField, FormFieldType};
9use std::fmt::Debug;
10
11/// A trait to be implemented by all input components in the form system.
12pub trait InputCpt<CID, CA, CS, CM>: Component<CID, CA, CS, CM> + Debug
13where
14    CID: EzCptIds,
15    CA: EzArgs,
16    CS: EzState,
17    CM: EzMsg,
18{
19    /// Get the current value of the input field.
20    fn get_value(&self) -> FormFieldValue;
21
22    /// Whether this input component should be forwarded the form validation
23    /// so it can act on it and return if it has been handled internally.
24    fn capture_validation(&mut self) -> bool {
25        false
26    }
27}
28
29/// A builder for creating input components based on a given form field.
30/// TODO: bad design
31#[derive(Debug)]
32pub struct InputBuilder;
33impl InputBuilder {
34    /// TODO
35    pub fn create<FID, CID, CA, CS, CM>(
36        field: &FormField<FID>,
37    ) -> Box<dyn InputCpt<CID, CA, CS, CM>>
38    where
39        FID: EzCptIds,
40        CID: EzCptIds,
41        CA: EzArgs,
42        CS: EzState,
43        CM: EzMsg,
44    {
45        match field.field_type() {
46            FormFieldType::Text {} => Box::new(TextInputCpt::clear(field.clone())),
47            FormFieldType::CheckBox {} => Box::new(CheckboxInputCpt::new(field.clone())),
48            FormFieldType::Range {} => Box::new(RangeInputCpt::new(field.clone())),
49            FormFieldType::DateTime {} => Box::new(DateTimeInputCpt::datetime(field.clone())),
50            FormFieldType::Date {} => Box::new(DateTimeInputCpt::date_only(field.clone())),
51            FormFieldType::Time {} => Box::new(DateTimeInputCpt::time_only(field.clone())),
52            FormFieldType::Number {} => Box::new(NumberInputCpt::new(field.clone())),
53            FormFieldType::Select { .. } => Box::new(SelectInputCpt::new(field.clone())),
54            FormFieldType::Password {} => Box::new(TextInputCpt::secret(field.clone())),
55        }
56    }
57}