PropValue enum

This commit is contained in:
veeso
2021-03-06 15:10:19 +01:00
parent 44041863ad
commit b90953f65e
4 changed files with 30 additions and 11 deletions

View File

@@ -25,7 +25,7 @@
// locals // locals
use super::super::props::InputType; use super::super::props::InputType;
use super::{Component, InputEvent, Msg, Payload, Props, PropsBuilder, Render}; use super::{Component, InputEvent, Msg, Payload, PropValue, Props, PropsBuilder, Render};
// ext // ext
use crossterm::event::{KeyCode, KeyModifiers}; use crossterm::event::{KeyCode, KeyModifiers};
use tui::{ use tui::{
@@ -153,7 +153,7 @@ impl Input {
// Initialize states // Initialize states
let mut states: OwnStates = OwnStates::default(); let mut states: OwnStates = OwnStates::default();
// Set state value from props // Set state value from props
if let Some(val) = props.value.as_ref() { if let PropValue::Str(val) = props.value.clone() {
for ch in val.chars() { for ch in val.chars() {
states.append(ch, props.input_type); states.append(ch, props.input_type);
} }

View File

@@ -24,7 +24,7 @@
*/ */
// imports // imports
use super::{Component, InputEvent, Msg, Payload, Props, PropsBuilder, Render}; use super::{Component, InputEvent, Msg, Payload, PropValue, Props, PropsBuilder, Render};
// exports // exports
pub mod file_list; pub mod file_list;

View File

@@ -28,7 +28,7 @@ pub mod components;
pub mod props; pub mod props;
// locals // locals
use props::{Props, PropsBuilder}; use props::{Props, PropsBuilder, PropValue};
// ext // ext
use crossterm::event::Event as InputEvent; use crossterm::event::Event as InputEvent;
use crossterm::event::KeyEvent; use crossterm::event::KeyEvent;

View File

@@ -43,7 +43,7 @@ pub struct Props {
pub input_type: InputType, // Input type pub input_type: InputType, // Input type
pub input_len: Option<usize>, // max input len pub input_len: Option<usize>, // max input len
pub texts: TextParts, // text parts pub texts: TextParts, // text parts
pub value: Option<String>, // Initial value pub value: PropValue, // Initial value
} }
impl Default for Props { impl Default for Props {
@@ -59,7 +59,7 @@ impl Default for Props {
input_type: InputType::Text, input_type: InputType::Text,
input_len: None, input_len: None,
texts: TextParts::default(), texts: TextParts::default(),
value: None, value: PropValue::None,
} }
} }
} }
@@ -214,9 +214,9 @@ impl PropsBuilder {
/// ### with_value /// ### with_value
/// ///
/// Set initial value for component /// Set initial value for component
pub fn with_value(&mut self, value: String) -> &mut Self { pub fn with_value(&mut self, value: PropValue) -> &mut Self {
if let Some(props) = self.props.as_mut() { if let Some(props) = self.props.as_mut() {
props.value = Some(value); props.value = value;
} }
self self
} }
@@ -259,6 +259,21 @@ impl Default for TextParts {
} }
} }
// -- Prop value
/// ### PropValue
///
/// PropValue describes a property initial value
#[derive(Clone, PartialEq, std::fmt::Debug)]
pub enum PropValue {
Str(String),
Unsigned(usize),
Signed(isize),
Float(f64),
Boolean(bool),
None,
}
// -- Input Type // -- Input Type
/// ## InputType /// ## InputType
@@ -288,7 +303,7 @@ mod tests {
assert!(props.texts.title.is_none()); assert!(props.texts.title.is_none());
assert_eq!(props.input_type, InputType::Text); assert_eq!(props.input_type, InputType::Text);
assert!(props.input_len.is_none()); assert!(props.input_len.is_none());
assert!(props.value.is_none()); assert_eq!(props.value, PropValue::None);
assert!(props.texts.body.is_none()); assert!(props.texts.body.is_none());
} }
@@ -318,7 +333,7 @@ mod tests {
)) ))
.with_input(InputType::Password) .with_input(InputType::Password)
.with_input_len(16) .with_input_len(16)
.with_value(String::from("Hello")) .with_value(PropValue::Str(String::from("Hello")))
.build(); .build();
assert_eq!(props.background, Color::Blue); assert_eq!(props.background, Color::Blue);
assert_eq!(props.bold, true); assert_eq!(props.bold, true);
@@ -327,7 +342,11 @@ mod tests {
assert_eq!(props.texts.title.as_ref().unwrap().as_str(), "hello"); assert_eq!(props.texts.title.as_ref().unwrap().as_str(), "hello");
assert_eq!(props.input_type, InputType::Password); assert_eq!(props.input_type, InputType::Password);
assert_eq!(*props.input_len.as_ref().unwrap(), 16); assert_eq!(*props.input_len.as_ref().unwrap(), 16);
assert_eq!(props.value.as_ref().unwrap().as_str(), "Hello"); if let PropValue::Str(s) = props.value {
assert_eq!(s.as_str(), "Hello");
} else {
panic!("Expected value to be a string");
}
assert_eq!( assert_eq!(
props.texts.body.as_ref().unwrap().get(0).unwrap().as_str(), props.texts.body.as_ref().unwrap().get(0).unwrap().as_str(),
"hey" "hey"