FancyTextBox.razor (1047B)
1 @inject IJSRuntime JsRuntime 2 @if (isVisible) 3 { 4 <input autofocus @bind="Value" @onfocusout="() => { isVisible = false; ValueChanged.InvokeAsync(Value); }" @ref="elementToFocus"/> 5 } 6 else 7 { 8 <span tabindex="0" style="border-bottom: #ccc solid 1px; height: 1.4em; display: inline-block; @(string.IsNullOrEmpty(Value) ? "min-width: 50px;" : "")" @onfocusin="() => isVisible = true">@(Formatter?.Invoke(Value) ?? (IsPassword ? string.Join("", Value.Select(x=>'*')) : Value))</span> 9 } 10 11 @code { 12 13 [Parameter] 14 public string Value { get; set; } 15 16 [Parameter] 17 public bool IsPassword { get; set; } = false; 18 19 [Parameter] 20 public EventCallback<string> ValueChanged { get; set; } 21 22 [Parameter] 23 public Func<string?, string>? Formatter { get; set; } 24 25 26 private bool isVisible { get; set; } = false; 27 28 private ElementReference elementToFocus; 29 30 protected override async Task OnAfterRenderAsync(bool firstRender) 31 { 32 await JsRuntime.InvokeVoidAsync("BlazorFocusElement", elementToFocus); 33 } 34 35 }