# field

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

<a id="construction"></a>

## Construction

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

**Without macros (View trait)**

```rust
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
}
```

**With macros**

```rust
use refract::prelude::*;

#[view]
fn editor(cx: &mut ViewContext<'_>) -> impl View {
    #[state]
    let value: String = String::new();

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

<a id="binding"></a>

## 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](/docs/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](/docs/text-editing) for UTF-16 offsets, focus, and editing examples.

<a id="typography"></a>

## 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.

<a id="view-specific-reference"></a>

## 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`](#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`](#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`](#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`](#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`](#label) | Sets the semantic label, including image alt text. Also available through `ViewExt` after styling; it labels the current outer element. |
| [`placeholder`](#placeholder) | Sets the hint shown when the bound string is empty. A placeholder does not replace an accessible label. |
| [`disabled`](#disabled) | Controls whether the element accepts interaction. The default is `false`; disabling a control does not remove it from layout. |
| [`selection`](#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](/docs/text-editing#selection). |
| [`selection_visible`](#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`](#font-size) | Sets a finite, nonnegative logical font size on the text or editable control. |
| [`font_weight`](#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`](#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`](#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`](#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`](#font-family) | Requests a font family on the text or editable control. Font availability and fallback belong to the platform adapter. |
| [`line_height`](#line-height) | Sets line height in logical pixels on the text or editable control. The value must be finite and nonnegative. |
| [`text_align`](#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`](#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](/docs/text-editing#formatting). |
| [`text_highlight`](#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](/docs/text-editing#highlights). |

<a id="multiline"></a>

### `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.

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

<a id="style"></a>

### `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`.

```rust
fn style(self, style: Style) -> Self
```

<a id="on-input"></a>

### `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.

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

<a id="on-submit"></a>

### `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.

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

<a id="label"></a>

### `label`

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

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

<a id="placeholder"></a>

### `placeholder`

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

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

<a id="disabled"></a>

### `disabled`

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

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

<a id="selection"></a>

### `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](/docs/text-editing#selection).

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

<a id="selection-visible"></a>

### `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.

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

<a id="font-size"></a>

### `font_size`

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

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

<a id="font-weight"></a>

### `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.

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

<a id="italic"></a>

### `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.

```rust
fn italic(self) -> Self
```

<a id="letter-spacing"></a>

### `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.

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

<a id="word-spacing"></a>

### `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.

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

<a id="font-family"></a>

### `font_family`

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

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

<a id="line-height"></a>

### `line_height`

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

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

<a id="text-align"></a>

### `text_align`

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

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

<a id="text-spans"></a>

### `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](/docs/text-editing#formatting).

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

<a id="text-highlight"></a>

### `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](/docs/text-editing#highlights).

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

<a id="common-modifiers"></a>

## 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](/docs/view-modifiers) for shared methods and modifier ordering. Focus bindings and callbacks preserve concrete builders; see [focus modifiers](/docs/view-modifiers#focus) and [Input & Focus](/docs/input-focus) for requests, observation, and indicators.

<a id="source"></a>

## 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`.
