似乎不一致是由于竞争状况造成的。单击菜单会使焦点离开输入,从而使菜单关闭。菜单关闭会导致焦点移回输入,使其再次打开。
问题在于菜单通常在菜单有机会发送所选事件之前关闭。
您需要阻止菜单在focusout
上关闭,或者在关闭菜单之前设置较大的超时时间。
input.listen("focusout",() => {
setTimeout(() => {
menu.open = false;
// The timer might need to be fiddled with. Needs to not be too big or too small.
},120);
});
https://codesandbox.io/s/new-brook-o7k1j?file=/src/index.js
这是另一个没有setTimeout计时问题的选项。它使用0超时来模拟setInterval,以便重新排列事件的时间顺序,而不是通过将事件推送到事件队列的末尾。这样应该更安全,而且较不容易出现种族状况问题。
let menuFocused = false;
input.listen("focusin",() => {
if (!menuFocused) menu.open = true;
});
input.listen("click",() => {
menu.open = true;
});
menu.listen("focusin",() => {
menuFocused = true;
});
menu.listen("focusout",() => {
// This interval is to help make sure that input.focusIn doesn't re-open the menu
setTimeout(() => {
menuFocused = false;
},0);
});
input.listen("focusout",() => {
setTimeout(() => {
if (!menuFocused) menu.open = false;
},0);
});
https://codesandbox.io/s/proud-hooks-c9qi5?file=/src/index.js
本文链接:https://www.f2er.com/2308288.html