pub struct FormBuilder { /* private fields */ }Expand description
Builder for creating forms.
Implementations§
Source§impl FormBuilder
impl FormBuilder
Sourcepub fn title(self, title: impl Into<String>) -> Self
pub fn title(self, title: impl Into<String>) -> Self
Sets the form title.
Examples found in repository?
examples/address_form.rs (line 28)
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 text(
self,
id: impl Into<String>,
label: impl Into<String>,
) -> TextFieldBuilder
pub fn text( self, id: impl Into<String>, label: impl Into<String>, ) -> TextFieldBuilder
Starts building a text field.
Examples found in repository?
examples/address_form.rs (line 29)
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 select(
self,
id: impl Into<String>,
label: impl Into<String>,
) -> SelectFieldBuilder
pub fn select( self, id: impl Into<String>, label: impl Into<String>, ) -> SelectFieldBuilder
Starts building a select field.
Sourcepub fn checkbox(
self,
id: impl Into<String>,
label: impl Into<String>,
) -> CheckboxFieldBuilder
pub fn checkbox( self, id: impl Into<String>, label: impl Into<String>, ) -> CheckboxFieldBuilder
Starts building a checkbox field.
Examples found in repository?
examples/address_form.rs (line 42)
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 block(self, block: impl FormBlock) -> Self
pub fn block(self, block: impl FormBlock) -> Self
Adds all fields from a block.
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 build(self) -> Form
pub fn build(self) -> Form
Builds the form.
Examples found in repository?
examples/address_form.rs (line 47)
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 FormBuilder
impl !RefUnwindSafe for FormBuilder
impl Send for FormBuilder
impl Sync for FormBuilder
impl Unpin for FormBuilder
impl !UnwindSafe for FormBuilder
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