Skip to content

Commit

Permalink
Fix creating new option on key down on unexpected keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Pirotte committed Apr 24, 2018
1 parent 404bf14 commit 76c7efc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ function onInputKeyDown(event) {
| `isValidNewOption` | function | Determines if the current input text represents a valid option. By default any non-empty string will be considered valid. Expected signature: `({ label: string }): boolean` |
| `newOptionCreator` | function | Factory to create new option. Expected signature: `({ label: string, labelKey: string, valueKey: string }): Object` |
| `onNewOptionClick` | function | new option click handler, it calls when new option has been selected. `function(option) {}` |
| `shouldKeyDownEventCreateNewOption` | function | Decides if a keyDown event (eg its `keyCode`) should result in the creation of a new option. ENTER, TAB and comma keys create new options by default. Expected signature: `({ keyCode: number }): boolean` |
| `shouldKeyDownEventCreateNewOption` | function | Decides if a keyDown event should result in the creation of a new option. ENTER, TAB and comma keys create new options by default. Expected signature: `(event): boolean` |
| `promptTextCreator` | function | Factory for overriding default option creator prompt label. By default it will read 'Create option "{label}"'. Expected signature: `(label: String): String` |

### Methods
Expand Down
12 changes: 6 additions & 6 deletions src/Creatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class CreatableSelect extends React.Component {
if (
focusedOption &&
focusedOption === this._createPlaceholderOption &&
shouldKeyDownEventCreateNewOption({ keyCode: event.keyCode })
shouldKeyDownEventCreateNewOption(event)
) {
this.createNewOption();

Expand Down Expand Up @@ -220,11 +220,11 @@ const newOptionCreator = ({ label, labelKey, valueKey }) => {

const promptTextCreator = label => `Create option "${label}"`;

const shouldKeyDownEventCreateNewOption = ({ keyCode }) => {
switch (keyCode) {
case 9: // TAB
case 13: // ENTER
case 188: // COMMA
const shouldKeyDownEventCreateNewOption = ({ key }) => {
switch (key) {
case 'Tab':
case 'Enter':
case ',':
return true;
default:
return false;
Expand Down
12 changes: 6 additions & 6 deletions test/Creatable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,14 @@ describe('Creatable', () => {
});

it('default :shouldKeyDownEventCreateNewOption function should accept TAB, ENTER, and comma keys', () => {
function test (keyCode) {
return Select.Creatable.shouldKeyDownEventCreateNewOption({ keyCode });
function test (key) {
return Select.Creatable.shouldKeyDownEventCreateNewOption({ key });
};

expect(test(9), 'to be', true);
expect(test(13), 'to be', true);
expect(test(188), 'to be', true);
expect(test(1), 'to be', false);
expect(test('Tab'), 'to be', true);
expect(test('Enter'), 'to be', true);
expect(test(','), 'to be', true);
expect(test('a'), 'to be', false);
});

it('default :onInputKeyDown should run user provided handler.', (done) => {
Expand Down

0 comments on commit 76c7efc

Please sign in to comment.