【发布时间】:2021-12-02 10:51:35
【问题描述】:
我在 Blazor 中有一个自定义对话框。它基本上工作正常,但有一些缺陷:
- 当使用键盘焦点在底层页面上的控件之间而不是对话框内部跳转时。
- 在键盘上单击 Esc 或 Enter 时不会触发 keydown 事件。
缺少什么?
<div class="custom-modal-overlay fade @showClass" style="@style" aria-modal="@Show" aria-hidden="@(!Show)" aria-labelledby="Dialog" @onclick="HandleCancel" @onkeydown="OverlayKeyDown">
<div class="custom-modal-dialog" @onclick:stopPropagation="true">
<div class="custom-modal-header">
<h1>@Title</h1>
@if (!ShowCancel)
{
<div class="cancel-icon" @onclick="HandleCancel">
<span class="material-icons-outlined cancel-icon" role="img" aria-label="Cancel">clear</span>
</div>
}
</div>
<div class="custom-modal-content">
@ChildContent
</div>
<div class="custom-modal-footer">
@if (ShowCancel)
{
<button type="button" class="btn btn-sm btn-outline-secondary" @onclick="HandleCancel">Cancel</button>
}
<button type="submit" class="btn btn-sm btn-primary" @onclick="HandleConfirm">OK</button>
</div>
</div>
</div>
@code {
[Parameter]
public bool ShowCancel { get; set; } = true;
[Parameter]
public bool Show { get; set; }
[Parameter]
public EventCallback<bool> ShowChanged { get; set; }
[Parameter]
public string Title { get; set; }
[Parameter]
public RenderFragment ChildContent { get; set; }
[Parameter]
public EventCallback OnCancel { get; set; }
[Parameter]
public EventCallback OnConfirm { get; set; }
private string showClass => Show ? "show" : string.Empty;
private string style => Show ? "display: block;" : "display: none;";
private void OverlayKeyDown(KeyboardEventArgs e)
{
// TODO: Detect what key is hit and take proper action
var key = e.Key;
Show = false;
ShowChanged.InvokeAsync(Show);
OnCancel.InvokeAsync();
StateHasChanged();
}
private void HandleCancel()
{
Show = false;
ShowChanged.InvokeAsync(Show);
OnCancel.InvokeAsync();
}
private void HandleConfirm()
{
Show = false;
ShowChanged.InvokeAsync(Show);
OnConfirm.InvokeAsync();
}
}
【问题讨论】:
-
将 tabindex="-1" 添加到你有 onkeydown 的根元素。将 ref 指令添加到元素并调用 elementReference.FocusAsync()
-
很好的答案!用 Enter 和 Esacpe 键解决了最重要的部分。但是 tabindex 仍然不起作用。
标签: modal-dialog blazor keyevent