Browse Source

Merge pull request #8416 from HappenHq/fix-blazor-textbox-input-focus

[Blazor] Prevent input focus leaving the TextBox after first click
pull/8440/head
Dan Walmsley 4 years ago
committed by GitHub
parent
commit
53c0f7f016
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/Web/Avalonia.Web.Blazor/AvaloniaView.razor
  2. 13
      src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs

3
src/Web/Avalonia.Web.Blazor/AvaloniaView.razor

@ -5,7 +5,8 @@
@onpointerdown="OnPointerDown" @onpointerdown="OnPointerDown"
@onpointerup="OnPointerUp" @onpointerup="OnPointerUp"
@onpointermove="OnPointerMove" @onpointermove="OnPointerMove"
@onpointercancel="OnPointerCancel"> @onpointercancel="OnPointerCancel"
@onfocus="OnFocus">
<canvas id="htmlCanvas" @ref="_htmlCanvas" @attributes="AdditionalAttributes"/> <canvas id="htmlCanvas" @ref="_htmlCanvas" @attributes="AdditionalAttributes"/>

13
src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs

@ -37,6 +37,7 @@ namespace Avalonia.Web.Blazor
private const SKColorType ColorType = SKColorType.Rgba8888; private const SKColorType ColorType = SKColorType.Rgba8888;
private bool _initialised; private bool _initialised;
private bool _inputElementFocused;
[Inject] private IJSRuntime Js { get; set; } = null!; [Inject] private IJSRuntime Js { get; set; } = null!;
@ -221,6 +222,16 @@ namespace Avalonia.Web.Blazor
_topLevelImpl.RawKeyboardEvent(RawKeyEventType.KeyUp, e.Code, e.Key, GetModifiers(e)); _topLevelImpl.RawKeyboardEvent(RawKeyEventType.KeyUp, e.Code, e.Key, GetModifiers(e));
} }
private void OnFocus(FocusEventArgs e)
{
// if focus has unexpectedly moved from the input element to the container element,
// shift it back to the input element
if (_inputElementFocused && _inputHelper is not null)
{
_inputHelper.Focus();
}
}
private void OnInput(ChangeEventArgs e) private void OnInput(ChangeEventArgs e)
{ {
if (e.Value != null) if (e.Value != null)
@ -374,10 +385,12 @@ namespace Avalonia.Web.Blazor
if (active) if (active)
{ {
_inputHelper.Show(); _inputHelper.Show();
_inputElementFocused = true;
_inputHelper.Focus(); _inputHelper.Focus();
} }
else else
{ {
_inputElementFocused = false;
_inputHelper.Hide(); _inputHelper.Hide();
} }
} }

Loading…
Cancel
Save