AntDesign Form
I want to use a “Select item” with a Custom Dropdown render for show a preloaded data, and you can add a new item if doesn’t exists.
The problem resides on submit the form, because the dropdown render has an “Input item”, and takes the value of the Input, instead of the Select.
Codepen of the code with the problem
const DropdownCustom = () => {
const [items, setItems] = useState(['jack', 'lucy']);
const [name, setName] = useState('');
const inputRef = useRef(null);
const onNameChange = (event) => {
setName(event.target.value);
};
const addItem = (e) => {
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,
}))}
/>
);
};
Is there a way to set the Input to add new data and prioritize the value of the Select Item?
New contributor
user25367853 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.