etui/widgets/form

Types

A single form field.

pub type Field(id) {
  Field(
    id: id,
    label: String,
    value: String,
    validator: fn(String) -> Result(Nil, String),
    error: String,
    max_length: Int,
  )
}

Constructors

  • Field(
      id: id,
      label: String,
      value: String,
      validator: fn(String) -> Result(Nil, String),
      error: String,
      max_length: Int,
    )

    Arguments

    max_length

    Number of graphemes / cells allowed (0 = unlimited within display width).

Form state: ordered list of fields plus focus index.

pub type Form(id) {
  Form(
    fields: List(Field(id)),
    focused: Int,
    submitted: Bool,
    label_width: Int,
    fg: style.Color,
    bg: style.Color,
    focused_fg: style.Color,
    focused_bg: style.Color,
    error_fg: style.Color,
  )
}

Constructors

Validator: Ok(Nil) if valid, Error(String) with message if not.

pub type Validator =
  fn(String) -> Result(Nil, String)

Values

pub fn add_field(
  f: Form(id),
  id: id,
  label: String,
  default_value: String,
  validator: fn(String) -> Result(Nil, String),
) -> Form(id)

Append a field with a validator.

pub fn add_optional(
  f: Form(id),
  id: id,
  label: String,
  default_value: String,
) -> Form(id)

Append an optional field (always valid).

pub fn add_required(
  f: Form(id),
  id: id,
  label: String,
  default_value: String,
) -> Form(id)

Append a required text field (non-empty validator).

pub fn backspace(f: Form(id)) -> Form(id)

Backspace on the currently focused field.

pub fn clear_focused(f: Form(id)) -> Form(id)

Clear the currently focused field’s value.

pub fn focus_index(f: Form(id), idx: Int) -> Form(id)

Move focus to a specific field by index.

pub fn focus_next(f: Form(id)) -> Form(id)

Move focus to the next field (wraps around).

pub fn focus_prev(f: Form(id)) -> Form(id)

Move focus to the previous field (wraps around).

pub fn form_new() -> Form(id)

Empty form with default styles.

pub fn get_value(f: Form(id), id: id) -> String

Get a field’s current value by id. Returns “” if not found.

pub fn is_submitted(f: Form(id)) -> Bool

True if the form was successfully submitted.

pub fn is_valid(f: Form(id)) -> Bool

True if all fields are valid (no errors after validation).

pub fn render(
  buf: buffer.Buffer,
  area: geometry.Rect,
  f: Form(id),
) -> buffer.Buffer

Render all fields as label + value rows. Each field takes 2 rows (value row + optional error row). Focused field is highlighted.

pub fn reset(f: Form(id)) -> Form(id)

Reset all fields to empty, clear errors and submitted flag.

pub fn set_value(f: Form(id), id: id, value: String) -> Form(id)

Set a field’s value by id.

pub fn submit(f: Form(id)) -> Form(id)

Validate then mark as submitted if valid. Returns the form.

pub fn type_char(f: Form(id), ch: String) -> Form(id)

Type a character into the currently focused field.

pub fn validate(f: Form(id)) -> Form(id)

Validate all fields. Returns form with error messages populated.

pub fn values(f: Form(id)) -> List(#(id, String))

Get all field values as #(id, value) pairs.

pub fn with_colors(
  f: Form(id),
  fg: style.Color,
  bg: style.Color,
) -> Form(id)

Set base foreground/background.

pub fn with_error_color(f: Form(id), fg: style.Color) -> Form(id)

Set validation error text color.

pub fn with_field_max_length(f: Form(id), max: Int) -> Form(id)

Set max grapheme length for the most-recently added field.

pub fn with_focused_colors(
  f: Form(id),
  fg: style.Color,
  bg: style.Color,
) -> Form(id)

Set focused field highlight colors.

pub fn with_label_width(f: Form(id), w: Int) -> Form(id)

Override label column width (auto-computed from labels if 0).

Search Document