Skip to content

TextInput

final class Phui\Components\TextInput

A single-line editor with selection, word motions and clipboard support. The value scrolls horizontally to keep the cursor visible, and newlines are stripped out of anything pasted in. Takes focus.

A single-line editor holding the value "phui/screen"

php
use Phui\Components\TextInput;
use Phui\Events\EmitEvent;

TextInput::mount()
    ->props(
        value: $this->query,
        placeholder: 'Search…',
        invalid: $this->query !== '' && ! $this->isValid,
    )
    ->on('change', fn (EmitEvent $e) => $this->query = $e->data);

Props

PropTypeDefaultDescription
maxCharsint1000Most characters the value may hold.
invalidboolfalseForces the invalid colour on.
maxWidth?intnullCaps the visible width; the value scrolls within it. Unbounded when null.
placeholderstring'Type here...'Shown while the value is empty and the field is unfocused.
markerstring'|'The marker drawn before the value.

Reaching maxChars shows the invalid colour, the same as setting invalid. Further typing and the overflow of a paste are dropped.

Models

The component holds this and emits on every change. Pass it as a prop to take ownership, and handle its emit — see binding a value both ways.

ModelTypeDefaultEmits
valuestring''change

Emits

EventPayloadEmitted when
changestringThe value is edited.

Input

InputAction
Any characterInsert at the cursor, replacing the selection
Move by one character
Alt + Move by one word
Home EndMove to the start or end of the value
Shift + any motionExtend the selection instead of moving
BackspaceDelete back, or the selection
Alt + BackspaceDelete the word before the cursor
Delete Ctrl+dDelete forward, or the selection
Ctrl+aSelect all
Ctrl+yCopy the selection to the system clipboard
Ctrl+xCut the selection
Ctrl+wDelete the word before the cursor, or the selection
Ctrl+kDelete to the end of the value
Ctrl+uDelete to the start of the value
PasteInsert the pasted text, replacing the selection
Left clickMove the cursor to the clicked position

Word motions treat spaces as boundaries.

Colours

Phui\Components\Colours\TextInputColours

ColourPalette defaultUsed for
texttextThe value.
markermutedThe marker at rest.
activeaccentThe marker and cursor while focused.
invaliddangerThe marker and cursor while invalid.
placeholdermutedThe placeholder text.
selectionsurfaceThe background behind the selection.

Released under the MIT License.