ant-design/components/auto-complete/demo/options.md

48 lines
882 B
Markdown
Raw Normal View History

2016-07-25 17:46:45 +08:00
---
order: 2
2016-08-23 21:00:35 +08:00
title:
2016-07-25 17:46:45 +08:00
zh-CN: 自定义选项
en-US: Customized
---
## zh-CN
可以返回自定义的 `Option` label
2016-07-25 17:46:45 +08:00
## en-US
You could set custom `Option` label
2016-07-25 17:46:45 +08:00
2020-01-21 16:21:37 +08:00
```tsx
2016-07-25 17:46:45 +08:00
import { AutoComplete } from 'antd';
2022-05-23 14:37:16 +08:00
import React, { useState } from 'react';
2016-08-23 21:00:35 +08:00
const App: React.FC = () => {
const [options, setOptions] = useState<{ value: string; label: string }[]>([]);
2020-01-21 16:21:37 +08:00
const handleSearch = (value: string) => {
let res: { value: string; label: string }[] = [];
2016-07-25 17:46:45 +08:00
if (!value || value.indexOf('@') >= 0) {
2020-01-21 16:21:37 +08:00
res = [];
2016-07-25 17:46:45 +08:00
} else {
res = ['gmail.com', '163.com', 'qq.com'].map(domain => ({
value,
label: `${value}@${domain}`,
}));
2016-07-25 17:46:45 +08:00
}
setOptions(res);
2019-05-07 14:57:32 +08:00
};
2020-01-21 16:21:37 +08:00
return (
<AutoComplete
style={{ width: 200 }}
onSearch={handleSearch}
placeholder="input here"
options={options}
/>
2020-01-21 16:21:37 +08:00
);
};
2016-07-25 17:46:45 +08:00
export default App;
2019-05-07 14:57:32 +08:00
```