在用户键入时加载带有异步选项的react-select动态下拉菜单 [英] react-select dynamic dropdown with async options loaded as user is typing
问题描述
我是React的新手,我正在尝试合并2个不同的功能.一种动态表单,您可以在其中添加和/或删除输入,以及一个具有异步react-select的表单,您可以在其中开始输入单词,然后根据API来源(例如,基于连接的用户)显示选项并进行过滤
I'm new to React and I'm trying to merge 2 different features. A dynamic form where you can add and/or remove inputs AND one with async react-select where you can start typing a word and options appear and get filtered based on an API source (based on connected user for example)
(我想)我快完成了:
当我开始键入内容时,我可以正确看到我的选项...并且在单击某个项目(以选择该项目)时,选项被正确过滤,但出现错误.
When I start typing I correctly see my options...and options get filtered correctly BUT when I click on an item (to select this item) I get an error.
我得到的错误是无法读取未定义的属性'name'
,但我不理解,我不确定这是我遇到的唯一问题.我不知道如何正确选择并正确放入对象数组( inputFields
)
The error I got is Cannot read property 'name' of undefined
but I don't understand it and I'm not sure it's the only problem I got. I have no clue how to get my choice to cprrectly get selected and correctly put into my array of objects (inputFields
)
以下是我尝试混合使用的2种不同来源(它们都可以完美地独立运行)
Here are the 2 different sources I try to mix (They both work perfectly put independantly)
React-Select Async下拉列表: https://stackblitz.com/edit/react-select-async-component?file = index.js 动态表单字段: https://www.youtube.com/watch?v=zgKH12s_95A
React-Select Async dropdown : https://stackblitz.com/edit/react-select-async-component?file=index.js Dynamic form field : https://www.youtube.com/watch?v=zgKH12s_95A
谢谢您帮助我理解问题所在!!
Thank you for helping me understand what's the problem !!!
这是我的代码:
function AsyncDynamicForm(props) {
const [inputFields, setInputFields] = useState([
{ firstName: '' },
]);
const [inputValue, setValue] = useState("");
const handleChangeInput = (index, event) => {
const values = [...inputFields];
values[index][event.target.name] = event.target.value;
setInputFields(values);
};
const AddFields = () => {
setInputFields([...inputFields, { firstName: '' }]);
};
const RemoveFields = (index) => {
const values = [...inputFields];
values.splice(index, 1);
setInputFields(values);
};
const loadOptions = (inputValue) => {
return fetch(
`http://127.0.0.1:8000/api/Objects/?q=${inputValue}`
).then((res) => res.json());
};
const handleInputChange = (value) => {
setValue(value)
};
const handleSubmit = (e) => {
e.preventDefault();
console.log("inputFields", inputFields); // Nothing for now
};
return (
<div>
<Container>
<Form onSubmit={handleSubmit}>
{inputFields.map((inputField, index) => (
<div key={index}>
<Form.Field inline>
<AsyncSelect
name="firstName"
value={inputField.firstName}
onChange={(event) => handleChangeInput(index, event)}
cacheOptions
defaultOptions
getOptionLabel={(e) => e.name.toString()}
getOptionValue={(e) => e.id}
loadOptions={loadOptions}
onInputChange={handleInputChange}
/>
</Form.Field>
<Button.Group basic size="small">
<Button icon="add" onClick={() => AddFields()} />
<Button
icon="x"
onClick={() => RemoveFields(index)}
/>
</Button.Group>
</div>
))}
<Button type="submit" onClick={handleSubmit}>
click
</Button>
</Form>
</Container>
</div>
);
}
export default AsyncDynamicForm
推荐答案
文档非常有用这里. onChange
道具采用带有特定签名的方法.
The documentation is very helpful here. The onChange
prop takes a method with a specific signature.
const onChange = (option, {action}) => {
/* The `option` value will be different, based on the Select type
* and the action, being one of `option`, an array of `option`s
* (in the instance of a multiselect), `null` (typical when clearing
* an option), or `undefined`.
* What you actually get will depend on the `action` the select passes,
* being one of:
* - 'select-option'
* - 'deselect-option'
* - 'remove-value'
* - 'pop-value'
* - 'set-value'
* - 'clear'
* - 'create-option'
*/
// example uses the `useState` hook defined earlier
switch (action) {
case 'select-option',
case 'remove-value',
case 'clear':
setColor(option);
break;
default:
// I'm not worried about other actions right now
break;
}
};
请记住,React-Select将 value
视为整个 option
对象,而不仅仅是在 getOptionValue <中定义的
option
值./code>.如果您要设置一个真正的表单值",则可能会以某种方式包装选择"来处理该问题.
Remember that React-Select treats value
as the entire option
object, not just the option
value you define in getOptionValue
. If you're looking at setting a true form 'value', you'll probably wrap Select in some way to handle that.
React-Select非常强大,而且非常复杂.该文档是您的朋友.当尝试尚未完全了解的功能时,我发现在CodeSandbox中玩耍很有帮助.
React-Select is incredibly powerful, and incredibly complex. The documentation is your friend here. I find it helpful to play around in CodeSandbox, when trying out features I don't fully understand yet.
这篇关于在用户键入时加载带有异步选项的react-select动态下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!