mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-25 03:29:59 +08:00
120db80c25
* type: type optimization * fix type * fix type * fix type * update docs
53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import React, { useState, useRef } from 'react';
|
|
import { PlusOutlined } from '@ant-design/icons';
|
|
import { Divider, Input, Select, Space, Button } from 'antd';
|
|
import type { InputRef } from 'antd';
|
|
|
|
let index = 0;
|
|
|
|
const App: React.FC = () => {
|
|
const [items, setItems] = useState(['jack', 'lucy']);
|
|
const [name, setName] = useState('');
|
|
const inputRef = useRef<InputRef>(null);
|
|
|
|
const onNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
setName(event.target.value);
|
|
};
|
|
|
|
const addItem = (e: React.MouseEvent<HTMLButtonElement | HTMLAnchorElement>) => {
|
|
e.preventDefault();
|
|
setItems([...items, name || `New item ${index++}`]);
|
|
setName('');
|
|
setTimeout(() => {
|
|
inputRef.current?.focus();
|
|
}, 0);
|
|
};
|
|
|
|
return (
|
|
<Select
|
|
style={{ width: 300 }}
|
|
placeholder="custom dropdown render"
|
|
dropdownRender={(menu) => (
|
|
<>
|
|
{menu}
|
|
<Divider style={{ margin: '8px 0' }} />
|
|
<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>
|
|
</Space>
|
|
</>
|
|
)}
|
|
options={items.map((item) => ({ label: item, value: item }))}
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default App;
|