ant-design/components/transfer/demo/search.md

81 lines
1.7 KiB
Markdown
Raw Normal View History

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
带搜索框的穿梭框,可以自定义搜索函数。
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
```tsx
2015-11-25 23:17:06 +08:00
import { Transfer } from 'antd';
import type { TransferDirection } from 'antd/es/transfer';
2022-05-21 22:14:15 +08:00
import React, { useEffect, useState } from 'react';
2015-11-25 23:17:06 +08:00
interface RecordType {
key: string;
title: string;
description: string;
chosen: boolean;
}
2018-06-27 15:55:04 +08:00
const App: React.FC = () => {
const [mockData, setMockData] = useState<RecordType[]>([]);
const [targetKeys, setTargetKeys] = useState<string[]>([]);
2018-06-27 15:55:04 +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}`,
2016-05-11 09:32:33 +08:00
chosen: Math.random() * 2 > 1,
2015-12-21 15:29:02 +08:00
};
if (data.chosen) {
tempTargetKeys.push(data.key);
2015-12-21 15:29:02 +08:00
}
tempMockData.push(data);
2015-12-21 15:29:02 +08:00
}
setMockData(tempMockData);
setTargetKeys(tempTargetKeys);
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
useEffect(() => {
getMock();
}, []);
2018-06-27 15:55:04 +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
const handleSearch = (dir: TransferDirection, value: string) => {
console.log('search:', dir, value);
};
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
export default App;
2019-05-07 14:57:32 +08:00
```