https://github.com/react-component/tree
我正在尝试提供的http://react-component.github.io/tree/examples,但找不到在树状视图上禁用文件(叶子)的选项
import React from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import Tree, { TreeNode } from 'rc-tree';
import 'rc-tree/assets/index.css';
import './basic.less';
const treeData = [
{ key: '0-0', title: 'parent 1', children:
[
{ key: '0-0-0', title: 'parent 1-1', children:
[
{ key: '0-0-0-0', title: 'parent 1-1-0' },
],
},
{ key: '0-0-1', title: 'parent 1-2', children:
[
{ key: '0-0-1-0', title: 'parent 1-2-0', disableCheckbox: true },
{ key: '0-0-1-1', title: 'parent 1-2-1' },
],
},
],
},
];
class Demo extends React.Component {
static propTypes = {
keys: PropTypes.array,
};
static defaultProps = {
keys: ['0-0-0-0'],
};
constructor(props) {
super(props);
const keys = props.keys;
this.state = {
defaultExpandedKeys: keys,
defaultSelectedKeys: keys,
defaultCheckedKeys: keys,
};
}
onExpand = (...args) => {
console.log('onExpand', ...args);
};
onSelect = (selectedKeys, info) => {
console.log('selected', selectedKeys, info);
this.selKey = info.node.props.eventKey;
if (this.tree) {
console.log(
'Selected DOM node:',
selectedKeys.map(key => ReactDOM.findDOMNode(this.tree.domTreeNodes[key])),
);
}
};
onCheck = (checkedKeys, info) => {
console.log('onCheck', checkedKeys, info);
};
onEdit = () => {
setTimeout(() => {
console.log('current key: ', this.selKey);
}, 0);
};
onDel = (e) => {
if (!window.confirm('sure to delete?')) {
return;
}
e.stopPropagation();
};
setTreeRef = (tree) => {
this.tree = tree;
};
render() {
const customLabel = (
<span className="cus-label">
<span>operations: </span>
<span style={{ color: 'blue' }} onClick={this.onEdit}>Edit</span>
<label onClick={(e) => e.stopPropagation()}>
<input type="checkbox" /> checked
</label>
<span style={{ color: '#EB0000' }} onClick={this.onDel}>Delete</span>
</span>
);
return (
<div style={{ margin: '0 20px' }}>
<h2>simple</h2>
<Tree
ref={this.setTreeRef}
className="myCls" showLine checkable defaultExpandAll
defaultExpandedKeys={this.state.defaultExpandedKeys}
onExpand={this.onExpand}
defaultSelectedKeys={this.state.defaultSelectedKeys}
defaultCheckedKeys={this.state.defaultCheckedKeys}
onSelect={this.onSelect} onCheck={this.onCheck}
>
<TreeNode title="parent 1" key="0-0">
<TreeNode title={customLabel} key="0-0-0">
<TreeNode title="leaf" key="0-0-0-0" style={{ background: 'rgba(255, 0, 0, 0.1)' }} />
<TreeNode title="leaf" key="0-0-0-1" />
</TreeNode>
<TreeNode title="parent 1-1" key="0-0-1">
<TreeNode title="parent 1-1-0" key="0-0-1-0" disableCheckbox />
<TreeNode title="parent 1-1-1" key="0-0-1-1" />
</TreeNode>
<TreeNode title="parent 1-2" key="0-0-2" disabled>
<TreeNode title="parent 1-2-0" key="0-0-2-0" checkable={false} />
<TreeNode title="parent 1-2-1" key="0-0-2-1" />
</TreeNode>
</TreeNode>
</Tree>
<h2>Check on Click TreeNode</h2>
<Tree
className="myCls"
showLine
checkable
selectable={ false }
defaultExpandAll
onExpand={this.onExpand}
defaultSelectedKeys={this.state.defaultSelectedKeys}
defaultCheckedKeys={this.state.defaultCheckedKeys}
onSelect={this.onSelect}
onCheck={this.onCheck}
treeData={treeData}
/>
</div>
);
}
}
ReactDOM.render(<Demo />, document.getElementById('__react-content'));
仅显示文件夹图标,不显示文件
答案 0 :(得分:0)
您可以添加prop isLeaf = {false}
<TreeNode title="leaf" key="0-0-0-1" />