Skip to content
Form Input

Input

A single-line text field.

On this page

Examples

Variants and icons

Searching for:

Razor
<div class="flex w-full max-w-sm flex-col gap-3">
    @foreach (var variant in Enum.GetValues<FieldVariant>())
    {
        <KlyInput Variant="variant" Placeholder="@($"{variant} input")" aria-label="@($"{variant} input")" />
    }
    <KlyInput @bind-Value="_search" Immediate Type="InputType.Search" Placeholder="Search…" aria-label="Search">
        <LeadingIcon><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><circle cx="11" cy="11" r="7" /><path d="m20 20-3.5-3.5" /></svg></LeadingIcon>
    </KlyInput>
    <p class="text-sm text-(--klyroo-text-muted)">Searching for: @_search</p>
</div>

@code {
    private string? _search;
}

Debounce and keyboard

Debounce reports once per pause, Enter reports at once and Escape clears (ClearOnEscape). The browser does the waiting: Server mode makes one round trip per pause, not per key. Reset calls SetValueAsync, and the form validates after each pause.

Value:

Reports: 0

Plain:

Escape only:

Razor
@using System.ComponentModel.DataAnnotations
<div class="flex w-full max-w-sm flex-col gap-3">
    <KlyInput @ref="_input" Value="@_query" ValueChanged="OnQuery" Debounce="400" ClearOnEscape Type="InputType.Search" Placeholder="Type, then pause" aria-label="Debounced search" data-testid="debounced" />
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="debounced-state">Value: @_query</p>
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="debounced-count">Reports: @_reports</p>
    <div class="flex gap-2">
        @* Keeps the focus in the field: the button replaces the text, even text still waiting for its pause. *@
        <button type="button" class="rounded-md border px-3 py-1 text-sm" data-testid="reset" @onmousedown:preventDefault="true" @onclick="Reset">Reset</button>
    </div>
    <KlyInput Value="@_plain" ValueChanged="OnPlain" aria-label="Plain input" Placeholder="No debounce: reported on change" data-testid="plain" />
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="plain-state">Plain: @_plain</p>
    <KlyInput Value="@_escOnly" ValueChanged="OnEscOnly" ClearOnEscape aria-label="Escape only" Placeholder="ClearOnEscape, no Debounce" data-testid="escape-only" />
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="escape-only-state">Escape only: @_escOnly</p>
    <button type="button" class="rounded-md border px-3 py-1 text-sm" data-testid="escape-only-set" @onclick="@(() => _escOnly = "from parent")">Set from parent</button>
    <EditForm Model="_account" class="flex flex-col gap-2">
        <DataAnnotationsValidator />
        <KlyInput @bind-Value="_account.Email" Debounce="300" Type="InputType.Text" aria-label="Email" Placeholder="Validated after each pause" data-testid="email" />
        <ValidationMessage For="() => _account.Email" />
    </EditForm>
</div>

@code {
    private KlyInput? _input;
    private string? _query;
    private string? _plain;
    private string? _escOnly;
    private int _reports;
    private readonly Account _account = new();

    private void OnQuery(string? value)
    {
        _query = value;
        _reports++;
    }

    private void OnEscOnly(string? value) => _escOnly = value;

    private void OnPlain(string? value) => _plain = value;

    private async Task Reset()
    {
        if (_input is not null) await _input.SetValueAsync("");
    }

    private sealed class Account
    {
        [EmailAddress(ErrorMessage = "Not an email address.")]
        public string? Email { get; set; }
    }
}

Escape in a dialog

The Escape that clears text is consumed; Escape on an empty field closes the dialog.

Search

Escape clears the text first, then closes the dialog.

Dialog: closed, text:

Escape keydowns seen by the page: 0

Razor
<div @onkeydown="OnKeyDown" class="flex w-full max-w-sm flex-col gap-3">
    <KlyModal @bind-Open="_open" Title="Search" Description="Escape clears the text first, then closes the dialog.">
        <ChildContent><KlyButton Label="Open search" Variant="ButtonVariant.Outline" data-testid="escape-trigger" /></ChildContent>
        <Body>
            <div class="flex flex-col gap-3">
                <KlyInput @bind-Value="_text" Debounce="200" ClearOnEscape Type="InputType.Search" aria-label="Search in dialog" data-testid="escape-input" />
                <KlyInput Value="locked" ClearOnEscape readonly aria-label="Read-only" data-testid="escape-readonly" />
            </div>
        </Body>
    </KlyModal>
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="escape-state">Dialog: @(_open ? "open" : "closed"), text: @_text</p>
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="escape-keys">Escape keydowns seen by the page: @_escapes</p>
</div>

