You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
2.8 KiB
132 lines
2.8 KiB
6 years ago
|
---
|
||
|
order: 6
|
||
|
title:
|
||
|
zh-CN: 树穿梭框
|
||
|
en-US: Tree Transfer
|
||
|
---
|
||
|
|
||
|
## zh-CN
|
||
|
|
||
|
使用 Tree 组件作为自定义渲染列表。
|
||
|
|
||
|
## en-US
|
||
|
|
||
|
Customize render list with Tree component.
|
||
|
|
||
6 years ago
|
```jsx
|
||
6 years ago
|
import { Transfer, Tree } from 'antd';
|
||
|
|
||
|
const { TreeNode } = Tree;
|
||
|
|
||
|
// Customize Table Transfer
|
||
|
const isChecked = (selectedKeys, eventKey) => {
|
||
|
return selectedKeys.indexOf(eventKey) !== -1;
|
||
6 years ago
|
};
|
||
6 years ago
|
|
||
|
const generateTree = (treeNodes = [], checkedKeys = []) => {
|
||
|
return treeNodes.map(({ children, ...props }) => (
|
||
|
<TreeNode {...props} disabled={checkedKeys.includes(props.key)}>
|
||
|
{generateTree(children, checkedKeys)}
|
||
|
</TreeNode>
|
||
|
));
|
||
|
};
|
||
|
|
||
|
const TreeTransfer = ({ dataSource, targetKeys, ...restProps }) => {
|
||
|
const transferDataSource = [];
|
||
|
function flatten(list = []) {
|
||
6 years ago
|
list.forEach(item => {
|
||
6 years ago
|
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}
|
||
6 years ago
|
onCheck={(
|
||
|
_,
|
||
|
{
|
||
|
node: {
|
||
|
props: { eventKey },
|
||
|
},
|
||
|
},
|
||
|
) => {
|
||
6 years ago
|
onItemSelect(eventKey, !isChecked(checkedKeys, eventKey));
|
||
|
}}
|
||
6 years ago
|
onSelect={(
|
||
|
_,
|
||
|
{
|
||
|
node: {
|
||
|
props: { eventKey },
|
||
|
},
|
||
|
},
|
||
|
) => {
|
||
6 years ago
|
onItemSelect(eventKey, !isChecked(checkedKeys, eventKey));
|
||
|
}}
|
||
|
>
|
||
|
{generateTree(dataSource, targetKeys)}
|
||
|
</Tree>
|
||
|
);
|
||
|
}
|
||
|
}}
|
||
|
</Transfer>
|
||
|
);
|
||
|
};
|
||
|
|
||
|
const treeData = [
|
||
|
{ key: '0-0', title: '0-0' },
|
||
6 years ago
|
{
|
||
|
key: '0-1',
|
||
|
title: '0-1',
|
||
|
children: [{ key: '0-1-0', title: '0-1-0' }, { key: '0-1-1', title: '0-1-1' }],
|
||
|
},
|
||
6 years ago
|
{ key: '0-2', title: '0-3' },
|
||
|
];
|
||
|
|
||
|
class App extends React.Component {
|
||
|
state = {
|
||
|
targetKeys: [],
|
||
6 years ago
|
};
|
||
6 years ago
|
|
||
6 years ago
|
onChange = targetKeys => {
|
||
6 years ago
|
console.log('Target Keys:', targetKeys);
|
||
|
this.setState({ targetKeys });
|
||
6 years ago
|
};
|
||
6 years ago
|
|
||
|
render() {
|
||
|
const { targetKeys } = this.state;
|
||
|
return (
|
||
|
<div>
|
||
6 years ago
|
<TreeTransfer dataSource={treeData} targetKeys={targetKeys} onChange={this.onChange} />
|
||
6 years ago
|
</div>
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ReactDOM.render(<App />, mountNode);
|
||
6 years ago
|
```
|
||
6 years ago
|
|
||
|
<style>
|
||
|
.tree-transfer .ant-transfer-list:first-child {
|
||
|
width: 50%;
|
||
|
flex: none;
|
||
|
}
|
||
|
</style>
|