Documentation / Reference

field

Edit a single-line or multiline string through a binding.

Construction

Use field(value: Binding<String>) -> Field. Import constructors and common modifiers from refract::prelude::*.

field.rs
use refract::prelude::*;

struct Editor;

impl View for Editor {
    fn body(&self, cx: &mut ViewContext<'_>) -> Result<impl View, ViewError> {
        let value = cx.state("value", String::new);

        Ok(field(value)
            .width(280.0)
            .multiline(true)
            .label("Notes")
            .placeholder("Write a note")
            .disabled(false)
            .height(160.0))
    }
}

fn editor() -> impl View {
    Editor
}

Binding and editing

The control reads its binding during view evaluation, subscribing to model changes. Input events write the edited string back with set_if_changed. Caret movement, selection, and composition handling belong to the adapter. Both modes use the same TextInput host primitive with a multiline property. On web, plain fields use an input or textarea, while text_spans enables a styled contenteditable field. Native configures one retained editor implementation.

Use on_input to observe user edits after the binding updates; programmatic writes do not invoke it. Shared on_key, on_pointer, on_hover, on_focus_change, on_focus_within_change, on_wheel, and on_clipboard preserve Field. Use focused or focused_value to bind direct focus on the editable control before adding a structural wrapper. focus_ring configures its indicator. See Input & Focus.

Fields are single-line by default: Enter submits through on_submit. Add .multiline(true) to allow line breaks: Enter inserts a newline, and Control+Enter or Command+Enter submits. IME composition never submits.

Bind selection to observe or request a range, use text_spans for inline formatting, and paint independent decorations with text_highlight. See Text Editing for UTF-16 offsets, focus, and editing examples.

Typography

Fields support font_size, font_weight, font_family, italic, letter_spacing, word_spacing, line_height, and text_align. They style the editable control and its placeholder while preserving the Field builder. Each method is listed below with its signature. Spacing uses logical pixels and accepts finite negative values.

View-specific reference

field returns Field. These builder methods return Self and configure the actual editable element. Dimensions, paint, labels, and disabled state also preserve Field and target the editable control. Add padding or frame when you want an outer box.

MethodPurpose
multilineAllows line breaks when true. Defaults to false. Multiline fields insert a newline on Enter and submit on Control+Enter or Command+Enter. Changing modes replaces the host control and resets focus, selection, undo history, and composition; the bound value is retained.
styleReplaces the style of the actual editable control, including its internal text and selection box. Import Style from refract::Style. Individual modifiers such as width, font_size, and background also style the editable control and preserve Field.
on_inputObserves user-edited text after updating the binding. Programmatic writes do not emit input; provisional IME composition remains owned by the host. Multiple callbacks run in registration order.
on_submitHandles semantic submission without inserting a newline. A single-line field submits with Enter; a multiline field uses Control+Enter or Command+Enter. IME composition does not submit.
labelSets the semantic label, including image alt text. Also available through ViewExt after styling; it labels the current outer element.
placeholderSets the hint shown when the bound string is empty. A placeholder does not replace an accessible label.
disabledControls whether the element accepts interaction. The default is false; disabling a control does not remove it from layout.
selectionBinds the caret or directional selection without remounting the field. Offsets use UTF-16 code units; None leaves selection under host control. Hosts observe user selection changes and publish the current range before editing commands. See Text Editing.
selection_visibleControls host caret and selection painting; defaults to true. Setting false leaves the logical range, focus, selection observations, and editing behavior intact. Use it when the application paints its own selection; highlights do not change this setting.
font_sizeSets a finite, nonnegative logical font size on the text or editable control.
font_weightRequests a numeric font weight, such as 400 for regular or 700 for bold. The selected face depends on the host's fonts.
italicRequests an italic face. When unavailable, the host synthesizes a slant. Available on Text and Field, including editable text and placeholders. Apply before padding or frame introduces a wrapper.
letter_spacingAdds spacing between letters in logical pixels. Values must be finite; negative values tighten text and zero restores normal spacing. Available on Text and Field. Changing spacing updates text measurement and wrapping.
word_spacingAdds spacing between words in logical pixels. Values must be finite; negative values tighten spacing and zero restores normal spacing. Available on Text and Field, including placeholders.
font_familyRequests a font family on the text or editable control. Font availability and fallback belong to the platform adapter.
line_heightSets line height in logical pixels on the text or editable control. The value must be finite and nonnegative.
text_alignAligns text lines within their available width. Use Start, Center, or End. This is separate from aligning a child inside a stack.
text_spansEnables styled editing with application-owned bold, italic, and underline spans over half-open UTF-16 ranges. Keep calling this with an empty vector when no formatting applies: changing between plain and styled editing remounts the control. The application maintains spans as text changes; see styled editing.
text_highlightDecorates one UTF-16 range with an explicit background color without moving or hiding the host selection. None removes the decoration. On web, enable styled editing with text_spans, even if its vector is empty. See highlights and selection painting.

