ant-design/components/tree/demo/basic-controlled.md

118 lines
2.4 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 1
2016-09-01 18:12:12 +08:00
title:
2016-07-21 09:52:39 +08:00
zh-CN: 受控操作示例
en-US: basic controlled example
2016-03-31 09:40:55 +08:00
---
2016-01-07 19:05:55 +08:00
2016-07-21 09:52:39 +08:00
## zh-CN
2016-01-07 19:05:55 +08:00
受控操作示例
2016-07-21 09:52:39 +08:00
## en-US
basic controlled example
2017-02-13 10:55:53 +08:00
````jsx
2016-01-07 19:05:55 +08:00
import { Tree } from 'antd';
2018-06-27 15:55:04 +08:00
2016-01-07 19:05:55 +08:00
const TreeNode = Tree.TreeNode;
2017-09-15 10:42:59 +08:00
const treeData = [{
2017-09-15 11:14:22 +08:00
title: '0-0',
key: '0-0',
children: [{
title: '0-0-0',
key: '0-0-0',
children: [
{ title: '0-0-0-0', key: '0-0-0-0' },
{ title: '0-0-0-1', key: '0-0-0-1' },
{ title: '0-0-0-2', key: '0-0-0-2' },
2017-09-15 10:42:59 +08:00
],
}, {
2017-09-15 11:14:22 +08:00
title: '0-0-1',
key: '0-0-1',
children: [
{ title: '0-0-1-0', key: '0-0-1-0' },
{ title: '0-0-1-1', key: '0-0-1-1' },
{ title: '0-0-1-2', key: '0-0-1-2' },
2017-09-15 10:42:59 +08:00
],
}, {
2017-09-15 11:14:22 +08:00
title: '0-0-2',
key: '0-0-2',
2017-09-15 10:42:59 +08:00
}],
}, {
2017-09-15 11:14:22 +08:00
title: '0-1',
key: '0-1',
children: [
{ title: '0-1-0-0', key: '0-1-0-0' },
{ title: '0-1-0-1', key: '0-1-0-1' },
{ title: '0-1-0-2', key: '0-1-0-2' },
2017-09-15 10:42:59 +08:00
],
}, {
2017-09-15 11:14:22 +08:00
title: '0-2',
key: '0-2',
2017-09-15 10:42:59 +08:00
}];
2016-01-07 19:05:55 +08:00
class Demo extends React.Component {
state = {
expandedKeys: ['0-0-0', '0-0-1'],
autoExpandParent: true,
checkedKeys: ['0-0-0'],
selectedKeys: [],
}
2018-06-27 15:55:04 +08:00
onExpand = (expandedKeys) => {
2018-06-23 16:35:42 +08:00
console.log('onExpand', expandedKeys);
// if not set autoExpandParent to false, if children expanded, parent can not collapse.
2016-09-01 18:12:12 +08:00
// or, you can remove all expanded children keys.
this.setState({
expandedKeys,
autoExpandParent: false,
});
}
2018-06-27 15:55:04 +08:00
onCheck = (checkedKeys) => {
2017-09-15 10:42:59 +08:00
console.log('onCheck', checkedKeys);
this.setState({ checkedKeys });
}
2018-06-27 15:55:04 +08:00
onSelect = (selectedKeys, info) => {
2016-01-15 20:10:46 +08:00
console.log('onSelect', info);
2016-01-28 15:13:17 +08:00
this.setState({ selectedKeys });
}
2018-06-27 15:55:04 +08:00
2017-09-15 10:42:59 +08:00
renderTreeNodes = (data) => {
return data.map((item) => {
2016-01-28 15:13:17 +08:00
if (item.children) {
return (
2017-09-15 10:42:59 +08:00
<TreeNode title={item.title} key={item.key} dataRef={item}>
{this.renderTreeNodes(item.children)}
2016-01-28 15:13:17 +08:00
</TreeNode>
);
}
2017-09-15 10:42:59 +08:00
return <TreeNode {...item} />;
2016-01-28 15:13:17 +08:00
});
2017-09-15 10:42:59 +08:00
}
2018-06-27 15:55:04 +08:00
2017-09-15 10:42:59 +08:00
render() {
2016-01-28 15:13:17 +08:00
return (
2016-07-13 16:08:10 +08:00
<Tree
checkable
2017-05-15 14:37:22 +08:00
onExpand={this.onExpand}
expandedKeys={this.state.expandedKeys}
autoExpandParent={this.state.autoExpandParent}
2017-05-15 14:37:22 +08:00
onCheck={this.onCheck}
checkedKeys={this.state.checkedKeys}
onSelect={this.onSelect}
selectedKeys={this.state.selectedKeys}
>
2017-09-15 10:42:59 +08:00
{this.renderTreeNodes(treeData)}
2016-01-07 19:05:55 +08:00
</Tree>
2016-01-28 15:13:17 +08:00
);
}
}
2016-01-07 19:05:55 +08:00
ReactDOM.render(<Demo />, mountNode);
````