mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-04 08:59:40 +08:00
76434a15df
* chore(🆙): upgrade devDeps
* prettier code
* fix dnd demo
* fix react-dnd demo
* fix npm start and demo tsx compile
* fix snapshot
105 lines
2.4 KiB
Markdown
105 lines
2.4 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 React, { useState } from 'react';
|
|
import { Tree } from 'antd';
|
|
|
|
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] = useState<React.Key[]>(['0-0-0', '0-0-1']);
|
|
const [checkedKeys, setCheckedKeys] = useState<React.Key[]>(['0-0-0']);
|
|
const [selectedKeys, setSelectedKeys] = useState<React.Key[]>([]);
|
|
const [autoExpandParent, setAutoExpandParent] = useState<boolean>(true);
|
|
|
|
const onExpand = (expandedKeysValue: React.Key[]) => {
|
|
console.log('onExpand', expandedKeysValue);
|
|
// if not set autoExpandParent to false, if children expanded, parent can not collapse.
|
|
// or, you can remove all expanded children keys.
|
|
setExpandedKeys(expandedKeysValue);
|
|
setAutoExpandParent(false);
|
|
};
|
|
|
|
const onCheck = (checkedKeysValue: React.Key[]) => {
|
|
console.log('onCheck', checkedKeysValue);
|
|
setCheckedKeys(checkedKeysValue);
|
|
};
|
|
|
|
const onSelect = (selectedKeysValue: React.Key[], info: any) => {
|
|
console.log('onSelect', info);
|
|
setSelectedKeys(selectedKeysValue);
|
|
};
|
|
|
|
return (
|
|
<Tree
|
|
checkable
|
|
onExpand={onExpand}
|
|
expandedKeys={expandedKeys}
|
|
autoExpandParent={autoExpandParent}
|
|
onCheck={onCheck}
|
|
checkedKeys={checkedKeys}
|
|
onSelect={onSelect}
|
|
selectedKeys={selectedKeys}
|
|
treeData={treeData}
|
|
/>
|
|
);
|
|
};
|
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
|
```
|