@code {
    private bool _open;
    private string? _text;
    private int _escapes;

    private void OnKeyDown(KeyboardEventArgs e)
    {
        if (e.Key == "Escape") _escapes++;
    }
}

API

KlyInput

Single-line text input with a variant, a size and optional leading and trailing icons. It binds to a string and takes part in EditForm validation.

Parameters

Name Type Default Description
Class string Classes added to the root element, merged last so they win over the theme.
ClearOnEscape bool false Escape empties the field and reports the empty value (immediately, whatever Debounce says). On an already empty field Escape is left alone, so it still closes a modal or popover around the field. The browser does the work, in every render mode. The Escape that clears text is consumed: no other handler of the page, Blazor's @onkeydown included, sees it. A read-only field is not cleared.
Color KlyColor "primary" Semantic color of the control: a built-in KlyColor or the name of a color you defined. When not set, Color applies, else primary. Values: primary, secondary, success, info, warning, error, neutral.
Debounce int? Milliseconds of quiet after the last keystroke before the value is reported, in the browser: typing a word raises one ValueChanged per pause instead of one per key (a round trip each in Server mode). Enter and leaving the field report at once, and nothing is reported in the middle of an IME composition. A render that arrives with an older value does not overwrite what was typed after it. null (the default) keeps the plain behavior. Debounce wins over when both are set. Inside an EditForm the field is validated each time a value is reported, so after each pause (or Enter, or leaving the field), not on every key. Static rendering has no .NET to report to, so there the attribute only does what the browser can do alone. While text is waiting to be reported, a value written into the field by the page's own script, or by a render that started before the last report, is ignored: typed text wins. To replace the text on purpose (a "reset" button), call .
Disabled bool false Disables the control: it cannot be focused or changed and is not submitted.
Id string The id of the native control. Defaults to the one of the enclosing KlyFormField , else a generated one.
Immediate bool false Update the bound value on every keystroke instead of on change. Costs a round trip per key in Server mode.
Name string Form field name outside an EditForm . Inside one, the name comes from the bound expression.
Placeholder string Hint shown while the field is empty.
Required bool false Marks the control as required, for the browser and for assistive technology.
Size KlySize? Defaults to the size of the enclosing KlyFormField , or medium. Values: Xs, Sm, Md, Lg, Xl.
Type InputType InputType.Text The type attribute: Text (the default), Email , Password , Search … Values: Text, Email, Password, Search, Tel, Url, Number, Date, Time, DateTimeLocal, Month, Week.
Ui InputUi Per-instance class overrides of the slots of the input, see InputUi . They are merged after the theme and the Class parameter wins on the root slot.
Value (@bind-Value) string The value of the control. Bind it with @bind-Value .
ValueExpression Expression<Func<string>> The expression of the bound field. The compiler sets it with @bind-Value , and validation uses it to find the field.
Variant FieldVariant FieldVariant.Outline Look of the field: Outline , Soft , Subtle , Ghost or None . Values: Outline, Soft, Subtle, Ghost, None.

Slots

Name Type Description
LeadingIcon RenderFragment Icon at the start of the field, as markup such as a KlyIcon .
TrailingIcon RenderFragment Icon at the end of the field, as markup such as a KlyIcon .

Ui slots

Ui takes a InputUi with one class string per slot, merged after the theme.

Slot Styles
Root Classes merged into the root slot of KlyInput , after the theme and the app configuration.
Base Classes merged into the base slot of KlyInput , after the theme and the app configuration.
Leading Classes merged into the leading slot of KlyInput , after the theme and the app configuration.
LeadingIcon Classes merged into the leadingIcon slot of KlyInput , after the theme and the app configuration.
Trailing Classes merged into the trailing slot of KlyInput , after the theme and the app configuration.
TrailingIcon Classes merged into the trailingIcon slot of KlyInput , after the theme and the app configuration.