我想通过单击按钮打开 react 选择菜单。
这是代码沙盒的链接
https://codesandbox.io/s/material-demo-kssrb
我知道一个 react 选择属性“menuIsOpen”,这个 Prop 的缺点是菜单总是打开的,因此默认关闭行为会丢失。
任何帮助表示赞赏。
最佳答案
您可以尝试使用存储对组件的引用,调用 .focus()
在上面。
并传递给 React Select Prop openMenuOnFocus
.
代码和盒子上的示例:https://codesandbox.io/s/react-select-hooks-forked-qol5m
简短的本质:
const selectRef = useRef();
const onClick = () => {
selectRef.current.focus();
};
<Dropdown
openMenuOnFocus={true}
ref={selectRef}
/>
关于reactjs - 如何实用地打开 react 选择菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60257724/