Struct Form

Source
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

Source

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.

Source

pub fn widget(&self) -> impl Widget + '_

Examples found in repository?
examples/minimal.rs (line 58)
55fn render_app(frame: &mut Frame<CrosstermBackend<Stdout>>, state: &State) {
56    match &state.submissions {
57        Some(fields) => frame.render_widget(Paragraph::new(fields.join("\n")), frame.size()),
58        None => frame.render_widget(state.form.widget(), frame.size()),
59    }
60}
Source

pub fn select(&mut self, s: FormSelection)

Source

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}
Source

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}
Source

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.

Source

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}
Source

pub fn append_selection(&mut self, ch: char)

Source

pub fn pop_selection(&mut self)

Source

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}
Source

pub fn next_field(&mut self)

Source

pub fn prev_field(&mut self)

Source

pub fn submitted(&mut self, submitted: bool)

Set whether the Form has been submitted

Source

pub fn active_field_style(&mut self, style: Style)

Source

pub fn invalid_field_style(&mut self, style: Style)

Source

pub fn hovered_field_style(&mut self, style: Style)

Source

pub fn default_field_style(&mut self, style: Style)

Trait Implementations§

Source§

impl Default for Form

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Vec<&str>> for Form

Source§

fn from(value: Vec<&str>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<(&str, &str)>> for Form

Source§

fn from(value: Vec<(&str, &str)>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<FieldBuffer>> for Form

Source§

fn from(value: Vec<FieldBuffer>) -> Self

Converts to this type from the input type.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.