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

125 lines
2.6 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: 受控操作示例
2019-04-20 06:39:58 +08:00
en-US: Controlled Tree
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
2019-04-20 06:39:58 +08:00
Controlled mode lets parent nodes reflect the status of child nodes more intelligently.
2016-07-21 09:52:39 +08:00
2019-05-07 14:57:32 +08:00
```jsx
2016-01-07 19:05:55 +08:00
import { Tree } from 'antd';
2018-06-27 15:55:04 +08:00
2018-11-28 15:00:03 +08:00
const { TreeNode } = Tree;
2016-01-07 19:05:55 +08:00
2019-05-07 14:57:32 +08:00
const treeData = [
{
title: '0-0',
key: '0-0',
2017-09-15 11:14:22 +08:00
children: [
2019-05-07 14:57:32 +08:00
{
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' },
],
},
{
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' },
],
},
{
title: '0-0-2',
key: '0-0-2',
},
2017-09-15 10:42:59 +08:00
],
2019-05-07 14:57:32 +08:00
},
{
title: '0-1',
key: '0-1',
2017-09-15 11:14:22 +08:00
children: [
2019-05-07 14:57:32 +08:00
{ 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
],
2019-05-07 14:57:32 +08:00
},
{
title: '0-2',
key: '0-2',
},
];
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: [],
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2019-05-07 14:57:32 +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,
});
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2019-05-07 14:57:32 +08:00
onCheck = checkedKeys => {
2017-09-15 10:42:59 +08:00
console.log('onCheck', checkedKeys);
this.setState({ checkedKeys });
2019-05-07 14:57:32 +08:00
};
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 });
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2019-05-07 14:57:32 +08:00
renderTreeNodes = data =>
data.map(item => {
if (item.children) {
return (
<TreeNode title={item.title} key={item.key} dataRef={item}>
{this.renderTreeNodes(item.children)}
</TreeNode>
);
}
2019-08-02 18:19:06 +08:00
return <TreeNode key={item.key} {...item} />;
2019-05-07 14:57:32 +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);
2019-05-07 14:57:32 +08:00
```