单击下拉列表项内的复选框时如何避免下拉菜单隐藏?
How to avoid dropdown menu hiding while click on checkbox inside the dropdown list items?
需要避免在下拉列表项中单击 'checkbox' 时隐藏下拉菜单。当在下拉菜单和下拉切换按钮区域外单击时,下拉菜单应该隐藏,所选项目的值也应该加载到下拉切换文本区域。请帮忙,我正在使用 react-bootstrap dropdown
<Dropdown className="dropdown-groove">
<Dropdown.Toggle variant="outline-secondary " id="dropdown-basic">
Select...
<label className="dropdown-label">Dropdown label</label>
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item href="#/action-1">
{" "}
<Form.Check
className="checkbox-groove"
inline
label="Unselected"
name="group1"
type="checkbox"
/>{" "}
</Dropdown.Item>
<Dropdown.Item href="#/action-2">List Item 2</Dropdown.Item>
<Dropdown.Item href="#/action-3">List Item 3</Dropdown.Item>
<Dropdown.Item href="#/action-4">List Item 4</Dropdown.Item>
<Dropdown.Item href="#/action-5">List Item 5</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
在<Form.Check>...</Form.Check>
中添加onClick={(e) => e.stopPropagation()}
。
e.stopPropagation()
在标签中将帮助此标签防止来自父级的 onClick 事件。
代码在这里:https://codesandbox.io/s/dropdown-with-checkbox-forked-88h9m6?file=/src/App.js
import "./styles.scss";
import "bootstrap/dist/css/bootstrap.min.css";
import { Dropdown, Form } from "react-bootstrap";
export default function App() {
return (
<div className="App">
<Dropdown className="dropdown-groove">
<Dropdown.Toggle variant="outline-secondary " id="dropdown-basic">
Select...
<label className="dropdown-label">Dropdown label</label>
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item href="#/action-1">
{" "}
<Form.Check
onClick={(e) => e.stopPropagation()} //<=== Add here
className="checkbox-groove"
inline
label="Unselected"
name="group1"
type="checkbox"
/>{" "}
</Dropdown.Item>
<Dropdown.Item href="#/action-2">List Item 2</Dropdown.Item>
<Dropdown.Item href="#/action-3">List Item 3</Dropdown.Item>
<Dropdown.Item href="#/action-4">List Item 4</Dropdown.Item>
<Dropdown.Item href="#/action-5">List Item 5</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
</div>
);
}
需要避免在下拉列表项中单击 'checkbox' 时隐藏下拉菜单。当在下拉菜单和下拉切换按钮区域外单击时,下拉菜单应该隐藏,所选项目的值也应该加载到下拉切换文本区域。请帮忙,我正在使用 react-bootstrap dropdown
<Dropdown className="dropdown-groove">
<Dropdown.Toggle variant="outline-secondary " id="dropdown-basic">
Select...
<label className="dropdown-label">Dropdown label</label>
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item href="#/action-1">
{" "}
<Form.Check
className="checkbox-groove"
inline
label="Unselected"
name="group1"
type="checkbox"
/>{" "}
</Dropdown.Item>
<Dropdown.Item href="#/action-2">List Item 2</Dropdown.Item>
<Dropdown.Item href="#/action-3">List Item 3</Dropdown.Item>
<Dropdown.Item href="#/action-4">List Item 4</Dropdown.Item>
<Dropdown.Item href="#/action-5">List Item 5</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
在<Form.Check>...</Form.Check>
中添加onClick={(e) => e.stopPropagation()}
。
e.stopPropagation()
在标签中将帮助此标签防止来自父级的 onClick 事件。
代码在这里:https://codesandbox.io/s/dropdown-with-checkbox-forked-88h9m6?file=/src/App.js
import "./styles.scss";
import "bootstrap/dist/css/bootstrap.min.css";
import { Dropdown, Form } from "react-bootstrap";
export default function App() {
return (
<div className="App">
<Dropdown className="dropdown-groove">
<Dropdown.Toggle variant="outline-secondary " id="dropdown-basic">
Select...
<label className="dropdown-label">Dropdown label</label>
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item href="#/action-1">
{" "}
<Form.Check
onClick={(e) => e.stopPropagation()} //<=== Add here
className="checkbox-groove"
inline
label="Unselected"
name="group1"
type="checkbox"
/>{" "}
</Dropdown.Item>
<Dropdown.Item href="#/action-2">List Item 2</Dropdown.Item>
<Dropdown.Item href="#/action-3">List Item 3</Dropdown.Item>
<Dropdown.Item href="#/action-4">List Item 4</Dropdown.Item>
<Dropdown.Item href="#/action-5">List Item 5</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
</div>
);
}