mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-25 08:43:57 +08:00
c8635a7bc0
* feat: add Transfer's key type * docs: update prop * fix: key type * fix: cycle dependency
64 lines
1.5 KiB
TypeScript
64 lines
1.5 KiB
TypeScript
import React, { useEffect, useState } from 'react';
|
|
import { Switch, Transfer } from 'antd';
|
|
import type { TransferProps } from 'antd';
|
|
|
|
interface RecordType {
|
|
key: string;
|
|
title: string;
|
|
description: string;
|
|
chosen: boolean;
|
|
}
|
|
|
|
const App: React.FC = () => {
|
|
const [oneWay, setOneWay] = useState(false);
|
|
const [mockData, setMockData] = useState<RecordType[]>([]);
|
|
const [targetKeys, setTargetKeys] = useState<React.Key[]>([]);
|
|
|
|
useEffect(() => {
|
|
const newTargetKeys = [];
|
|
const newMockData = [];
|
|
for (let i = 0; i < 2000; i++) {
|
|
const data = {
|
|
key: i.toString(),
|
|
title: `content${i + 1}`,
|
|
description: `description of content${i + 1}`,
|
|
chosen: i % 2 === 0,
|
|
};
|
|
if (data.chosen) {
|
|
newTargetKeys.push(data.key);
|
|
}
|
|
newMockData.push(data);
|
|
}
|
|
|
|
setTargetKeys(newTargetKeys);
|
|
setMockData(newMockData);
|
|
}, []);
|
|
|
|
const onChange: TransferProps['onChange'] = (newTargetKeys, direction, moveKeys) => {
|
|
console.log(newTargetKeys, direction, moveKeys);
|
|
setTargetKeys(newTargetKeys);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<Transfer
|
|
dataSource={mockData}
|
|
targetKeys={targetKeys}
|
|
onChange={onChange}
|
|
render={(item) => item.title}
|
|
oneWay={oneWay}
|
|
pagination
|
|
/>
|
|
<br />
|
|
<Switch
|
|
unCheckedChildren="one way"
|
|
checkedChildren="one way"
|
|
checked={oneWay}
|
|
onChange={setOneWay}
|
|
/>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default App;
|