mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-05 09:49:57 +08:00
cc0b7fdd8f
Prevent keydown bug when using custom dropdown render in multiple mode Select component. Example case: when pressing backspace on inner dropdown's Input component, the selected option in the Select component also getting triggered by backspace. Signed-off-by: Gideon <106825924+gidgudgod@users.noreply.github.com>
54 lines
1.5 KiB
TypeScript
54 lines
1.5 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}
|
|
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;
|