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-08-12 13:22:36 +08:00
|
|
|
```tsx
|
2016-01-07 19:05:55 +08:00
|
|
|
import { Tree } from 'antd';
|
2022-07-04 22:09:54 +08:00
|
|
|
import type { DataNode } from 'antd/es/tree';
|
2022-05-23 14:37:16 +08:00
|
|
|
import React, { useState } from 'react';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const treeData: DataNode[] = [
|
2019-05-07 14:57:32 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
2020-12-17 15:09:18 +08:00
|
|
|
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[]>([]);
|
2020-01-22 12:11:49 +08:00
|
|
|
const [autoExpandParent, setAutoExpandParent] = useState<boolean>(true);
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2021-02-23 10:45:11 +08:00
|
|
|
const onExpand = (expandedKeysValue: React.Key[]) => {
|
|
|
|
console.log('onExpand', expandedKeysValue);
|
2016-04-21 13:11:55 +08:00
|
|
|
// 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.
|
2021-02-23 10:45:11 +08:00
|
|
|
setExpandedKeys(expandedKeysValue);
|
2019-08-12 13:22:36 +08:00
|
|
|
setAutoExpandParent(false);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2021-02-23 10:45:11 +08:00
|
|
|
const onCheck = (checkedKeysValue: React.Key[]) => {
|
|
|
|
console.log('onCheck', checkedKeysValue);
|
|
|
|
setCheckedKeys(checkedKeysValue);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2021-02-23 10:45:11 +08:00
|
|
|
const onSelect = (selectedKeysValue: React.Key[], info: any) => {
|
2016-01-15 20:10:46 +08:00
|
|
|
console.log('onSelect', info);
|
2021-02-23 10:45:11 +08:00
|
|
|
setSelectedKeys(selectedKeysValue);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-08-12 13:22:36 +08:00
|
|
|
return (
|
|
|
|
<Tree
|
|
|
|
checkable
|
|
|
|
onExpand={onExpand}
|
|
|
|
expandedKeys={expandedKeys}
|
|
|
|
autoExpandParent={autoExpandParent}
|
|
|
|
onCheck={onCheck}
|
|
|
|
checkedKeys={checkedKeys}
|
|
|
|
onSelect={onSelect}
|
|
|
|
selectedKeys={selectedKeys}
|
|
|
|
treeData={treeData}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2016-01-07 19:05:55 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|