@code {
///
/// Current persisted value shown when idle. Bind to the source-of-truth
/// property; never mutate it directly — wait for OnSave to confirm.
///
[Parameter, EditorRequired] public string Value { get; set; } = "";
///
/// Save handler — called when the user commits (Enter / Tab / blur). The
/// component awaits the returned Task and shows a spinner meanwhile. If
/// the task throws, the input restores to the previous value and the
/// component surfaces an inline error state.
///
[Parameter] public EventCallback OnSave { get; set; }
///
/// Placeholder shown when Value is empty. Mirrors HTML <input
/// placeholder> semantics in edit mode AND a muted "click to add" affordance
/// in idle mode.
///
[Parameter] public string Placeholder { get; set; } = "Click to edit";
///
/// Maximum length for the input. Default 200 — most identity-name fields
/// in our schemas cap below that.
///
[Parameter] public int MaxLength { get; set; } = 200;
///
/// When true, the field renders read-only — no pencil icon, no click handler.
///
[Parameter] public bool Disabled { get; set; }
///
/// Optional validation predicate — runs synchronously before OnSave is
/// dispatched. Return null to accept, or an error string to reject (the
/// input stays open with no save attempt).
///
[Parameter] public Func? Validate { get; set; }
private bool _editing;
private bool _saving;
private string _draft = "";
private string _previousValue = "";
private ElementReference _inputRef;
private async Task BeginEdit()
{
if (Disabled || _editing) return;
_previousValue = Value;
_draft = Value;
_editing = true;
StateHasChanged();
// Focus + select on the next render cycle. The element is mounted by
// then; FocusAsync is a JS interop call so we can't issue it before
// the input is in the DOM.
await Task.Yield();
try { await _inputRef.FocusAsync(); }
catch { /* harmless — focus is best-effort. */ }
}
private async Task OnKeyDownAsync(KeyboardEventArgs e)
{
if (_saving) return;
switch (e.Key)
{
case "Enter":
case "Tab":
await CommitAsync();
break;
case "Escape":
Cancel();
break;
}
}
private async Task OnBlurAsync()
{
// Blur fires after Enter/Tab keydown handlers already kicked off Commit.
// Guard against double-save when the field is already saving / closed.
if (_saving || !_editing) return;
await CommitAsync();
}
private async Task CommitAsync()
{
if (string.Equals(_draft, _previousValue, StringComparison.Ordinal))
{
// No change — close cleanly without invoking the save callback.
_editing = false;
StateHasChanged();
return;
}
if (Validate is not null && Validate(_draft) is { } _)
{
// Validation rejected the draft. Stay in editing mode so the user
// can correct. We don't surface the error message ourselves yet —
// callers that want richer UX can wrap UiInlineEdit and render
// their own error label adjacent to it (RFC for batch 2).
return;
}
_saving = true;
StateHasChanged();
try
{
await OnSave.InvokeAsync(_draft);
// Caller is expected to update Value via its own state mutation
// (typically by binding to a model property the parent owns).
_editing = false;
}
catch
{
// Restore previous value on save failure so the field doesn't lie
// about persistence. Caller is responsible for surfacing the error
// (e.g. via ToastService or an inline alert).
_draft = _previousValue;
_editing = false;
}
finally
{
_saving = false;
StateHasChanged();
}
}
private void Cancel()
{
_draft = _previousValue;
_editing = false;
StateHasChanged();
}
}