newOptionCreator is called twice for every key pressed?
See original GitHub issueCreated 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:
- Created 7 years ago
- Reactions:4
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I discovered also that if you manually set the
shouldKeyDownEventCreateNewOption={(keyCode) => keyCode === 13}
prop onCreatable
to handle enter (even though it should be set by default) the two press bug disappears.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.