Skip to content

Commit

Permalink
Add option to show new creatable item as first or last option
Browse files Browse the repository at this point in the history
  • Loading branch information
Stenerson committed Dec 15, 2016
1 parent ef3a468 commit 9c963d0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ Property | Type | Description
`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` |
`promptTextCreator` | function | Factory for overriding default option creator prompt label. By default it will read 'Create option "{label}"'. Expected signature: `(label: String): String` |
`showNewOptionAtTop` | bool | `true`: (Default) Show new option at top of list <br> `false`: Show new option at bottom of list |

### Combining Async and Creatable

Expand Down
14 changes: 12 additions & 2 deletions src/Creatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ const Creatable = React.createClass({

// Decides if a keyDown event (eg its `keyCode`) should result in the creation of a new option.
shouldKeyDownEventCreateNewOption: React.PropTypes.func,

// Creates prompt/placeholder option text.
// true: new option prompt at top of list (default)
// false: new option prompt at bottom of list
showNewOptionAtTop: React.PropTypes.bool,
},

// Default prop methods
Expand All @@ -68,6 +73,7 @@ const Creatable = React.createClass({
menuRenderer: defaultMenuRenderer,
newOptionCreator,
promptTextCreator,
showNewOptionAtTop: true,
shouldKeyDownEventCreateNewOption,
};
},
Expand Down Expand Up @@ -99,7 +105,7 @@ const Creatable = React.createClass({
},

filterOptions (...params) {
const { filterOptions, isValidNewOption, options, promptTextCreator } = this.props;
const { filterOptions, isValidNewOption, options, promptTextCreator, showNewOptionAtTop } = this.props;

// TRICKY Check currently selected options as well.
// Don't display a create-prompt for a value that's selected.
Expand Down Expand Up @@ -133,7 +139,11 @@ const Creatable = React.createClass({
valueKey: this.valueKey
});

filteredOptions.unshift(this._createPlaceholderOption);
if (showNewOptionAtTop) {
filteredOptions.unshift(this._createPlaceholderOption);
} else {
filteredOptions.push(this._createPlaceholderOption);
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/Creatable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ describe('Creatable', () => {
expect(creatableNode.querySelector('.Select-create-option-placeholder'), 'to have text', Select.Creatable.promptTextCreator('foo'));
});

it('should add a placeholder "create..." prompt as last option when showNewOptionAtTop is false', () => {
createControl({
showNewOptionAtTop: false
});
const searchTerm = 'Th';
typeSearchText(searchTerm);
let nodes = creatableNode.querySelectorAll('.Select-option');
expect(nodes, 'to have length', 2); // [Three, Create "th"?]
expect(nodes[nodes.length-1], 'to have text', Select.Creatable.promptTextCreator(searchTerm));
});

it('should not show a "create..." prompt if current filter text is an exact match for an existing option', () => {
createControl({
isOptionUnique: () => false
Expand Down

0 comments on commit 9c963d0

Please sign in to comment.