#[derive(Clone, Debug, PartialEq)]
pub enum TextFieldBehavior {
FreeText,
NumericFloat { min: f64, max: f64 },
NumericInt { min: u32, max: u32 },
Search,
}
#[derive(Clone, Debug)]
pub struct TextFieldConfig {
pub field_id: String,
pub behavior: TextFieldBehavior,
pub live_update: bool,
}
impl TextFieldConfig {
pub fn new(field_id: &str, behavior: TextFieldBehavior) -> Self {
let live_update = matches!(behavior, TextFieldBehavior::Search);
Self {
field_id: field_id.to_string(),
behavior,
live_update,
}
}
pub fn with_live_update(mut self, live: bool) -> Self {
self.live_update = live;
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum TextInputKey {
Left,
Right,
Home,
End,
Backspace,
Delete,
Enter,
Escape,
SelectAll,
Tab,
Copy, Cut, Paste(String), }
#[derive(Clone, Debug)]
pub enum TextInputAction {
Consumed,
Changed(String),
Confirmed(ConfirmedValue),
Cancelled(String),
FocusNext,
FocusPrev,
NotConsumed,
}
#[derive(Clone, Debug, PartialEq)]
pub enum ConfirmedValue {
Text(String),
Float(f64),
Int(u32),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_auto_live_update_for_search() {
let config = TextFieldConfig::new("search", TextFieldBehavior::Search);
assert!(config.live_update);
}
#[test]
fn test_config_manual_live_update() {
let config = TextFieldConfig::new("text", TextFieldBehavior::FreeText)
.with_live_update(true);
assert!(config.live_update);
}
}