2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 1
|
2016-08-08 14:12:02 +08:00
|
|
|
title:
|
2016-08-03 10:10:13 +08:00
|
|
|
zh-CN: 带搜索框
|
|
|
|
en-US: Search
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2015-11-25 23:17:06 +08:00
|
|
|
|
2016-08-03 10:10:13 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2016-08-05 10:49:54 +08:00
|
|
|
带搜索框的穿梭框,可以自定义搜索函数。
|
2015-11-25 23:17:06 +08:00
|
|
|
|
2016-08-03 10:10:13 +08:00
|
|
|
## en-US
|
|
|
|
|
2016-08-08 14:12:02 +08:00
|
|
|
Transfer with a search box.
|
2016-08-03 10:10:13 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2015-11-25 23:17:06 +08:00
|
|
|
import { 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, { useEffect, useState } from 'react';
|
2015-11-25 23:17:06 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
interface RecordType {
|
|
|
|
key: string;
|
|
|
|
title: string;
|
|
|
|
description: string;
|
|
|
|
chosen: boolean;
|
|
|
|
}
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
|
|
|
const [mockData, setMockData] = useState<RecordType[]>([]);
|
|
|
|
const [targetKeys, setTargetKeys] = useState<string[]>([]);
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const getMock = () => {
|
|
|
|
const tempTargetKeys = [];
|
|
|
|
const tempMockData = [];
|
2015-12-21 15:29:02 +08:00
|
|
|
for (let i = 0; i < 20; i++) {
|
|
|
|
const data = {
|
2016-10-24 11:06:31 +08:00
|
|
|
key: i.toString(),
|
2016-08-03 10:10:13 +08:00
|
|
|
title: `content${i + 1}`,
|
|
|
|
description: `description of content${i + 1}`,
|
2022-09-09 15:51:35 +08:00
|
|
|
chosen: i % 2 === 0,
|
2015-12-21 15:29:02 +08:00
|
|
|
};
|
|
|
|
if (data.chosen) {
|
2022-05-19 09:46:26 +08:00
|
|
|
tempTargetKeys.push(data.key);
|
2015-12-21 15:29:02 +08:00
|
|
|
}
|
2022-05-19 09:46:26 +08:00
|
|
|
tempMockData.push(data);
|
2015-12-21 15:29:02 +08:00
|
|
|
}
|
2022-05-19 09:46:26 +08:00
|
|
|
setMockData(tempMockData);
|
|
|
|
setTargetKeys(tempTargetKeys);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
useEffect(() => {
|
|
|
|
getMock();
|
|
|
|
}, []);
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const filterOption = (inputValue: string, option: RecordType) =>
|
|
|
|
option.description.indexOf(inputValue) > -1;
|
|
|
|
|
|
|
|
const handleChange = (newTargetKeys: string[]) => {
|
|
|
|
setTargetKeys(newTargetKeys);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleSearch = (dir: TransferDirection, value: string) => {
|
2018-10-12 23:47:42 +08:00
|
|
|
console.log('search:', dir, value);
|
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
return (
|
|
|
|
<Transfer
|
|
|
|
dataSource={mockData}
|
|
|
|
showSearch
|
|
|
|
filterOption={filterOption}
|
|
|
|
targetKeys={targetKeys}
|
|
|
|
onChange={handleChange}
|
|
|
|
onSearch={handleSearch}
|
|
|
|
render={item => item.title}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2015-12-21 15:29:02 +08:00
|
|
|
|
2022-04-15 16:20:56 +08:00
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|