You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.0 KiB

# 右键菜单
9 years ago
- order: 0
右键菜单示例
9 years ago
---
````jsx
import { Tree, Tooltip } from 'antd';
import assign from 'object-assign';
const TreeNode = Tree.TreeNode;
9 years ago
import React from 'react';
9 years ago
function contains(root, n) {
let node = n;
while (node) {
if (node === root) {
return true;
}
node = node.parentNode;
}
return false;
}
const Demo = React.createClass({
propTypes: {},
componentDidMount() {
this.getContainer();
9 years ago
// console.log(ReactDOM.findDOMNode(this), this.cmContainer);
console.log(contains(ReactDOM.findDOMNode(this), this.cmContainer));
9 years ago
},
componentWillUnmount() {
if (this.cmContainer) {
ReactDOM.unmountComponentAtNode(this.cmContainer);
document.body.removeChild(this.cmContainer);
this.cmContainer = null;
}
},
onSelect(info) {
console.log('selected', info);
},
onRightClick(info) {
console.log('right click', info);
this.renderCm(info);
},
9 years ago
onMouseEnter(info) {
console.log('enter', info);
this.renderCm(info);
},
9 years ago
onMouseLeave(info) {
console.log('leave', info);
},
getContainer() {
if (!this.cmContainer) {
this.cmContainer = document.createElement('div');
document.body.appendChild(this.cmContainer);
}
return this.cmContainer;
},
renderCm(info) {
if (this.toolTip) {
ReactDOM.unmountComponentAtNode(this.cmContainer);
this.toolTip = null;
}
9 years ago
this.toolTip = (<Tooltip trigger="click" placement="bottomRight" prefixCls="rc-tree-contextmenu" defaultVisible overlay={<h4>{info.node.props.title}</h4>}>
<span></span>
9 years ago
</Tooltip>);
const container = this.getContainer();
assign(this.cmContainer.style, {
position: 'absolute',
left: info.event.pageX + 'px',
top: info.event.pageY + 'px',
});
9 years ago
ReactDOM.render(this.toolTip, container);
9 years ago
},
render() {
return (
<div>
<h2>right click contextmenu</h2>
<Tree onRightClick={this.onRightClick} onSelect={this.onSelect}
9 years ago
defaultSelectedKeys={['0-1', '0-1-1']}
9 years ago
multiple defaultExpandAll showLine>
<TreeNode title="parent 1" key="0-1">
<TreeNode title="parent 1-0" key="0-1-1">
<TreeNode title="leaf0" />
<TreeNode title="leaf1" />
<TreeNode title="leaf2" />
</TreeNode>
<TreeNode title="parent 1-1">
<TreeNode title="leaf" />
</TreeNode>
</TreeNode>
</Tree>
9 years ago
<h2>hover popup contextmenu</h2>
<Tree onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave} onSelect={this.onSelect} multiple defaultExpandAll showLine>
<TreeNode title="parent 1" key="0-1">
<TreeNode title="parent 1-0" key="0-1-1">
<TreeNode title="leaf" />
<TreeNode title="leaf" />
</TreeNode>
<TreeNode title="parent 1-1">
<TreeNode title="leaf" />
</TreeNode>
</TreeNode>
</Tree>
9 years ago
</div>
);
},
});
ReactDOM.render(<Demo />, mountNode);
````