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
-
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, )
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 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 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.