mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
PropValue enum
This commit is contained in:
@@ -25,7 +25,7 @@
|
||||
|
||||
// locals
|
||||
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
|
||||
use crossterm::event::{KeyCode, KeyModifiers};
|
||||
use tui::{
|
||||
@@ -153,7 +153,7 @@ impl Input {
|
||||
// Initialize states
|
||||
let mut states: OwnStates = OwnStates::default();
|
||||
// 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() {
|
||||
states.append(ch, props.input_type);
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
*/
|
||||
|
||||
// imports
|
||||
use super::{Component, InputEvent, Msg, Payload, Props, PropsBuilder, Render};
|
||||
use super::{Component, InputEvent, Msg, Payload, PropValue, Props, PropsBuilder, Render};
|
||||
|
||||
// exports
|
||||
pub mod file_list;
|
||||
|
||||
@@ -28,7 +28,7 @@ pub mod components;
|
||||
pub mod props;
|
||||
|
||||
// locals
|
||||
use props::{Props, PropsBuilder};
|
||||
use props::{Props, PropsBuilder, PropValue};
|
||||
// ext
|
||||
use crossterm::event::Event as InputEvent;
|
||||
use crossterm::event::KeyEvent;
|
||||
|
||||
@@ -43,7 +43,7 @@ pub struct Props {
|
||||
pub input_type: InputType, // Input type
|
||||
pub input_len: Option<usize>, // max input len
|
||||
pub texts: TextParts, // text parts
|
||||
pub value: Option<String>, // Initial value
|
||||
pub value: PropValue, // Initial value
|
||||
}
|
||||
|
||||
impl Default for Props {
|
||||
@@ -59,7 +59,7 @@ impl Default for Props {
|
||||
input_type: InputType::Text,
|
||||
input_len: None,
|
||||
texts: TextParts::default(),
|
||||
value: None,
|
||||
value: PropValue::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -214,9 +214,9 @@ impl PropsBuilder {
|
||||
/// ### with_value
|
||||
///
|
||||
/// 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() {
|
||||
props.value = Some(value);
|
||||
props.value = value;
|
||||
}
|
||||
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
|
||||
|
||||
/// ## InputType
|
||||
@@ -288,7 +303,7 @@ mod tests {
|
||||
assert!(props.texts.title.is_none());
|
||||
assert_eq!(props.input_type, InputType::Text);
|
||||
assert!(props.input_len.is_none());
|
||||
assert!(props.value.is_none());
|
||||
assert_eq!(props.value, PropValue::None);
|
||||
assert!(props.texts.body.is_none());
|
||||
}
|
||||
|
||||
@@ -318,7 +333,7 @@ mod tests {
|
||||
))
|
||||
.with_input(InputType::Password)
|
||||
.with_input_len(16)
|
||||
.with_value(String::from("Hello"))
|
||||
.with_value(PropValue::Str(String::from("Hello")))
|
||||
.build();
|
||||
assert_eq!(props.background, Color::Blue);
|
||||
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.input_type, InputType::Password);
|
||||
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!(
|
||||
props.texts.body.as_ref().unwrap().get(0).unwrap().as_str(),
|
||||
"hey"
|
||||
|
||||
Reference in New Issue
Block a user