简体   繁体   English

如何在反应选择下拉列表中单击按钮添加选择?

[英]How to make a button click add selection in react-select dropdown?

I am currently trying to get a react button to "make a selection" in a react-select multi input dropdown.我目前正在尝试在 react-select 多输入下拉列表中获取一个反应按钮以“进行选择”。

I want my application to allow users to select items from both the drop-down and through the button.我希望我的应用程序允许用户从下拉列表和按钮中选择项目。 When the button is clicked however, it the react-select (dropdown) element should show the value selected by the button.但是,当单击按钮时,react-select(下拉)元素应显示按钮选择的值。 It makes functionality more intuitive for my specific use case.它使我的特定用例的功能更加直观。

This is what my select (multi-dropdown from react-select module) and button components look like:这是我的选择(来自 react-select 模块的多下拉菜单)和按钮组件的样子:

<Select
  options={props.options}
  isMulti
  onChange={handleDropdown}
  placeholder={props.placeholder}
/>

<Button
  onClick={addSelection}
/>

This is what my functions look like:这是我的功能的样子:

  const handleDropdown = (selected) =>{
  
  //Saving the array of selected items into a variable (selected_values)
  selected_values = selected
}

  const addSelection = (event) =>{
  
  //make dropdown value "car" appear as selected in select component (dropdown)
}

So far, I am kind of lost.到目前为止,我有点失落。 I'm pretty new to the React way of doing things.我对 React 的做事方式很陌生。

I have tried using refs in order to target the dropdown, but it doesnt yield any results.我曾尝试使用 refs 来定位下拉列表,但它没有产生任何结果。 The react-select module documentation reccomends modifying component props in order to access their dropdown actions, but this doesnt work for my particular case, as I cant use it from an external component (the button I want to use). react-select 模块文档建议修改组件道具以访问它们的下拉操作,但这不适用于我的特定情况,因为我无法从外部组件(我想使用的按钮)使用它。

I appreciate any response that may help me work towards a solution.我感谢任何可以帮助我寻求解决方案的回应。 A push in the right direction would be appreciated.朝着正确的方向推动将不胜感激。

So, after some reconsideration and some help from the comments, this is the solution to this particular problem:因此,经过一些重新考虑和评论的帮助,这是这个特定问题的解决方案:

This solution is for a class component此解决方案适用于类组件

Component tags:组件标签:

<Select
  value={this.state.selectedValues}
  options={this.state.options}
  isMulti
/>

<Button
  onClick={this.addSelection.bind(this)}
  {/* Working in a class component, so "this" was added, and it was binded
      in order to be able to access it in its addSelection function */}
/>

Function:功能:

  //Part of class component:
  addSelection () {
  this.setState({selectedValues: {value: 280, label: "Car"}})
  }

Basically, we are setting the value of the dropdown to be held inside a state ( selectedValues ).基本上,我们将下拉列表的值设置为保持在状态( selectedValues )内。 From there, we can modify its value through the click of a button external to the Select Component.从那里,我们可以通过单击 Select Component 外部的按钮来修改其值。 Once states are modified, they trigger a re-render of components, which in turn updates the value of the dropdown component.一旦状态被修改,它们就会触发组件的重新渲染,进而更新下拉组件的值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM