ratatui_form/block/
contact.rs1use crate::block::Block;
4use crate::field::{Field, TextInput};
5use crate::validation::rules::{Email, Pattern};
6
7pub struct ContactBlock {
9 prefix: String,
10 title: Option<String>,
11 required: bool,
12}
13
14impl ContactBlock {
15 pub fn new(prefix: impl Into<String>) -> Self {
17 Self {
18 prefix: prefix.into(),
19 title: None,
20 required: false,
21 }
22 }
23
24 pub fn title(mut self, title: impl Into<String>) -> Self {
26 self.title = Some(title.into());
27 self
28 }
29
30 pub fn required(mut self) -> Self {
32 self.required = true;
33 self
34 }
35
36 fn field_id(&self, name: &str) -> String {
37 format!("{}_{}", self.prefix, name)
38 }
39}
40
41impl Block for ContactBlock {
42 fn prefix(&self) -> &str {
43 &self.prefix
44 }
45
46 fn title(&self) -> Option<&str> {
47 self.title.as_deref()
48 }
49
50 fn fields(&self) -> Vec<Box<dyn Field>> {
51 let mut fields: Vec<Box<dyn Field>> = Vec::new();
52
53 let mut name = TextInput::new(self.field_id("name"), "Full Name")
55 .placeholder("John Doe");
56 if self.required {
57 name = name.required();
58 }
59 fields.push(Box::new(name));
60
61 let mut email = TextInput::new(self.field_id("email"), "Email")
63 .placeholder("john@example.com")
64 .validator(Box::new(Email));
65 if self.required {
66 email = email.required();
67 }
68 fields.push(Box::new(email));
69
70 let phone = TextInput::new(self.field_id("phone"), "Phone")
72 .placeholder("(555) 123-4567")
73 .validator(Box::new(Pattern::phone()));
74 fields.push(Box::new(phone));
75
76 fields
77 }
78}