[英]stopPropagation on select from AntDesign
I have the next application: https://codesandbox.io/s/compassionate-snow-kjyqv?file=/index.js我有下一个应用程序: https://codesandbox.io/s/compassionate-snow-kjyqv?file=/index.js
import React from "react"; import ReactDOM from "react-dom"; import "antd/dist/antd.css"; import "./index.css"; import { Select } from "antd"; const { Option } = Select; function handleChange(value) { console.log(`selected ${value}`); } function click() { console.log("click"); } function onMouseEnter(e) { e.stopPropagation(); } ReactDOM.render( <> <div className="click" onClick={click}> <Select onMouseEnter={onMouseEnter} defaultValue="lucy" style={{ width: 120 }} onChange={handleChange} > <Option value="jack">Jack</Option> <Option value="lucy">Lucy</Option> </Select> </div> </>, document.getElementById("container") );
I want, when user clicks on red area to appear in console the word click
, but when user will click on select to open it, the:我想,当用户单击红色区域以显示在控制台中时
click
单词,但是当用户单击 select 将其打开时,:
function click() { console.log("click"); }
shouldn't work, this is why i inserted:不应该工作,这就是我插入的原因:
function onMouseEnter(e) { e.stopPropagation(); }
..but anyway the stopPropagation does not work and when i clicks on select the above function also work. ..但无论如何 stopPropagation 不起作用,当我点击 select 时,上面的 function 也可以工作。
Question: How to create the next scenario?: -- when user clic kson red area to apply console.log('work')
, but when will click on select the console.log should not be visible.问题:如何创建下一个场景?: -- 当用户单击 kson 红色区域以应用
console.log('work')
时,但当单击 select 时,console.log 应该不可见。
stopPropogation won't stop default behavior, you need preventDefault. stopPropogation 不会停止默认行为,您需要 preventDefault。 You should use onClick event instead of mouseEnter.
您应该使用 onClick 事件而不是 mouseEnter。
function onMouseEnter(e) {
e.stopPropagation();
e.preventDefault();
}
<Select
onClick={onMouseEnter}
defaultValue="lucy"
style={{ width: 120 }}
onChange={handleChange}
>
you need to stopPropagation in selection part in click event not in mouseEnter您需要在单击事件中的选择部分停止传播,而不是在 mouseEnter
i do some changes here:我在这里做了一些改变:
import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Select } from "antd";
const { Option } = Select;
function handleChange(value) {
console.log(`selected ${value}`);
}
function click() {
console.log("click");
}
// function for stoping propagation in clikc
function onClickStopProg(e) {
e.stopPropagation();
}
ReactDOM.render(
<>
<div className="click" onClick={click}>
<Select
// calling function in onclick event
// for stoping propagation
onClick={onClickStopProg}
defaultValue="lucy"
style={{ width: 120 }}
onChange={handleChange}
>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
</Select>
</div>
</>,
document.getElementById("container")
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.