简体   繁体   English

将所选选项显示为选择下拉列表中的第一个选项?

[英]Show the selected option as the first option in the select dropdown?

I am using react-select to show a list of users in a dropdown.我正在使用 react-select 在下拉列表中显示用户列表。 I want it to always show the selected element as the first element in the list.我希望它始终将所选元素显示为列表中的第一个元素。 Is there native support for this?是否有本地支持?

Example:例子:

In the following image, since Green is selected, I want Green to appear at the top of the list before Ocean在下图中,由于选择了Green ,我希望Green出现在Ocean之前的列表顶部

在此处输入图片说明

You can pass an initialize callback to useState to sort the options array for the first time and put the defaultValue on top:您可以将初始化回调传递给useState以对options数组进行第一次排序并将defaultValue放在顶部:

const defaultValue = initialOptions[5];
const [options] = React.useState(() =>
  initialOptions.sort((a, b) => {
    if (a === defaultValue) return -1;
    if (b === defaultValue) return 1;
    return 0;
  })
);

return <Select defaultValue={defaultValue} name="colors" options={options} />;

代码沙盒演示

Try this Code :试试这个代码:

  const [options, setOptions] = useState([
    { label: "Blue", value: "Blue" },
    { label: "Red", value: "Red" },
    { label: "Yellow", value: "Yellow" },
    { label: "Green", value: "Green" },
    { label: "Pink", value: "Pink" },
    { label: "White", value: "White" },
    { label: "Brown", value: "Brown" }
  ]);
  const [value, setValue] = useState("Select");
  function handler(e) {
    setValue(e);
    let newOptions = [];
    newOptions.push({ label: e, value: e });
    for (let i in options) {
      if (options[i].value !== e) {
        newOptions.push(options[i]);
      }
    }
    setOptions(newOptions);
  }
  return (
    <div className="App">
      <Select
        name="colors"
        defaultValue={value}
        options={options}
        onChange={(e) => handler(e.value)}
      />
    </div>
  );

Click this link to view demo点击此链接查看演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 除非先选择另一个选项,否则选择元素的第一个选项不起作用 - First Option of Select Element Not Working Unless Another Option Is Selected First 如何在 React JS 编辑表单中的 select 下拉列表中显示所选选项 - How to display selected option in select dropdown in React JS edit form ReactJS - 即使从选择下拉列表中选择了相同的选项,也会触发事件 - ReactJS - trigger event even if the same option is selected from select dropdown 使用多下拉菜单时获取反应选择选项 - Get react-select selected option when using multi dropdown 第一次选择“选择”选项但第二次没有选择时,React / Redux失败 - React/Redux fails when a Select option is selected the first time but not the second 选择选择选项时,React js 显示隐藏的 div - React js show hidden div when select option is selected AntDesign select 菜单,希望选中时只显示部分选项 - AntDesign select menu, want to only show part of the option when selected 如何根据 React Js 中第一个下拉列表中的选定选项动态呈现不同的下拉列表 - How to render different dropdown list dynamically based on selected option in first dropdown in React Js Reactjs 显示 object 选择的选项 - Reactjs show selected option by object 用户单击以及如何根据所选编辑选择下拉选项后,不会更改所选的reactjs单选按钮 - reactjs radio button selected is not changed after user click and how to select dropdown option based on the selected edit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM