FieldController interface

Module @runic-artifex/views in @runic-artifex/views 0.23.0

interface FieldController<TState, TClient, TValue>

A framework-neutral binding of one View member to an input (@runic-artifex/angular, react, vue and svelte wrap it as useField / injectField). It holds a draft while writes are in flight, so typing never loses characters; sends nothing during IME composition; reads checked-write receipts; follows the member's validation; and shows the View's value, including any normalization, once the last write settled.

Properties

current

readonly current: FieldState<TValue>;

No documentation.

field

readonly field: {
    readonly kind: BridgeFieldKind | undefined;
    readonly nullable: boolean;
    readonly integer: boolean;
    readonly choices: readonly TValue[] | undefined;
    readonly offset: boolean;
    readonly constraints: BridgeFieldConstraints | undefined;
};

The definition's kind, nullable, integer and choices, for bindings that wire an element.

Methods

format

format(value: TValue): string;

The text the field shows for value when no draft exists: the format option, or the kind's form.

subscribe

subscribe(listener: () => void): () => void;

Calls listener after each change of current, until the returned function is called.

setSource

setSource(source: FieldSource<TState, TClient> | undefined): void;

Sets the View the field reads and writes, or undefined while none is connected. Call it with each new state. setSource does not notify subscribers; read current after calling it.

input

input(value: TValue): void;

The user changed the value.

inputText

inputText(text: string): void;

The user changed the field's text, such as on an input event: parsed for the field's kind and written like input, or kept as the draft with a FieldError of kind invalid-text when it does not parse.

compositionStart

compositionStart(): void;

An IME composition started: values are kept as the draft but not written until it ends.

compositionEnd

compositionEnd(text: string): void;

The IME composition ended with text, which is then written like inputText.

commit

commit(): Promise<FieldState<TValue>>;

The user is done with the field, such as when it loses focus: with commit: "pause" the draft is written now. The text typed is shown formatted again. Resolves with current once no write of the field is in flight or waiting, so a command that depends on the field, such as Save, can run after it and skip a failed write: if ((await title.commit()).error === undefined) await editor.save(). It never rejects; a failed write is the state's error. A write that waits for retry() after a conflict, or for a client, does not delay it.

retry

retry(): void;

Writes the draft again after a failed write; after a conflict, over the producer's value.

reset

reset(): void;

Drops the draft, the error and a conflict and shows the View's value. Writes in flight still complete.

dispose

dispose(): void;

Stops notifying. A value that waits to be written (coalescing, a pause) is still written.