mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-12 07:09:55 +08:00
8114516496
* update tree deps * add part of style * flex grid * update disabled * update demo * second demo * add draggable style * update demo * update rc-tree version * temp md * update tree deps * update icon demo * update style * update less * update deps * clean up * update test case * fix show line * update snapshot * fix lint * update style * update deps
106 lines
2.3 KiB
Markdown
106 lines
2.3 KiB
Markdown
---
|
|
order: 1
|
|
title:
|
|
zh-CN: 受控操作示例
|
|
en-US: Controlled Tree
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
受控操作示例
|
|
|
|
## en-US
|
|
|
|
Controlled mode lets parent nodes reflect the status of child nodes more intelligently.
|
|
|
|
```tsx
|
|
import { Tree } from 'antd';
|
|
|
|
const { TreeNode } = Tree;
|
|
|
|
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',
|
|
},
|
|
];
|
|
|
|
const Demo = () => {
|
|
const [expandedKeys, setExpandedKeys] = React.useState<string[]>(['0-0-0', '0-0-1']);
|
|
const [checkedKeys, setCheckedKeys] = React.useState<string[]>(['0-0-0']);
|
|
const [selectedKeys, setSelectedKeys] = React.useState<string[]>([]);
|
|
const [autoExpandParent, setAutoExpandParent] = React.useState<boolean>(true);
|
|
|
|
const 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.
|
|
setExpandedKeys(expandedKeys);
|
|
setAutoExpandParent(false);
|
|
};
|
|
|
|
const onCheck = checkedKeys => {
|
|
console.log('onCheck', checkedKeys);
|
|
setCheckedKeys(checkedKeys);
|
|
};
|
|
|
|
const onSelect = (selectedKeys, info) => {
|
|
console.log('onSelect', info);
|
|
setSelectedKeys(selectedKeys);
|
|
};
|
|
|
|
return (
|
|
<Tree
|
|
checkable
|
|
onExpand={onExpand}
|
|
expandedKeys={expandedKeys}
|
|
autoExpandParent={autoExpandParent}
|
|
onCheck={onCheck}
|
|
checkedKeys={checkedKeys}
|
|
onSelect={onSelect}
|
|
selectedKeys={selectedKeys}
|
|
treeData={treeData}
|
|
/>
|
|
);
|
|
};
|
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
|
```
|