简体   繁体   English

onfocusout 防止下拉菜单项单击 Blazor

[英]onfocusout preventing dropdown menu item to click in Blazor

Loving Blazor.爱 Blazor。

I want a dropdown menu to close when I click away.当我点击离开时,我希望关闭一个下拉菜单。

I've added onfocusout to the surrounding div to set the showDropdown toggle to false, and this makes the menu close.我已将 onfocusout 添加到周围的 div 以将 showDropdown 切换设置为 false,这会使菜单关闭。

However, this then disables the menu items onclick events (eg to run UpdateDate method below) because, it seems, the onfocusout event is activating first.但是,这会禁用菜单项 onclick 事件(例如运行下面的 UpdateDate 方法),因为似乎首先激活了 onfocusout 事件。

<h3>@date</h3>
<div type="button" class="race-info-item" @onfocusout="e => showDropdown = false">

<button @onclick="e => showDropdown = !showDropdown">Show dropdown</button>

<div class="dropdown-menu select-class-dropdown @(showDropdown ? "show" : "")" >
    <div class="button" @onclick="UpdateDate">Update date</div>
    <div class="button" @onclick="e => showDropdown = false">Cancel</div>
</div>

</div>

private bool showDropdown = false;
private DateTime date = DateTime.Now;

private void UpdateDate()
{
    date = DateTime.Now;
    showDropdown = false;
}

private async Task OutFocus()
{
    await Task.Delay(100);
    showDropdown = false;
}

Swapping in the OutFocus() method above for onfocusout does seem to fix the problem, but introducing a threaded delay feels like a fudge to me and not very elegant.将上面的 OutFocus() 方法换成 onfocusout 似乎确实可以解决问题,但是引入线程延迟对我来说就像是一种软糖,而且不是很优雅。

I've also tried adding @onclick:stopPropagation="true" for the menu @onclick events, but that doesn't work either - looks like the onfocusout event still gets there first.我还尝试为菜单 @onclick 事件添加 @onclick:stopPropagation="true" ,但这也不起作用 - 看起来 onfocusout 事件仍然首先到达那里。

Hard to believe this is a difficult problem to fix and that there isn't a straight up logic solution.很难相信这是一个难以解决的问题,并且没有直接的逻辑解决方案。 Don't know javascript (hence why I'm working with Blazor) so don't want to investigate JS interop.不知道 javascript(因此我使用 Blazor 的原因)所以不想研究 JS 互操作。

Thanks in advance for any suggestions!在此先感谢您的任何建议!

After spending aa whole day on this problem and then posting the question, I think I just stumbled across the answer at https://stackoverflow.com/a/47944833/15410916在这个问题上花了一整天然后发布问题后,我想我只是偶然发现了https://stackoverflow.com/a/47944833/15410916的答案

Using onmousedown for the menu items instead of onclick.对菜单项使用 onmousedown 而不是 onclick。 Now seems to work.现在似乎工作了。

<h3>@date</h3>

<div type="button" class="race-info-item" @onfocusout="e => showDropdown = false">

<button @onclick="e => showDropdown = !showDropdown">Show dropdown</button>

<div class="dropdown-menu select-class-dropdown @(showDropdown ? "show" : "")" >
    <div class="button" @onmousedown="UpdateDate">Update date</div>
    <div class="button" @onmousedown="e => showDropdown = false">Cancel</div>
</div>

</div>

Would welcome any views on whether there is anything negative in doing this.欢迎就这样做是否有负面影响提出任何意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM