简体   繁体   English

Select 使用 React 下拉列表中的值

[英]Select a value from dropdown using React

I am making a very simple autocomplete section in a react application.我在反应应用程序中制作了一个非常简单的自动完成部分。

Code as follows,代码如下,

index.js index.js

import React from 'react';
import { render } from 'react-dom';
import Autocomplete from './Autocomplete';

const styles = {
  fontFamily: 'sans-serif',
  textAlign: 'left',
};

const items = ['Moscow', 'Ufa', 'Tver', 'Alma ata'];

function onAutoCompleteHandle(val) {
  alert(val);
}

const App = () => (
  <div style={styles}>
    <Autocomplete items={items} onAutocomplete={onAutoCompleteHandle.bind(this)}/>
  </div>
);

render(<App />, document.getElementById('root'));

autocomplete.js自动完成.js

Render method:渲染方法:

const showSuggest = {
      display: this.state.show ? 'block' : 'none',
    }
    return (
      <div>
      <input type="text" 
        className="autoCompleteInput"
        onChange={this.handleChange}
        ref={input => { this.textInput = input; }} 
        onClick={this.handleClick}
        onKeyPress={this.handleKeyPress}
      />
      <span className='suggestWrapper' style={showSuggest}>
          <ul className='suggestAutocomplete'>
          {this.state.items.map((item, i) => {
            return 
            <li key={i} onClick={this.selectSuggestion}>
              {item}
            </li>
          })}
        </ul>
      </span>
      </div>
    )

Complete working example: https://codesandbox.io/s/react-autocomplete-nbo3n完整的工作示例: https://codesandbox.io/s/react-autocomplete-nbo3n

Steps to reproduce in the above given sandbox example:在上面给出的沙盒示例中重现的步骤:

-> Click on the input box. ->点击输入框。

-> Enter single alphabet eg.., a . ->输入单个字母,例如.., a .

-> This gives the two items as result Ufa , Alma ata . ->这给出了两个项目作为结果Ufa , Alma ata

-> Press the down arrow key in keyboard. ->按键盘上的向下箭头键。

As nothing happens here, unable to select any of the dropdown items.由于此处没有任何反应,因此无法 select 任何下拉项目。

As of now things work only if we move the mouse over the dropdown and select any item but I am in the need to implement the same behaviour for key down and enter.到目前为止,只有当我们将鼠标移到下拉列表和 select 任何项目上时,事情才会起作用,但我需要为按键和输入实现相同的行为。

Expected behaviour:预期行为:

-> On keydown/keyup should be able to navigate the dropdown list items. ->在 keydown/keyup 上应该能够导航下拉列表项。

-> On click enter key on an item then that item should be the selected one. ->在一个项目上单击输入键,那么该项目应该是选定的。

I have tried assigning ref={input => { this.textInput = input; }}我试过分配ref={input => { this.textInput = input; }} ref={input => { this.textInput = input; }} to the ul list items suggestAutocomplete but that also doesn't help.. ref={input => { this.textInput = input; }}到 ul 列表项suggestAutocomplete ,但这也无济于事..

Really I am stuck with this for very very long time.真的,我被这个困扰了很长时间。 I humbly request you to consider this question.我谦虚地请求你考虑这个问题。

It is also okay if you change this existing code, but I need to have both mouse selection and keyboard selection as well in the autocomplete..如果您更改此现有代码也可以,但我需要在自动完成中同时进行鼠标选择和键盘选择。

  1. Initialize a state with value -1用值 -1 初始化 state

  2. Add an event on keyDown在 keyDown 上添加事件

something like this:像这样的东西:

handleKeyDown(e) {
  if (e.keyCode == 40) { //down
    this.setState({active: ++this.state.active})
  } else if (e.keyCode == 38) { //up
    this.setState({active: --this.state.active})
  } else if (e.keyCode == 13) { //enter
    e.preventDefault();
    if (this.state.active > -1) {
      this.selectSuggestion();
    }
  }
};
  1. On click, reset the state to -1 again.单击时,将 state 再次重置为 -1。

Check this: https://codesandbox.io/s/react-autocomplete-cf2yd检查这个: https://codesandbox.io/s/react-autocomplete-cf2yd

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

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