question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

newOptionCreator is called twice for every key pressed?

See original GitHub issue

Created this simple demo to illustrate: (sorry, I tried to use plunker, but could not import Creatable)

import React from 'react'

import {Creatable} from 'react-select'
import '/node_modules/react-select/dist/react-select.css'

const optionList = [
  {value: 1, label: 'one'},
  {value: 2, label: 'two'},
  {value: 3, label: 'three'}
];

export const SelectCreate = React.createClass({

  getInitialState () {
    return {
      options: [
        {value: 2, label: 'two'}
      ]
    };
  },

  onChange (options) {
    console.log('onChange', options);
    this.setState({options: options});
  },

  newOption (obj) {
    console.log('newOption', obj);
    return {
      [obj.labelKey]: obj.label,
      [obj.valueKey]: obj.label
    };
  },

  validateOption (obj) {
    return obj && obj.label && obj.label.length > 1;
  },

  render () {
    return (
      <Creatable
        name="select-create"
        multi={true}
        newOptionCreator={this.newOption}
        isValidNewOption={this.validateOption}
        value={this.state.options}
        options={optionList}
        onChange={this.onChange}
        clearable={true}
      />
    )
  }
});

When typing ‘qwerty’ in the select input field, the console output looks like this:

newOption Object {label: "qw", labelKey: "label", valueKey: "value"}
newOption Object {label: "Create option "qw"", labelKey: "label", valueKey: "value"}
newOption Object {label: "qwe", labelKey: "label", valueKey: "value"}
newOption Object {label: "Create option "qwe"", labelKey: "label", valueKey: "value"}
newOption Object {label: "qwer", labelKey: "label", valueKey: "value"}
newOption Object {label: "Create option "qwer"", labelKey: "label", valueKey: "value"}
newOption Object {label: "qwert", labelKey: "label", valueKey: "value"}
newOption Object {label: "Create option "qwert"", labelKey: "label", valueKey: "value"}
newOption Object {label: "qwerty", labelKey: "label", valueKey: "value"}
newOption Object {label: "Create option "qwerty"", labelKey: "label", valueKey: "value"}

(It does work BTW; if I press TAB, the new option ‘qwerty’ is added to the options.)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
TylerWardlecommented, Dec 14, 2016

I discovered also that if you manually set the shouldKeyDownEventCreateNewOption={(keyCode) => keyCode === 13} prop on Creatable to handle enter (even though it should be set by default) the two press bug disappears.

2reactions
willcosgrovecommented, Oct 30, 2016

I’ve got the same issue with my project. In addition, it takes two enter presses, or tab presses, or clicks on the option to get the option selected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

KeyTyped being called twice in this code? - Stack Overflow
For some reason, when I am sending the message to the server, every message is received twice for any single keystroke! for instance, ......
Read more >
react-select-legacy - npm
React-Select v2 is a complete rewrite, all new with some major API ... selectedOption can be null when the `x` (close) button is...
Read more >
keyboard keys being entered twice each time the key is pressed.
I have been typing for 20 years and started to think I was loosing my mind not being able to type accurately!!!!!!! Thanks...
Read more >
A fork of react-select with support for option groups
It decorates a Select and so it supports all of the default ... whether to select the currently focused value when the [tab]...
Read more >
github.com-JedWatson-react-select_-_2017-11-25_09-05-50
It decorates a Select and so it supports all of the default ... boolean | true | whether pressing delete key removes the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found