Skip to content
Merged
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
14 changes: 12 additions & 2 deletions src/Creatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ const Creatable = createClass({
};
},

getInitialState () {
return {
newOptions: []
};
},

createNewOption () {
const {
isValidNewOption,
Expand All @@ -92,7 +98,7 @@ const Creatable = createClass({
if (onNewOptionClick) {
onNewOptionClick(option);
} else {
options.unshift(option);
this.setState({ newOptions: [option, ...this.state.newOptions] });

this.select.selectValue(option);
}
Expand All @@ -101,7 +107,7 @@ const Creatable = createClass({
},

filterOptions (...params) {
const { filterOptions, isValidNewOption, options, promptTextCreator } = this.props;
const { filterOptions, isValidNewOption, promptTextCreator } = 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 @@ -213,6 +219,7 @@ const Creatable = createClass({
const {
newOptionCreator,
shouldKeyDownEventCreateNewOption,
options: propOptions = [],
...restProps
} = this.props;

Expand All @@ -225,8 +232,11 @@ const Creatable = createClass({
children = defaultChildren;
}

const options = [...this.state.newOptions, ...propOptions];

const props = {
...restProps,
options,
allowCreate: true,
filterOptions: this.filterOptions,
menuRenderer: this.menuRenderer,
Expand Down
14 changes: 8 additions & 6 deletions test/Creatable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ describe('Creatable', () => {
});
typeSearchText('foo');
TestUtils.Simulate.mouseDown(creatableNode.querySelector('.Select-create-option-placeholder'));
expect(options, 'to have length', 1);
expect(options[0].label, 'to equal', 'foo');
expect(selectedOption, 'to be', options[0]);
const newOptions = creatableInstance.state.newOptions;
expect(newOptions, 'to have length', 1);
expect(newOptions[0].label, 'to equal', 'foo');
expect(selectedOption, 'to equal', newOptions[0]);
});

it('should create (and auto-select) a new option when ENTER is pressed while placeholder option is selected', () => {
Expand All @@ -141,9 +142,10 @@ describe('Creatable', () => {
});
typeSearchText('foo');
TestUtils.Simulate.keyDown(filterInputNode, { keyCode: 13 });
expect(options, 'to have length', 1);
expect(options[0].label, 'to equal', 'foo');
expect(selectedOption, 'to be', options[0]);
const newOptions = creatableInstance.state.newOptions;
expect(newOptions, 'to have length', 1);
expect(newOptions[0].label, 'to equal', 'foo');
expect(selectedOption, 'to be', newOptions[0]);
});

it('should not create a new option if the placeholder option is not selected but should select the focused option', () => {
Expand Down