SutraUI.Input (Sutra UI v0.2.0)
View SourceRenders form input elements with label and error handling.
A drop-in replacement for Phoenix's generated input component, providing full parity with Phoenix generators while using Sutra UI styling.
Examples
# Basic text input
<.input type="text" name="username" placeholder="Username" />
# With form field (auto-extracts id, name, value, errors)
<.input field={@form[:email]} type="email" label="Email" />
# Password input with label
<.input type="password" name="password" label="Password" required />
# Select with options
<.input
type="select"
name="country"
label="Country"
prompt="Select a country"
options={[{"United States", "us"}, {"Canada", "ca"}]}
/>
# Checkbox
<.input type="checkbox" name="terms" label="I agree to the terms" />
# Switch (toggle)
<.input type="switch" name="notifications" label="Enable notifications" />
# Textarea
<.input type="textarea" name="bio" label="Bio" rows={4} />
# Range slider
<.input type="range" name="volume" label="Volume" min={0} max={100} />Types
This component accepts all HTML input types, with special handling for:
type="checkbox"- Delegates toSutraUI.Checkboxwith hidden false valuetype="switch"- Delegates toSutraUI.Switch(toggle with role="switch")type="select"- Renders a native<select>elementtype="textarea"- Delegates toSutraUI.Textareatype="range"- Delegates toSutraUI.Sliderwith colocated hooktype="hidden"- Renders just the input, no wrapper or label
For live file uploads, see Phoenix.Component.live_file_input/1.
Select Types
This component provides two ways to render select inputs:
Native Select (via <.input type="select">)
For standard form selects that work with Phoenix generators:
<.input
field={@form[:country]}
type="select"
label="Country"
prompt="Select a country"
options={[{"United States", "us"}, {"Canada", "ca"}]}
/>Custom Select (via SutraUI.Select)
For advanced features like search/filter, keyboard navigation, and custom styling,
use the dedicated SutraUI.Select component directly:
<.select id="country" name="country" value={@country} searchable>
<.select_option value="us" label="United States" />
<.select_option value="ca" label="Canada" />
</.select>See SutraUI.Select for more details.
Accessibility
- Labels are properly associated with inputs via wrapping
aria-invalidis automatically set when errors are present- Error messages are displayed below inputs
- Switch inputs include
role="switch"andaria-checked - Range inputs include
aria-valuemin,aria-valuemax,aria-valuenow - Supports standard ARIA attributes via
:rest
Summary
Functions
Renders an input with label and error messages.
Translates an error message using gettext.
Translates the errors for a field from a keyword list of errors.
Functions
Renders an input with label and error messages.
A Phoenix.HTML.FormField may be passed as argument,
which is used to retrieve the input name, id, and values.
Otherwise all attributes may be passed explicitly.
Examples
<.input field={@form[:email]} type="email" />
<.input name="my-input" errors={["oh no!"]} />Attributes
id(:any) - The id attribute for the input. Defaults tonil.name(:any) - The name attribute for the input. Defaults tonil.label(:string) - Label text - renders label before input. Defaults tonil.value(:any) - The value of the input. Defaults tonil.type(:string) - Defaults to"text". Must be one of"checkbox","color","date","datetime-local","email","file","hidden","month","number","password","range","search","select","switch","tel","text","textarea","time","url", or"week".field(Phoenix.HTML.FormField) - A form field struct retrieved from the form, for example: @form[:email].errors(:list) - List of error messages to display. Defaults to[].checked(:boolean) - The checked flag for checkbox inputs.prompt(:string) - The prompt for select inputs. Defaults tonil.options(:list) - The options to pass to Phoenix.HTML.Form.options_for_select/2.multiple(:boolean) - The multiple flag for select inputs. Defaults tofalse.class(:any) - Additional CSS classes. Defaults tonil.- Global attributes are accepted. Supports all globals plus:
["accept", "autocomplete", "capture", "cols", "disabled", "form", "list", "max", "maxlength", "min", "minlength", "multiple", "pattern", "placeholder", "readonly", "required", "rows", "size", "step", "phx-debounce", "phx-mounted", "aria-label", "aria-describedby", "aria-invalid", "aria-required"].
Translates an error message using gettext.
Translates the errors for a field from a keyword list of errors.