2017-02-10 22:59:28 +08:00
|
|
|
---
|
2017-03-03 22:10:21 +08:00
|
|
|
order: 5
|
2017-02-10 22:59:28 +08:00
|
|
|
title:
|
|
|
|
zh-CN: 查询模式 - 不确定类目
|
|
|
|
en-US: Lookup-Patterns - Uncertain Category
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
[查询模式: 不确定类目](https://ant.design/docs/spec/reaction#Lookup-Patterns) 示例。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
Demonstration of [Lookup Patterns: Uncertain Category](https://ant.design/docs/spec/reaction#Lookup-Patterns).
|
2017-02-10 22:59:28 +08:00
|
|
|
|
2020-01-21 16:21:37 +08:00
|
|
|
```tsx
|
|
|
|
import React, { useState, FC } from 'react';
|
2020-01-17 11:39:31 +08:00
|
|
|
import { Input, AutoComplete } from 'antd';
|
2020-01-21 16:21:37 +08:00
|
|
|
import { SelectProps } from 'antd/es/select';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2020-01-21 16:21:37 +08:00
|
|
|
function getRandomInt(max: number, min: number = 0) {
|
2017-02-13 10:55:53 +08:00
|
|
|
return Math.floor(Math.random() * (max - min + 1)) + min; // eslint-disable-line no-mixed-operators
|
2017-02-10 22:59:28 +08:00
|
|
|
}
|
|
|
|
|
2020-01-21 16:21:37 +08:00
|
|
|
const searchResult = (query: string) => {
|
2019-05-07 14:57:32 +08:00
|
|
|
return new Array(getRandomInt(5))
|
|
|
|
.join('.')
|
|
|
|
.split('.')
|
2019-09-12 20:15:17 +08:00
|
|
|
.map((item, idx) => {
|
|
|
|
const category = `${query}${idx}`;
|
|
|
|
return {
|
|
|
|
value: category,
|
|
|
|
label: (
|
2020-01-17 11:39:31 +08:00
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<span>
|
2019-09-12 20:15:17 +08:00
|
|
|
Found {query} on{' '}
|
|
|
|
<a
|
|
|
|
href={`https://s.taobao.com/search?q=${query}`}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
|
|
|
{category}
|
|
|
|
</a>
|
|
|
|
</span>
|
2020-01-20 17:21:29 +08:00
|
|
|
<span>{getRandomInt(200, 100)} results</span>
|
2019-09-12 20:15:17 +08:00
|
|
|
</div>
|
|
|
|
),
|
|
|
|
};
|
|
|
|
});
|
2020-01-21 16:21:37 +08:00
|
|
|
};
|
2017-02-10 22:59:28 +08:00
|
|
|
|
2020-01-21 16:21:37 +08:00
|
|
|
const Complete: FC = () => {
|
|
|
|
const [options, setOptions] = useState<SelectProps<object>['options']>([]);
|
|
|
|
|
|
|
|
const handleSearch = (value: string) => {
|
|
|
|
setOptions(value ? searchResult(value) : []);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-02-19 20:12:41 +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
|
|
|
};
|
2017-02-19 20:12:41 +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>
|
|
|
|
);
|
|
|
|
};
|
2017-02-10 22:59:28 +08:00
|
|
|
|
|
|
|
ReactDOM.render(<Complete />, mountNode);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|