multiline

Allows line breaks when true. Defaults to false. Multiline fields insert a newline on Enter and submit on Control+Enter or Command+Enter. Changing modes replaces the host control and resets focus, selection, undo history, and composition; the bound value is retained.

Signature
fn multiline(self, value: bool) -> Self

style

Replaces the style of the actual editable control, including its internal text and selection box. Import Style from refract::Style. Individual modifiers such as width, font_size, and background also style the editable control and preserve Field.

Signature
fn style(self, style: Style) -> Self

on_input

Observes user-edited text after updating the binding. Programmatic writes do not emit input; provisional IME composition remains owned by the host. Multiple callbacks run in registration order.

Signature
fn on_input(self, action: impl Fn(&mut ActionContext<'_>, &str) + 'static) -> Self

on_submit

Handles semantic submission without inserting a newline. A single-line field submits with Enter; a multiline field uses Control+Enter or Command+Enter. IME composition does not submit.

Signature
fn on_submit(self, action: impl Fn(&mut ActionContext<'_>) + 'static) -> Self

label

Sets the semantic label, including image alt text. Also available through ViewExt after styling; it labels the current outer element.

Signature
fn label(self, label: impl Into<String>) -> Self

placeholder

Sets the hint shown when the bound string is empty. A placeholder does not replace an accessible label.

Signature
fn placeholder(self, value: impl Into<String>) -> Self

disabled

Controls whether the element accepts interaction. The default is false; disabling a control does not remove it from layout.

Signature
fn disabled(self, value: bool) -> Self

selection

Binds the caret or directional selection without remounting the field. Offsets use UTF-16 code units; None leaves selection under host control. Hosts observe user selection changes and publish the current range before editing commands. See Text Editing.

Signature
fn selection(self, value: Binding<Option<TextSelection>>) -> Self

selection_visible

Controls host caret and selection painting; defaults to true. Setting false leaves the logical range, focus, selection observations, and editing behavior intact. Use it when the application paints its own selection; highlights do not change this setting.

Signature
fn selection_visible(self, visible: bool) -> Self

font_size

Sets a finite, nonnegative logical font size on the text or editable control.

Signature
fn font_size(self, value: f32) -> Self

font_weight

Requests a numeric font weight, such as 400 for regular or 700 for bold. The selected face depends on the host's fonts.

Signature
fn font_weight(self, value: u16) -> Self

italic

Requests an italic face. When unavailable, the host synthesizes a slant. Available on Text and Field, including editable text and placeholders. Apply before padding or frame introduces a wrapper.

Signature
fn italic(self) -> Self

letter_spacing

Adds spacing between letters in logical pixels. Values must be finite; negative values tighten text and zero restores normal spacing. Available on Text and Field. Changing spacing updates text measurement and wrapping.

Signature
fn letter_spacing(self, value: f32) -> Self

word_spacing

Adds spacing between words in logical pixels. Values must be finite; negative values tighten spacing and zero restores normal spacing. Available on Text and Field, including placeholders.

Signature
fn word_spacing(self, value: f32) -> Self

font_family

Requests a font family on the text or editable control. Font availability and fallback belong to the platform adapter.

Signature
fn font_family(self, value: impl Into<String>) -> Self

line_height

Sets line height in logical pixels on the text or editable control. The value must be finite and nonnegative.

Signature
fn line_height(self, value: f32) -> Self

text_align

Aligns text lines within their available width. Use Start, Center, or End. This is separate from aligning a child inside a stack.

Signature
fn text_align(self, value: Alignment) -> Self

text_spans

Enables styled editing with application-owned bold, italic, and underline spans over half-open UTF-16 ranges. Keep calling this with an empty vector when no formatting applies: changing between plain and styled editing remounts the control. The application maintains spans as text changes; see styled editing.

Signature
fn text_spans(self, spans: Vec<TextSpan>) -> Self

text_highlight

Decorates one UTF-16 range with an explicit background color without moving or hiding the host selection. None removes the decoration. On web, enable styled editing with text_spans, even if its vector is empty. See highlights and selection painting.

Signature
fn text_highlight(self, highlight: Option<TextHighlight>) -> Self

Common view modifiers

Dimensions, paint, labels, and disabled state preserve each concrete builder type, so their builder methods can be interleaved. padding and frame return wrappers; configure the inner element before adding those. ViewExt supplies the shared API for composed views.

See View Modifiers for shared methods and modifier ordering. Focus bindings and callbacks preserve concrete builders; see focus modifiers and Input & Focus for requests, observation, and indicators.

Source reference

The implementation is in crates/core/src/view/field.rs. Primitive and layout builders are in crates/core/src/view/primitives.rs. Shared modifiers are in crates/core/src/view/modifiers.rs. Style values and validation are in crates/core/src/style.rs.