简体   繁体   English

单击 isClearable 图标(仅限移动设备)时,如何阻止打开反应选择输入下拉菜单?

[英]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.对我有用,希望对你也有用。

check this检查这个

Still not working well at least, it will give you hint how to modify the default behavior.至少仍然不能很好地工作,它会给你提示如何修改默认行为。

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.

相关问题 React-Select:菜单打开时如何旋转下拉指示器 - React-Select : How to rotate dropdown indicator when menu open 如何从 react-select 下拉列表中看不到 select 值和选项(标签名称)? - How do I select value and options (label names) are invisible from react-select dropdown? 我可以在react-select(#react-select)中更改下拉菜单的颜色吗? - Can I change the dropdown menu color in react-select (#react-select)? 如何在反应选择下拉列表中单击按钮添加选择? - How to make a button click add selection in react-select dropdown? 单击任何地方关闭菜单; 单击任何位置时如何使其停止打开 - click anywhere close menu; how do I make it stop opening when clicked anywhere React-Select:如何在选定值 onChange 上更新选定选项下拉列表的默认值? - React-Select: How do I update the selected option dropdown's defaultValue on selected value onChange? 如何在 react-select 的选项中添加图标? - How to add an icon to the options in react-select? react-select 如何在点击 using typescript 时显示选项 - react-select how to display the option when click on using typescript 防止在单击时展开“反应选择” - Prevent expands 'React-select' when click on it 如何添加一个下拉菜单,当您 select 一个选项并单击发送时,它会显示 email 中的选项? - How do I add a dropdown menu that when you select an option and click send it shows the option in the email?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM