[英]How do I stop the react-select input dropdown menu from opening when click the isClearable icon (mobile only)?
I made this very simple codesandbox to show my problem.我制作了这个非常简单的代码框来显示我的问题。
When I click the 'x' icon that appears after adding isClearable
to the Select component, it both clears the selection AND it also opens the dropdown.当我单击将
isClearable
添加到 Select 组件后出现的“x”图标时,它既清除了选择,也打开了下拉列表。 I do not want to open the dropdown menu, but I cannot figure out how to stop it.我不想打开下拉菜单,但我不知道如何停止它。 Any ideas?
有任何想法吗?
This problem only exists on mobile browsers.此问题仅存在于移动浏览器上。 The clear function works as expected on desktop browsers.
清晰的 function 在桌面浏览器上按预期工作。 To see the codesandbox on mobile, open Chrome devtools and click the "toggle device toolbar" button.
要在移动设备上查看代码框,请打开 Chrome 开发工具并单击“切换设备工具栏”按钮。
EDIT: Since I haven't received a response yet (22 hours), here are some more details.编辑:由于我还没有收到回复(22 小时),这里有更多细节。 This discussion most closely resembles my issue.
这个讨论最接近我的问题。 They discuss the cause may be due to the react-tap-event-plugin.
他们讨论的原因可能是由于 react-tap-event-plugin。
I have tried using the react-fastclick package since I thought it may be a delayed tap (long-pressing in mobile does not open menu).我曾尝试使用 react-fastclick package,因为我认为它可能是延迟点击(在移动设备中长按不会打开菜单)。
I have tried adding a div around the Select
component with the attribute onClick={e => { e.preventDefault(); e.stopPropagation(); }}
我尝试在
Select
组件周围添加一个 div,其属性为onClick={e => { e.preventDefault(); e.stopPropagation(); }}
onClick={e => { e.preventDefault(); e.stopPropagation(); }}
I have tried adding a dummy div, and calling document.getElementById("dummy").focus()
at the end of the onChange
event in the Select
component.我尝试添加一个虚拟 div,并在
Select
组件的onChange
事件结束时调用document.getElementById("dummy").focus()
。
EDIT 2: If you add an event break-point for the touchend
event, click the 'x' causing the script to pause, then press F8 (resume script), the menu will not open.编辑 2:如果为
touchend
事件添加事件断点,单击“x”导致脚本暂停,然后按 F8(恢复脚本),菜单将不会打开。 There must be some sort of delayed tap happening.一定有某种延迟点击发生。
I have a hacky solution.我有一个 hacky 解决方案。 I set
pointer-events: null
in the css for 50 milliseconds at the end of my onChange
event function attribute within the Select
component. I set
pointer-events: null
in the css for 50 milliseconds at the end of my onChange
event function attribute within the Select
component. If someone knows a less hacky way of making this work, please let me know!如果有人知道使这项工作更简单的方法,请告诉我!
temporarilyDisable = () => {
const elementToDisable = document.getElementById('selectComponentID');
const wait = ms => new Promise((r, j)=>setTimeout(r, ms))
elementToDisable.setAttribute("style", "pointer-events: none")
let prom = wait(50).then(() => elementToDisable.setAttribute("style", "pointer-events: all"))
}
Try to add this property to the Select component:尝试将此属性添加到 Select 组件中:
openMenuOnClick={false}
Works for me, hope it does to you too.对我有用,希望对你也有用。
Assuming you got some code like this假设你有这样的代码
<Select
isClearable
options={options}
//..
/>
Then you have the same bug I had.然后你有和我一样的错误。 The "isClearable" flag needs a value.
“isClearable”标志需要一个值。
<Select
isClearable={true}
options={options}
//..
/>
Hope it helps.希望能帮助到你。 ♥
♥
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.