编辑react-select的最后一个输入

Akz*_*kza 3 reactjs react-select

我正在使用反应选择,只是注意到当我在输入字段中已经有值时(通过用户类型或通过选择菜单列表中的选项),然后我模糊输入然后再次聚焦它 - 尝试编辑我的最后一个输入 - 它只是从头开始,而不是从输入的最后一个字符继续。我刚刚在作者的github上发现了这个问题。它是两年前提出的,目前仍然是一个悬而未决的问题。真的没有解决方法可以实现这一目标吗?

Lau*_*ura 6

我建议您使用受控道具inputValue并与以下代码value配对:onChangeonInputChange

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      inputValue: "",
      value: ""
    };
  }
  onInputChange = (option, { action }) => {
    console.log(option, action);
    if (action === "input-change") {
      const optionLength = option.length;
      const inputValue = this.state.inputValue;
      const inputValueLength = inputValue.length;

      const newInputValue =
        optionLength < inputValueLength
          ? option
          : this.state.inputValue + option[option.length - 1];
      this.setState({
        inputValue: newInputValue
      });
    }
  };
  onChange = option => {
    this.setState({
      value: option
    });
  };
  render() {
    return (
      <div className="App">
        <Select
          options={options}
          onChange={this.onChange}
          onInputChange={this.onInputChange}
          inputValue={this.state.inputValue}
          value={this.state.value}
        />
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

在react-select v1中,propsonSelectResetsInput设置为false完成这项工作,但我想对于v2,你将需要做这个技巧。

这是一个活生生的例子