2018-11-27 12:01:30 +08:00
|
|
|
---
|
|
|
|
order: 21
|
|
|
|
title:
|
|
|
|
zh-CN: 扩展菜单
|
|
|
|
en-US: Custom dropdown
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
使用 `dropdownRender` 对下拉菜单进行自由扩展。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Customize the dropdown menu via `dropdownRender`.
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
2022-02-11 10:39:10 +08:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { Select, Divider, Input, Typography, Space } from 'antd';
|
2019-11-28 12:34:33 +08:00
|
|
|
import { PlusOutlined } from '@ant-design/icons';
|
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-02-11 10:39:10 +08:00
|
|
|
const App = () => {
|
|
|
|
const [items, setItems] = useState(['jack', 'lucy']);
|
|
|
|
const [name, setName] = useState('');
|
2019-09-29 20:58:37 +08:00
|
|
|
|
2022-02-11 10:39:10 +08:00
|
|
|
const onNameChange = event => {
|
|
|
|
setName(event.target.value);
|
2019-09-24 22:37:34 +08:00
|
|
|
};
|
|
|
|
|
2022-02-11 10:39:10 +08:00
|
|
|
const addItem = e => {
|
|
|
|
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
|
|
|
|
|
|
|
ReactDOM.render(<App />, mountNode);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|