TermUI.Widgets.FormBuilder (TermUI v0.2.0)
View SourceFormBuilder widget for structured forms with multiple field types.
Provides comprehensive form handling with validation, navigation, conditional fields, and field grouping.
Usage
FormBuilder.new(
fields: [
%{id: :username, type: :text, label: "Username", required: true},
%{id: :password, type: :password, label: "Password", required: true},
%{id: :remember, type: :checkbox, label: "Remember me"},
%{id: :role, type: :select, label: "Role",
options: [{"admin", "Admin"}, {"user", "User"}]}
],
on_submit: fn values -> handle_submit(values) end,
on_change: fn field_id, value -> handle_change(field_id, value) end
)Field Types
:text- Single line text input:password- Masked text input:checkbox- Boolean toggle:radio- Single selection from options:select- Dropdown single selection:multi_select- Multiple selection from options
Keyboard Navigation
- Tab/Shift+Tab: Move between fields
- Up/Down: Navigate options (radio/select/multi_select)
- Space: Toggle checkbox, select option
- Enter: Submit form (when on submit button)
- Escape: Cancel editing
Summary
Functions
Focuses a specific field.
Gets all validation errors.
Gets the currently focused field.
Gets the value of a specific field.
Gets the current form values.
Creates new FormBuilder widget props.
Resets the form to initial values.
Sets the value of a specific field.
Sets multiple field values at once.
Toggles a group's collapsed state.
Checks if the form has any validation errors.
Validates all fields and returns updated state.
Types
@type field_def() :: %{ id: atom(), type: field_type(), label: String.t(), options: [{term(), String.t()}] | nil, required: boolean(), validators: [(term() -> :ok | {:error, String.t()})], visible_when: (map() -> boolean()) | nil, group: atom() | nil, placeholder: String.t() | nil, default: term() | nil }
@type field_type() :: :text | :password | :checkbox | :radio | :select | :multi_select
Functions
Focuses a specific field.
Gets all validation errors.
Gets the currently focused field.
Gets the value of a specific field.
Gets the current form values.
Creates new FormBuilder widget props.
Options
:fields- List of field definitions (required):groups- List of group definitions for organizing fields:on_submit- Callback when form is submitted:on_change- Callback when any field value changes:values- Initial field values:show_submit_button- Whether to show submit button (default: true):submit_label- Label for submit button (default: "Submit"):validate_on_blur- Validate when field loses focus (default: true)
Resets the form to initial values.
Sets the value of a specific field.
Sets multiple field values at once.
Toggles a group's collapsed state.
Checks if the form has any validation errors.
Validates all fields and returns updated state.