Skip to content

Commit b068247

Browse files
andremeagirton
authored andcommitted
Hide create option after closing menu (JedWatson#1306)
* hide create option after closing menu * cleaned up tests
1 parent d8a34ec commit b068247

2 files changed

Lines changed: 44 additions & 17 deletions

File tree

src/Select.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,12 @@ const Select = createClass({
396396
this.setState({
397397
isOpen: false,
398398
isPseudoFocused: this.state.isFocused && !this.props.multi,
399-
inputValue: ''
399+
inputValue: this.handleInputValueChange('')
400400
});
401401
} else {
402402
this.setState({
403403
isOpen: false,
404-
isPseudoFocused: this.state.isFocused && !this.props.multi,
405-
inputValue: this.state.inputValue
404+
isPseudoFocused: this.state.isFocused && !this.props.multi
406405
});
407406
}
408407
this.hasScrolledToOption = false;
@@ -437,20 +436,16 @@ const Select = createClass({
437436
isPseudoFocused: false,
438437
};
439438
if (this.props.onBlurResetsInput) {
440-
onBlurredState.inputValue = '';
439+
onBlurredState.inputValue = this.handleInputValueChange('');
441440
}
442441
this.setState(onBlurredState);
443442
},
444443

445444
handleInputChange (event) {
446445
let newInputValue = event.target.value;
447446

448-
if (this.state.inputValue !== event.target.value && this.props.onInputChange) {
449-
let nextState = this.props.onInputChange(newInputValue);
450-
// Note: != used deliberately here to catch undefined and null
451-
if (nextState != null && typeof nextState !== 'object') {
452-
newInputValue = '' + nextState;
453-
}
447+
if (this.state.inputValue !== event.target.value) {
448+
newInputValue = this.handleInputValueChange(newInputValue);
454449
}
455450

456451
this.setState({
@@ -460,6 +455,17 @@ const Select = createClass({
460455
});
461456
},
462457

458+
handleInputValueChange(newValue) {
459+
if (this.props.onInputChange) {
460+
let nextState = this.props.onInputChange(newValue);
461+
// Note: != used deliberately here to catch undefined and null
462+
if (nextState != null && typeof nextState !== 'object') {
463+
newValue = '' + nextState;
464+
}
465+
}
466+
return newValue;
467+
},
468+
463469
handleKeyDown (event) {
464470
if (this.props.disabled) return;
465471

@@ -610,15 +616,15 @@ const Select = createClass({
610616
this.hasScrolledToOption = false;
611617
if (this.props.multi) {
612618
this.setState({
613-
inputValue: '',
619+
inputValue: this.handleInputValueChange(''),
614620
focusedIndex: null
615621
}, () => {
616622
this.addValue(value);
617623
});
618624
} else {
619625
this.setState({
620626
isOpen: false,
621-
inputValue: '',
627+
inputValue: this.handleInputValueChange(''),
622628
isPseudoFocused: this.state.isFocused,
623629
}, () => {
624630
this.setValue(value);
@@ -664,7 +670,7 @@ const Select = createClass({
664670
this.setValue(this.getResetValue());
665671
this.setState({
666672
isOpen: false,
667-
inputValue: '',
673+
inputValue: this.handleInputValueChange(''),
668674
}, this.focus);
669675
},
670676

test/Creatable-test.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var TestUtils = require('react-addons-test-utils');
1919
var Select = require('../src/Select');
2020

2121
describe('Creatable', () => {
22-
let creatableInstance, creatableNode, filterInputNode, innserSelectInstance, renderer;
22+
let creatableInstance, creatableNode, filterInputNode, innerSelectInstance, renderer;
2323

2424
beforeEach(() => renderer = TestUtils.createRenderer());
2525

@@ -36,7 +36,7 @@ describe('Creatable', () => {
3636
<Select.Creatable {...props} />
3737
);
3838
creatableNode = ReactDOM.findDOMNode(creatableInstance);
39-
innserSelectInstance = creatableInstance.select;
39+
innerSelectInstance = creatableInstance.select;
4040
findAndFocusInputControl();
4141
};
4242

@@ -106,7 +106,7 @@ describe('Creatable', () => {
106106
createControl({
107107
filterOptions: () => null
108108
});
109-
typeSearchText('test');;
109+
typeSearchText('test');
110110
});
111111

112112
it('should not show a "create..." prompt if current filter text is not a valid option (as determined by :isValidNewOption prop)', () => {
@@ -150,14 +150,35 @@ describe('Creatable', () => {
150150
const options = [{ label: 'One', value: 1 }];
151151
createControl({
152152
options,
153-
shouldKeyDownEventCreateNewOption: ({ keyCode }) => keyCode === 13
154153
});
155154
typeSearchText('on'); // ['Create option "on"', 'One']
156155
TestUtils.Simulate.keyDown(filterInputNode, { keyCode: 40, key: 'ArrowDown' }); // Select 'One'
157156
TestUtils.Simulate.keyDown(filterInputNode, { keyCode: 13 });
158157
expect(options, 'to have length', 1);
159158
});
160159

160+
it('should remove the new option after closing on selecting option', () => {
161+
createControl();
162+
typeSearchText('9');
163+
TestUtils.Simulate.keyDown(filterInputNode, { keyCode: 40, key: 'ArrowDown' });
164+
TestUtils.Simulate.keyDown(filterInputNode, { keyCode: 13 });
165+
expect(creatableInstance.inputValue, 'to equal', '');
166+
});
167+
168+
it('should remove the new option after closing on escape', () => {
169+
createControl();
170+
typeSearchText('9');
171+
TestUtils.Simulate.keyDown(filterInputNode, { keyCode: 27 });
172+
expect(creatableInstance.inputValue, 'to equal', '');
173+
});
174+
175+
it('should remove the new option after closing on blur', () => {
176+
createControl();
177+
typeSearchText('9');
178+
TestUtils.Simulate.blur(filterInputNode);
179+
expect(creatableInstance.inputValue, 'to equal', '');
180+
});
181+
161182
it('should allow a custom select type to be rendered via the :children property', () => {
162183
let childProps;
163184
createControl({

0 commit comments

Comments
 (0)