mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-22 14:44:47 +08:00
502dac12aa
* docs: fix code * feat: lint * feat: prettier * feat: test * feat: review * feat: format html * feat: format html
54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import React, { useRef, useState } from 'react';
|
|
import { PlusOutlined } from '@ant-design/icons';
|
|
import { Button, Divider, Input, Select, Space } 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}
|
|
onKeyDown={(e) => e.stopPropagation()}
|
|
/>
|
|
<Button type="text" icon={<PlusOutlined />} onClick={addItem}>
|
|
Add item
|
|
</Button>
|
|
</Space>
|
|
</>
|
|
)}
|
|
options={items.map((item) => ({ label: item, value: item }))}
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default App;
|