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::*.
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.
| Method | Purpose |
|---|---|
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. |
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. |
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. |
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. |
label | Sets the semantic label, including image alt text. Also available through ViewExt after styling; it labels the current outer element. |
placeholder | Sets the hint shown when the bound string is empty. A placeholder does not replace an accessible label. |
disabled | Controls whether the element accepts interaction. The default is false; disabling a control does not remove it from layout. |
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. |
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. |
font_size | Sets a finite, nonnegative logical font size on the text or editable control. |
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. |
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. |
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. |
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. |
font_family | Requests a font family on the text or editable control. Font availability and fallback belong to the platform adapter. |
line_height | Sets line height in logical pixels on the text or editable control. The value must be finite and nonnegative. |
text_align | Aligns text lines within their available width. Use Start, Center, or End. This is separate from aligning a child inside a stack. |
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. |
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. |
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.
fn multiline(self, value: bool) -> Selfstyle
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.
fn style(self, style: Style) -> Selfon_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.
fn on_input(self, action: impl Fn(&mut ActionContext<'_>, &str) + 'static) -> Selfon_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.
fn on_submit(self, action: impl Fn(&mut ActionContext<'_>) + 'static) -> Selflabel
Sets the semantic label, including image alt text. Also available through ViewExt after styling; it labels the current outer element.
fn label(self, label: impl Into<String>) -> Selfplaceholder
Sets the hint shown when the bound string is empty. A placeholder does not replace an accessible label.
fn placeholder(self, value: impl Into<String>) -> Selfdisabled
Controls whether the element accepts interaction. The default is false; disabling a control does not remove it from layout.
fn disabled(self, value: bool) -> Selfselection
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.
fn selection(self, value: Binding<Option<TextSelection>>) -> Selfselection_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.
fn selection_visible(self, visible: bool) -> Selffont_size
Sets a finite, nonnegative logical font size on the text or editable control.
fn font_size(self, value: f32) -> Selffont_weight
Requests a numeric font weight, such as 400 for regular or 700 for bold. The selected face depends on the host's fonts.
fn font_weight(self, value: u16) -> Selfitalic
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.
fn italic(self) -> Selfletter_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.
fn letter_spacing(self, value: f32) -> Selfword_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.
fn word_spacing(self, value: f32) -> Selffont_family
Requests a font family on the text or editable control. Font availability and fallback belong to the platform adapter.
fn font_family(self, value: impl Into<String>) -> Selfline_height
Sets line height in logical pixels on the text or editable control. The value must be finite and nonnegative.
fn line_height(self, value: f32) -> Selftext_align
Aligns text lines within their available width. Use Start, Center, or End. This is separate from aligning a child inside a stack.
fn text_align(self, value: Alignment) -> Selftext_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.
fn text_spans(self, spans: Vec<TextSpan>) -> Selftext_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.
fn text_highlight(self, highlight: Option<TextHighlight>) -> SelfCommon 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.