pub struct Form { /* private fields */ }
Expand description
A widget to display data in a collection of fields
§Example
let form = Form::new(&["A", "B", "C"], |field| !field.is_empty());
// all fields remain valid until form is submitted.
assert_eq!(form.status().iter().all(|field| field.is_valid()), true);
// fields will now be invalid after submitting.
form.submit();
assert_eq!(form.status().iter().all(|field| field.is_valid()), false);
form.select(FormSelection::Active(0));
form.append_selection('a');
assert!(form.status[0].is_valid());
Implementations§
Source§impl Form
impl Form
Sourcepub fn new(
fields: &[&str],
validation_fn: impl Fn(&str) -> bool + 'static,
) -> Self
pub fn new( fields: &[&str], validation_fn: impl Fn(&str) -> bool + 'static, ) -> Self
Create a new Form
from a slice of field titles and a validator function.
validation_fn
is used to mark fields as either valid or invalid when .status()
is called.
pub fn select(&mut self, s: FormSelection)
Sourcepub fn selected(&self) -> &FormSelection
pub fn selected(&self) -> &FormSelection
Examples found in repository?
examples/minimal.rs (line 65)
62fn handle_input(state: &mut State) -> io::Result<()> {
63 if event::poll(Duration::from_millis(250))? {
64 if let Event::Key(key) = event::read()? {
65 match state.form.selected() {
66 FormSelection::NoSelection => match key.code {
67 KeyCode::Esc | KeyCode::Char('q') => state.should_quit = true,
68 KeyCode::Char('s') => {
69 let fields = state.form.submit();
70 if fields.iter().any(|f| !f.is_valid()) {
71 } else {
72 // Field impls Into<String>
73 state.submissions = Some(fields.into_iter().map(Into::into).collect());
74
75 state.form.deselect();
76 }
77 }
78 _ => {}
79 },
80 _ => {}
81 }
82
83 state.form.input(key.code);
84 }
85 }
86
87 Ok(())
88}
Sourcepub fn submit(&mut self) -> Vec<Field<'_>>
pub fn submit(&mut self) -> Vec<Field<'_>>
Submits form and returns status of fields.
Examples found in repository?
examples/minimal.rs (line 69)
62fn handle_input(state: &mut State) -> io::Result<()> {
63 if event::poll(Duration::from_millis(250))? {
64 if let Event::Key(key) = event::read()? {
65 match state.form.selected() {
66 FormSelection::NoSelection => match key.code {
67 KeyCode::Esc | KeyCode::Char('q') => state.should_quit = true,
68 KeyCode::Char('s') => {
69 let fields = state.form.submit();
70 if fields.iter().any(|f| !f.is_valid()) {
71 } else {
72 // Field impls Into<String>
73 state.submissions = Some(fields.into_iter().map(Into::into).collect());
74
75 state.form.deselect();
76 }
77 }
78 _ => {}
79 },
80 _ => {}
81 }
82
83 state.form.input(key.code);
84 }
85 }
86
87 Ok(())
88}
Sourcepub fn status(&self) -> Vec<Field<'_>>
pub fn status(&self) -> Vec<Field<'_>>
Returns the state of all fields in the form. Uses a Field
struct to indicate whether or
not each field’s buffer is valid.
Sourcepub fn input(&mut self, key: KeyCode)
pub fn input(&mut self, key: KeyCode)
Examples found in repository?
examples/minimal.rs (line 83)
62fn handle_input(state: &mut State) -> io::Result<()> {
63 if event::poll(Duration::from_millis(250))? {
64 if let Event::Key(key) = event::read()? {
65 match state.form.selected() {
66 FormSelection::NoSelection => match key.code {
67 KeyCode::Esc | KeyCode::Char('q') => state.should_quit = true,
68 KeyCode::Char('s') => {
69 let fields = state.form.submit();
70 if fields.iter().any(|f| !f.is_valid()) {
71 } else {
72 // Field impls Into<String>
73 state.submissions = Some(fields.into_iter().map(Into::into).collect());
74
75 state.form.deselect();
76 }
77 }
78 _ => {}
79 },
80 _ => {}
81 }
82
83 state.form.input(key.code);
84 }
85 }
86
87 Ok(())
88}
pub fn append_selection(&mut self, ch: char)
pub fn pop_selection(&mut self)
Sourcepub fn deselect(&mut self)
pub fn deselect(&mut self)
Examples found in repository?
examples/minimal.rs (line 75)
62fn handle_input(state: &mut State) -> io::Result<()> {
63 if event::poll(Duration::from_millis(250))? {
64 if let Event::Key(key) = event::read()? {
65 match state.form.selected() {
66 FormSelection::NoSelection => match key.code {
67 KeyCode::Esc | KeyCode::Char('q') => state.should_quit = true,
68 KeyCode::Char('s') => {
69 let fields = state.form.submit();
70 if fields.iter().any(|f| !f.is_valid()) {
71 } else {
72 // Field impls Into<String>
73 state.submissions = Some(fields.into_iter().map(Into::into).collect());
74
75 state.form.deselect();
76 }
77 }
78 _ => {}
79 },
80 _ => {}
81 }
82
83 state.form.input(key.code);
84 }
85 }
86
87 Ok(())
88}
pub fn next_field(&mut self)
pub fn prev_field(&mut self)
pub fn active_field_style(&mut self, style: Style)
pub fn invalid_field_style(&mut self, style: Style)
pub fn hovered_field_style(&mut self, style: Style)
pub fn default_field_style(&mut self, style: Style)
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Form
impl !RefUnwindSafe for Form
impl !Send for Form
impl !Sync for Form
impl Unpin for Form
impl !UnwindSafe for Form
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