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
|
2019-09-29 20:58:37 +08:00
|
|
|
import { Select, Divider, Input } 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;
|
|
|
|
|
|
|
|
class App extends React.Component {
|
|
|
|
state = {
|
|
|
|
items: ['jack', 'lucy'],
|
2019-09-29 20:58:37 +08:00
|
|
|
name: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
onNameChange = event => {
|
|
|
|
this.setState({
|
|
|
|
name: event.target.value,
|
|
|
|
});
|
2019-09-24 22:37:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
addItem = () => {
|
|
|
|
console.log('addItem');
|
2019-09-29 20:58:37 +08:00
|
|
|
const { items, name } = this.state;
|
2019-09-24 22:37:34 +08:00
|
|
|
this.setState({
|
2019-09-29 20:58:37 +08:00
|
|
|
items: [...items, name || `New item ${index++}`],
|
|
|
|
name: '',
|
2019-09-24 22:37:34 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2019-09-29 20:58:37 +08:00
|
|
|
const { items, name } = this.state;
|
2019-09-24 22:37:34 +08:00
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
style={{ width: 240 }}
|
|
|
|
placeholder="custom dropdown render"
|
|
|
|
dropdownRender={menu => (
|
|
|
|
<div>
|
|
|
|
{menu}
|
|
|
|
<Divider style={{ margin: '4px 0' }} />
|
2019-09-29 20:58:37 +08:00
|
|
|
<div style={{ display: 'flex', flexWrap: 'nowrap', padding: 8 }}>
|
|
|
|
<Input style={{ flex: 'auto' }} value={name} onChange={this.onNameChange} />
|
|
|
|
<a
|
|
|
|
style={{ flex: 'none', padding: '8px', display: 'block', cursor: 'pointer' }}
|
|
|
|
onClick={this.addItem}
|
|
|
|
>
|
2019-11-28 12:34:33 +08:00
|
|
|
<PlusOutlined /> Add item
|
2019-09-29 20:58:37 +08:00
|
|
|
</a>
|
|
|
|
</div>
|
2019-09-24 22:37:34 +08:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{items.map(item => (
|
|
|
|
<Option key={item}>{item}</Option>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(<App />, mountNode);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|