2019-05-07 17:10:42 +08:00
|
|
|
---
|
|
|
|
order: 6
|
|
|
|
title:
|
|
|
|
zh-CN: 树穿梭框
|
|
|
|
en-US: Tree Transfer
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
使用 Tree 组件作为自定义渲染列表。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Customize render list with Tree component.
|
|
|
|
|
2019-05-07 17:22:47 +08:00
|
|
|
```jsx
|
2019-05-07 17:10:42 +08:00
|
|
|
import { Transfer, Tree } from 'antd';
|
|
|
|
|
|
|
|
// Customize Table Transfer
|
|
|
|
const isChecked = (selectedKeys, eventKey) => {
|
|
|
|
return selectedKeys.indexOf(eventKey) !== -1;
|
2019-05-07 17:22:47 +08:00
|
|
|
};
|
2019-05-07 17:10:42 +08:00
|
|
|
|
|
|
|
const generateTree = (treeNodes = [], checkedKeys = []) => {
|
2020-05-13 19:15:40 +08:00
|
|
|
return treeNodes.map(({ children, ...props }) => ({
|
|
|
|
...props,
|
|
|
|
disabled: checkedKeys.includes(props.key),
|
|
|
|
children: generateTree(children, checkedKeys),
|
|
|
|
}));
|
2019-05-07 17:10:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const TreeTransfer = ({ dataSource, targetKeys, ...restProps }) => {
|
|
|
|
const transferDataSource = [];
|
|
|
|
function flatten(list = []) {
|
2019-05-07 17:22:47 +08:00
|
|
|
list.forEach(item => {
|
2019-05-07 17:10:42 +08:00
|
|
|
transferDataSource.push(item);
|
|
|
|
flatten(item.children);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
flatten(dataSource);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Transfer
|
|
|
|
{...restProps}
|
|
|
|
targetKeys={targetKeys}
|
|
|
|
dataSource={transferDataSource}
|
|
|
|
className="tree-transfer"
|
|
|
|
render={item => item.title}
|
|
|
|
showSelectAll={false}
|
|
|
|
>
|
|
|
|
{({ direction, onItemSelect, selectedKeys }) => {
|
|
|
|
if (direction === 'left') {
|
|
|
|
const checkedKeys = [...selectedKeys, ...targetKeys];
|
|
|
|
return (
|
|
|
|
<Tree
|
|
|
|
blockNode
|
|
|
|
checkable
|
|
|
|
checkStrictly
|
|
|
|
defaultExpandAll
|
|
|
|
checkedKeys={checkedKeys}
|
2020-05-13 19:15:40 +08:00
|
|
|
treeData={generateTree(dataSource, targetKeys)}
|
|
|
|
onCheck={(_, { node: { key } }) => {
|
|
|
|
onItemSelect(key, !isChecked(checkedKeys, key));
|
2019-05-07 17:10:42 +08:00
|
|
|
}}
|
2020-05-13 19:15:40 +08:00
|
|
|
onSelect={(_, { node: { key } }) => {
|
|
|
|
onItemSelect(key, !isChecked(checkedKeys, key));
|
2019-05-07 17:10:42 +08:00
|
|
|
}}
|
2020-05-13 19:15:40 +08:00
|
|
|
/>
|
2019-05-07 17:10:42 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
</Transfer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const treeData = [
|
|
|
|
{ key: '0-0', title: '0-0' },
|
2019-05-07 17:22:47 +08:00
|
|
|
{
|
|
|
|
key: '0-1',
|
|
|
|
title: '0-1',
|
2020-05-13 19:15:40 +08:00
|
|
|
children: [
|
|
|
|
{ key: '0-1-0', title: '0-1-0' },
|
|
|
|
{ key: '0-1-1', title: '0-1-1' },
|
|
|
|
],
|
2019-05-07 17:22:47 +08:00
|
|
|
},
|
2019-05-07 17:10:42 +08:00
|
|
|
{ key: '0-2', title: '0-3' },
|
|
|
|
];
|
|
|
|
|
|
|
|
class App extends React.Component {
|
|
|
|
state = {
|
|
|
|
targetKeys: [],
|
2019-05-07 17:22:47 +08:00
|
|
|
};
|
2019-05-07 17:10:42 +08:00
|
|
|
|
2019-05-07 17:22:47 +08:00
|
|
|
onChange = targetKeys => {
|
2019-05-07 17:10:42 +08:00
|
|
|
console.log('Target Keys:', targetKeys);
|
|
|
|
this.setState({ targetKeys });
|
2019-05-07 17:22:47 +08:00
|
|
|
};
|
2019-05-07 17:10:42 +08:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { targetKeys } = this.state;
|
|
|
|
return (
|
|
|
|
<div>
|
2019-05-07 17:22:47 +08:00
|
|
|
<TreeTransfer dataSource={treeData} targetKeys={targetKeys} onChange={this.onChange} />
|
2019-05-07 17:10:42 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(<App />, mountNode);
|
2019-05-07 17:22:47 +08:00
|
|
|
```
|
2019-05-07 17:10:42 +08:00
|
|
|
|
|
|
|
<style>
|
|
|
|
.tree-transfer .ant-transfer-list:first-child {
|
|
|
|
width: 50%;
|
|
|
|
flex: none;
|
|
|
|
}
|
|
|
|
</style>
|