mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-05 17:59:57 +08:00
118 lines
2.4 KiB
Markdown
118 lines
2.4 KiB
Markdown
---
|
|
order: 1
|
|
title:
|
|
zh-CN: 受控操作示例
|
|
en-US: basic controlled example
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
受控操作示例
|
|
|
|
## en-US
|
|
|
|
basic controlled example
|
|
|
|
````jsx
|
|
import { Tree } from 'antd';
|
|
|
|
const TreeNode = Tree.TreeNode;
|
|
|
|
const treeData = [{
|
|
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' },
|
|
],
|
|
}, {
|
|
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',
|
|
}],
|
|
}, {
|
|
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' },
|
|
],
|
|
}, {
|
|
title: '0-2',
|
|
key: '0-2',
|
|
}];
|
|
|
|
class Demo extends React.Component {
|
|
state = {
|
|
expandedKeys: ['0-0-0', '0-0-1'],
|
|
autoExpandParent: true,
|
|
checkedKeys: ['0-0-0'],
|
|
selectedKeys: [],
|
|
}
|
|
|
|
onExpand = (expandedKeys) => {
|
|
console.log('onExpand', expandedKeys);
|
|
// if not set autoExpandParent to false, if children expanded, parent can not collapse.
|
|
// or, you can remove all expanded children keys.
|
|
this.setState({
|
|
expandedKeys,
|
|
autoExpandParent: false,
|
|
});
|
|
}
|
|
|
|
onCheck = (checkedKeys) => {
|
|
console.log('onCheck', checkedKeys);
|
|
this.setState({ checkedKeys });
|
|
}
|
|
|
|
onSelect = (selectedKeys, info) => {
|
|
console.log('onSelect', info);
|
|
this.setState({ selectedKeys });
|
|
}
|
|
|
|
renderTreeNodes = (data) => {
|
|
return data.map((item) => {
|
|
if (item.children) {
|
|
return (
|
|
<TreeNode title={item.title} key={item.key} dataRef={item}>
|
|
{this.renderTreeNodes(item.children)}
|
|
</TreeNode>
|
|
);
|
|
}
|
|
return <TreeNode {...item} />;
|
|
});
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<Tree
|
|
checkable
|
|
onExpand={this.onExpand}
|
|
expandedKeys={this.state.expandedKeys}
|
|
autoExpandParent={this.state.autoExpandParent}
|
|
onCheck={this.onCheck}
|
|
checkedKeys={this.state.checkedKeys}
|
|
onSelect={this.onSelect}
|
|
selectedKeys={this.state.selectedKeys}
|
|
>
|
|
{this.renderTreeNodes(treeData)}
|
|
</Tree>
|
|
);
|
|
}
|
|
}
|
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
|
````
|