2018-11-27 12:01:30 +08:00
---
order: 21
title:
zh-CN: 扩展菜单
en-US: Custom dropdown
---
## zh-CN
2022-07-23 17:00:19 +08:00
使用 `open` 对下拉菜单进行自由扩展。如果希望点击自定义内容后关闭浮层,你需要使用受控模式自行控制([codesandbox](https://codesandbox.io/s/ji-ben-shi-yong-antd-4-21-7-forked-gnp4cy?file=/demo.js))。
2018-11-27 12:01:30 +08:00
## en-US
2022-07-23 17:00:19 +08:00
Customize the dropdown menu via `dropdownRender` . If you want to close the dropdown after clicking the custom content, you need to control `open` prop, here is an [codesandbox ](https://codesandbox.io/s/ji-ben-shi-yong-antd-4-21-7-forked-gnp4cy?file=/demo.js ).
2018-11-27 12:01:30 +08:00
2022-05-19 09:46:26 +08:00
```tsx
2019-11-28 12:34:33 +08:00
import { PlusOutlined } from '@ant-design/icons';
2022-07-23 17:00:19 +08:00
import { Divider, Input, Select, Space, Button } from 'antd';
import type { InputRef } from 'antd';
import React, { useState, useRef } from 'react';
2018-11-27 12:01:30 +08:00
2019-09-24 22:37:34 +08:00
let index = 0;
2022-05-19 09:46:26 +08:00
const App: React.FC = () => {
2022-02-11 10:39:10 +08:00
const [items, setItems] = useState(['jack', 'lucy']);
const [name, setName] = useState('');
2022-07-23 17:00:19 +08:00
const inputRef = useRef< InputRef > (null);
2019-09-29 20:58:37 +08:00
2022-05-19 09:46:26 +08:00
const onNameChange = (event: React.ChangeEvent< HTMLInputElement > ) => {
2022-02-11 10:39:10 +08:00
setName(event.target.value);
2019-09-24 22:37:34 +08:00
};
2022-05-19 09:46:26 +08:00
const addItem = (e: React.MouseEvent< HTMLAnchorElement > ) => {
2022-02-11 10:39:10 +08:00
e.preventDefault();
setItems([...items, name || `New item ${index++}` ]);
setName('');
2022-07-23 17:00:19 +08:00
setTimeout(() => {
inputRef.current?.focus();
}, 0);
2019-09-24 22:37:34 +08:00
};
2022-02-11 10:39:10 +08:00
return (
< Select
style={{ width: 300 }}
placeholder="custom dropdown render"
dropdownRender={menu => (
< >
{menu}
< Divider style = {{ margin: ' 8px 0 ' } } / >
2022-07-23 17:00:19 +08:00
< Space style = {{ padding: ' 0 8px 4px ' } } >
< Input
placeholder="Please enter item"
ref={inputRef}
value={name}
onChange={onNameChange}
/>
< Button type = "text" icon = {<PlusOutlined / > } onClick={addItem}>
Add item
< / Button >
2022-02-11 10:39:10 +08:00
< / Space >
< />
)}
2022-09-19 18:01:16 +08:00
options={items.map(item => ({ label: item, value: item }))}
/>
2022-02-11 10:39:10 +08:00
);
};
2019-09-24 22:37:34 +08:00
2022-04-15 16:20:56 +08:00
export default App;
2019-05-07 14:57:32 +08:00
```