Input len

This commit is contained in:
veeso
2021-03-04 13:36:07 +01:00
parent c9871a0079
commit 3ecf172fb5

View File

@@ -34,15 +34,17 @@ use tui::style::{Color, Modifier};
#[derive(Clone)] #[derive(Clone)]
pub struct Props { pub struct Props {
// Values // Values
pub visible: bool, // Is the element visible ON CREATE? pub visible: bool, // Is the element visible ON CREATE?
pub focus: bool, // Is the element focused pub focus: bool, // Is the element focused
pub foreground: Color, // Foreground color pub foreground: Color, // Foreground color
pub background: Color, // Background color pub background: Color, // Background color
pub bold: bool, // Text bold pub bold: bool, // Text bold
pub italic: bool, // Italic pub italic: bool, // Italic
pub underlined: bool, // Underlined pub underlined: bool, // Underlined
pub input_type: InputType, // Input type pub input_type: InputType, // Input type
pub texts: TextParts, // text parts pub input_len: Option<usize>, // max input len
pub texts: TextParts, // text parts
pub value: Option<String>, // Initial value
} }
impl Default for Props { impl Default for Props {
@@ -57,7 +59,9 @@ impl Default for Props {
italic: false, italic: false,
underlined: false, underlined: false,
input_type: InputType::Text, input_type: InputType::Text,
input_len: None,
texts: TextParts::default(), texts: TextParts::default(),
value: None,
} }
} }
} }
@@ -218,6 +222,26 @@ impl PropsBuilder {
} }
self self
} }
/// ### with_input_len
///
/// Set max input len
pub fn with_input_len(&mut self, len: usize) -> &mut Self {
if let Some(props) = self.props.as_mut() {
props.input_len = Some(len);
}
self
}
/// ### with_value
///
/// Set initial value for component
pub fn with_value(&mut self, value: String) -> &mut Self {
if let Some(props) = self.props.as_mut() {
props.value = Some(value);
}
self
}
} }
impl Default for PropsBuilder { impl Default for PropsBuilder {
@@ -286,6 +310,8 @@ mod tests {
assert_eq!(props.underlined, false); assert_eq!(props.underlined, false);
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.value.is_none());
assert!(props.texts.body.is_none()); assert!(props.texts.body.is_none());
} }
@@ -315,6 +341,8 @@ mod tests {
Some(vec![String::from("hey")]), Some(vec![String::from("hey")]),
)) ))
.with_input(InputType::Password) .with_input(InputType::Password)
.with_input_len(16)
.with_value(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);
@@ -323,6 +351,8 @@ mod tests {
assert_eq!(props.italic, true); assert_eq!(props.italic, true);
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.value.as_ref().unwrap().as_str(), "Hello");
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"