Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix creating new option on key down on unexpected keys #2554

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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