如果用户认为适合的选择不在“选择”选项中显示的选项中,我想让用户能够添加自定义文本。我已经成功创建了自定义文本字段,但是获取用户输入并将其设置为选择组件的值变得很痛苦(听起来很容易做到,但由于某种原因它不工作) 这是我的实现
const [selectedpurpose, setSelectedPurpose] = useState("");// selected value of the select component const [customOption, setCustomOption] = useState ("");//custom userinput const [dropoptions, setDropOptions] = useState([ { value: "General Meeting", label: "General Meeting" }, { value: "Consultation Meeting", label: "Consultation Meeting" }, { value: "AdCampaign Meeting", label: "Ad Campaign Meeting" }, { value: "Marketing Meeting", label: "Marketing Meeting" }, ]);//select options // function to recored custom user input const handleAddOption = () => { if (customOption) { const newOption = { value: customOption, label: customOption }; setDropOptions([newOption, ...dropoptions]); setSelectedPurpose(customOption); setCustomOption(""); } }; //my form
我做错了什么,根据我的代码,单击添加按钮按钮并按 Enter 键应该将新值添加到选项数组中,并选择用户输入作为选择组件的值,但到目前为止它只会将其添加到选项数组并忽略将其设置为选择值 感谢任何帮助
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
try this one import React from "react"; import "./styles.css"; import Select, { components } from "react-select"; const CustomInput = (props) => { const { maxLength } = props.selectProps; const inputProps = { ...props, maxLength }; return ;
};
const App = () => {
const options = [
{ label: "Option 1", value: 1 },
{ label: "Option 2", value: 2 },
{ label: "Option 3", value: 3 }
];
return (
);
};
export default App;