ant-design/components/auto-complete/demo/uncertain-category.md

80 lines
1.9 KiB
Markdown
Raw Normal View History

---
order: 5
title:
zh-CN: 查询模式 - 不确定类目
en-US: Lookup-Patterns - Uncertain Category
---
## zh-CN
[查询模式: 不确定类目](https://ant.design/docs/spec/reaction#Lookup-Patterns) 示例。
## en-US
Demonstration of [Lookup Patterns: Uncertain Category](https://ant.design/docs/spec/reaction#Lookup-Patterns).
2020-01-21 16:21:37 +08:00
```tsx
2022-05-23 14:37:16 +08:00
import { AutoComplete, Input } from 'antd';
import type { SelectProps } from 'antd/es/select';
2022-05-23 14:37:16 +08:00
import React, { useState } from 'react';
2018-06-27 15:55:04 +08:00
const getRandomInt = (max: number, min = 0) => Math.floor(Math.random() * (max - min + 1)) + min;
const searchResult = (query: string) =>
new Array(getRandomInt(5))
2019-05-07 14:57:32 +08:00
.join('.')
.split('.')
.map((_, idx) => {
const category = `${query}${idx}`;
return {
value: category,
label: (
<div
style={{
display: 'flex',
justifyContent: 'space-between',
}}
>
<span>
Found {query} on{' '}
<a
href={`https://s.taobao.com/search?q=${query}`}
target="_blank"
rel="noopener noreferrer"
>
{category}
</a>
</span>
<span>{getRandomInt(200, 100)} results</span>
</div>
),
};
});
const App: React.FC = () => {
2020-01-22 12:11:49 +08:00
const [options, setOptions] = useState<SelectProps<object>['options']>([]);
2020-01-21 16:21:37 +08:00
const handleSearch = (value: string) => {
setOptions(value ? searchResult(value) : []);
2019-05-07 14:57:32 +08:00
};
2020-01-21 16:21:37 +08:00
const onSelect = (value: string) => {
console.log('onSelect', value);
2019-05-07 14:57:32 +08:00
};
2020-01-21 16:21:37 +08:00
return (
<AutoComplete
dropdownMatchSelectWidth={252}
style={{ width: 300 }}
options={options}
onSelect={onSelect}
onSearch={handleSearch}
>
<Input.Search size="large" placeholder="input here" enterButton />
</AutoComplete>
);
};
export default App;
2019-05-07 14:57:32 +08:00
```