2018-11-27 12:01:30 +08:00
---
order: 21
title:
zh-CN: 扩展菜单
en-US: Custom dropdown
---
## zh-CN
2022-03-14 22:24:35 +08:00
使用 `dropdownRender` 对下拉菜单进行自由扩展。自定义内容点击时会关闭浮层,如果不喜欢关闭,可以添加 `onMouseDown={e => e.preventDefault()}` 进行阻止(更多详情见 [#13448 ](https://github.com/ant-design/ant-design/issues/13448 ))。
2018-11-27 12:01:30 +08:00
## en-US
2022-03-14 22:24:35 +08:00
Customize the dropdown menu via `dropdownRender` . Dropdown menu will be closed if click `dropdownRender` area, you can prevent it by wrapping `onMouseDown={e => e.preventDefault()}` (see more at [#13448 ](https://github.com/ant-design/ant-design/issues/13448 )).
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-05-21 22:14:15 +08:00
import { Divider, Input, Select, Space, Typography } from 'antd';
import React, { useState } from 'react';
2018-11-27 12:01:30 +08:00
2019-05-27 21:32:45 +08:00
const { Option } = Select;
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('');
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('');
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 ' } } / >
< Space align = "center" style = {{ padding: ' 0 8px 4px ' } } >
< Input placeholder = "Please enter item" value = {name} onChange = {onNameChange} / >
< Typography.Link onClick = {addItem} style = {{ whiteSpace: ' nowrap ' } } >
< PlusOutlined / > Add item
< / Typography.Link >
< / Space >
< />
)}
>
{items.map(item => (
< Option key = {item} > {item}< / Option >
))}
< / Select >
);
};
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
```