2020-05-13 19:15:40 +08:00
|
|
|
---
|
|
|
|
order: 0.1
|
|
|
|
title:
|
|
|
|
zh-CN: 单向样式
|
|
|
|
en-US: One Way
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
通过 `oneWay` 将 Transfer 转为单向样式。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Use `oneWay` to makes Transfer to one way style.
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2022-05-23 14:37:16 +08:00
|
|
|
import { Switch, Transfer } from 'antd';
|
2022-05-19 09:46:26 +08:00
|
|
|
import type { TransferDirection } from 'antd/es/transfer';
|
2022-05-23 14:37:16 +08:00
|
|
|
import React, { useState } from 'react';
|
2020-05-13 19:15:40 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
interface RecordType {
|
|
|
|
key: string;
|
|
|
|
title: string;
|
|
|
|
description: string;
|
|
|
|
disabled: boolean;
|
2020-05-13 19:15:40 +08:00
|
|
|
}
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const mockData: RecordType[] = Array.from({ length: 20 }).map((_, i) => ({
|
|
|
|
key: i.toString(),
|
|
|
|
title: `content${i + 1}`,
|
|
|
|
description: `description of content${i + 1}`,
|
|
|
|
disabled: i % 3 < 1,
|
|
|
|
}));
|
2020-05-13 19:15:40 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const oriTargetKeys = mockData.filter(item => Number(item.key) % 3 > 1).map(item => item.key);
|
|
|
|
|
|
|
|
const App: React.FC = () => {
|
|
|
|
const [targetKeys, setTargetKeys] = useState<string[]>(oriTargetKeys);
|
|
|
|
const [selectedKeys, setSelectedKeys] = useState<string[]>([]);
|
|
|
|
const [disabled, setDisabled] = useState(false);
|
2020-05-13 19:15:40 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleChange = (
|
|
|
|
newTargetKeys: string[],
|
|
|
|
direction: TransferDirection,
|
|
|
|
moveKeys: string[],
|
|
|
|
) => {
|
|
|
|
setTargetKeys(newTargetKeys);
|
2020-05-13 19:15:40 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
console.log('targetKeys: ', newTargetKeys);
|
2020-05-13 19:15:40 +08:00
|
|
|
console.log('direction: ', direction);
|
|
|
|
console.log('moveKeys: ', moveKeys);
|
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleSelectChange = (sourceSelectedKeys: string[], targetSelectedKeys: string[]) => {
|
|
|
|
setSelectedKeys([...sourceSelectedKeys, ...targetSelectedKeys]);
|
2020-05-13 19:15:40 +08:00
|
|
|
|
|
|
|
console.log('sourceSelectedKeys: ', sourceSelectedKeys);
|
|
|
|
console.log('targetSelectedKeys: ', targetSelectedKeys);
|
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleScroll = (
|
|
|
|
direction: TransferDirection,
|
|
|
|
e: React.SyntheticEvent<HTMLUListElement, Event>,
|
|
|
|
) => {
|
2020-05-13 19:15:40 +08:00
|
|
|
console.log('direction:', direction);
|
|
|
|
console.log('target:', e.target);
|
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleDisable = (checked: boolean) => {
|
|
|
|
setDisabled(checked);
|
2020-05-13 19:15:40 +08:00
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Transfer
|
|
|
|
dataSource={mockData}
|
|
|
|
titles={['Source', 'Target']}
|
|
|
|
targetKeys={targetKeys}
|
|
|
|
selectedKeys={selectedKeys}
|
|
|
|
onChange={handleChange}
|
|
|
|
onSelectChange={handleSelectChange}
|
|
|
|
onScroll={handleScroll}
|
|
|
|
render={item => item.title}
|
|
|
|
disabled={disabled}
|
|
|
|
oneWay
|
|
|
|
style={{ marginBottom: 16 }}
|
|
|
|
/>
|
|
|
|
<Switch
|
|
|
|
unCheckedChildren="disabled"
|
|
|
|
checkedChildren="disabled"
|
|
|
|
checked={disabled}
|
|
|
|
onChange={handleDisable}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2020-05-13 19:15:40 +08:00
|
|
|
|
2022-04-15 16:20:56 +08:00
|
|
|
export default App;
|
2020-05-13 19:15:40 +08:00
|
|
|
```
|