pub struct AddressBlock { /* private fields */ }Expand description
A composite block for US addresses.
Implementations§
Source§impl AddressBlock
impl AddressBlock
Sourcepub fn new(prefix: impl Into<String>) -> Self
pub fn new(prefix: impl Into<String>) -> Self
Creates a new address block with the given prefix.
Examples found in repository?
examples/address_form.rs (line 41)
18fn main() -> io::Result<()> {
19 // Setup terminal
20 enable_raw_mode()?;
21 let mut stdout = io::stdout();
22 execute!(stdout, EnterAlternateScreen)?;
23 let backend = CrosstermBackend::new(stdout);
24 let mut terminal = Terminal::new(backend)?;
25
26 // Create the form
27 let mut form = Form::builder()
28 .title("Shipping Information")
29 .text("name", "Full Name")
30 .placeholder("John Doe")
31 .required()
32 .done()
33 .text("email", "Email")
34 .placeholder("john@example.com")
35 .required()
36 .validator(Box::new(ratatui_form::Email))
37 .done()
38 .text("phone", "Phone")
39 .placeholder("(555) 123-4567")
40 .done()
41 .block(AddressBlock::new("shipping").required())
42 .checkbox("newsletter", "Subscribe to newsletter")
43 .done()
44 .checkbox("terms", "I agree to the terms and conditions")
45 .required()
46 .done()
47 .build();
48
49 // Main loop
50 loop {
51 // Render
52 terminal.draw(|frame| {
53 let area = frame.area();
54 form.render(area, frame.buffer_mut());
55 })?;
56
57 // Handle input
58 if let Event::Key(key_event) = event::read()? {
59 // Quick exit with Ctrl+C
60 if key_event.code == KeyCode::Char('c')
61 && key_event.modifiers.contains(event::KeyModifiers::CONTROL)
62 {
63 break;
64 }
65
66 form.handle_input(key_event);
67
68 match form.result() {
69 FormResult::Submitted => {
70 // Write JSON and exit
71 form.write_json("shipping.json")?;
72 break;
73 }
74 FormResult::Cancelled => {
75 break;
76 }
77 FormResult::Active => {}
78 }
79 }
80 }
81
82 // Cleanup terminal
83 disable_raw_mode()?;
84 execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
85
86 // Print result
87 match form.result() {
88 FormResult::Submitted => {
89 println!("Form submitted! Data saved to shipping.json");
90 println!("\nForm data:");
91 println!("{}", serde_json::to_string_pretty(&form.to_json()).unwrap());
92 }
93 FormResult::Cancelled => {
94 println!("Form cancelled.");
95 }
96 FormResult::Active => {
97 println!("Form exited.");
98 }
99 }
100
101 Ok(())
102}Sourcepub fn required(self) -> Self
pub fn required(self) -> Self
Marks all fields in this block as required.
Examples found in repository?
examples/address_form.rs (line 41)
18fn main() -> io::Result<()> {
19 // Setup terminal
20 enable_raw_mode()?;
21 let mut stdout = io::stdout();
22 execute!(stdout, EnterAlternateScreen)?;
23 let backend = CrosstermBackend::new(stdout);
24 let mut terminal = Terminal::new(backend)?;
25
26 // Create the form
27 let mut form = Form::builder()
28 .title("Shipping Information")
29 .text("name", "Full Name")
30 .placeholder("John Doe")
31 .required()
32 .done()
33 .text("email", "Email")
34 .placeholder("john@example.com")
35 .required()
36 .validator(Box::new(ratatui_form::Email))
37 .done()
38 .text("phone", "Phone")
39 .placeholder("(555) 123-4567")
40 .done()
41 .block(AddressBlock::new("shipping").required())
42 .checkbox("newsletter", "Subscribe to newsletter")
43 .done()
44 .checkbox("terms", "I agree to the terms and conditions")
45 .required()
46 .done()
47 .build();
48
49 // Main loop
50 loop {
51 // Render
52 terminal.draw(|frame| {
53 let area = frame.area();
54 form.render(area, frame.buffer_mut());
55 })?;
56
57 // Handle input
58 if let Event::Key(key_event) = event::read()? {
59 // Quick exit with Ctrl+C
60 if key_event.code == KeyCode::Char('c')
61 && key_event.modifiers.contains(event::KeyModifiers::CONTROL)
62 {
63 break;
64 }
65
66 form.handle_input(key_event);
67
68 match form.result() {
69 FormResult::Submitted => {
70 // Write JSON and exit
71 form.write_json("shipping.json")?;
72 break;
73 }
74 FormResult::Cancelled => {
75 break;
76 }
77 FormResult::Active => {}
78 }
79 }
80 }
81
82 // Cleanup terminal
83 disable_raw_mode()?;
84 execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
85
86 // Print result
87 match form.result() {
88 FormResult::Submitted => {
89 println!("Form submitted! Data saved to shipping.json");
90 println!("\nForm data:");
91 println!("{}", serde_json::to_string_pretty(&form.to_json()).unwrap());
92 }
93 FormResult::Cancelled => {
94 println!("Form cancelled.");
95 }
96 FormResult::Active => {
97 println!("Form exited.");
98 }
99 }
100
101 Ok(())
102}Trait Implementations§
Auto Trait Implementations§
impl Freeze for AddressBlock
impl RefUnwindSafe for AddressBlock
impl Send for AddressBlock
impl Sync for AddressBlock
impl Unpin for AddressBlock
impl UnwindSafe for AddressBlock
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more