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

Hide create option after closing menu #1306

Merged
merged 2 commits into from
Jun 23, 2017
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
32 changes: 19 additions & 13 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,12 @@ const Select = React.createClass({
this.setState({
isOpen: false,
isPseudoFocused: this.state.isFocused && !this.props.multi,
inputValue: ''
inputValue: this.handleInputValueChange('')
});
} else {
this.setState({
isOpen: false,
isPseudoFocused: this.state.isFocused && !this.props.multi,
inputValue: this.state.inputValue
isPseudoFocused: this.state.isFocused && !this.props.multi
});
}
this.hasScrolledToOption = false;
Expand Down Expand Up @@ -436,20 +435,16 @@ const Select = React.createClass({
isPseudoFocused: false,
};
if (this.props.onBlurResetsInput) {
onBlurredState.inputValue = '';
onBlurredState.inputValue = this.handleInputValueChange('');
}
this.setState(onBlurredState);
},

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

if (this.state.inputValue !== event.target.value && this.props.onInputChange) {
let nextState = this.props.onInputChange(newInputValue);
// Note: != used deliberately here to catch undefined and null
if (nextState != null && typeof nextState !== 'object') {
newInputValue = '' + nextState;
}
if (this.state.inputValue !== event.target.value) {
newInputValue = this.handleInputValueChange(newInputValue);
}

this.setState({
Expand All @@ -459,6 +454,17 @@ const Select = React.createClass({
});
},

handleInputValueChange(newValue) {
if (this.props.onInputChange) {
let nextState = this.props.onInputChange(newValue);
// Note: != used deliberately here to catch undefined and null
if (nextState != null && typeof nextState !== 'object') {
newValue = '' + nextState;
}
}
return newValue;
},

handleKeyDown (event) {
if (this.props.disabled) return;

Expand Down Expand Up @@ -603,15 +609,15 @@ const Select = React.createClass({
this.hasScrolledToOption = false;
if (this.props.multi) {
this.setState({
inputValue: '',
inputValue: this.handleInputValueChange(''),
focusedIndex: null
}, () => {
this.addValue(value);
});
} else {
this.setState({
isOpen: false,
inputValue: '',
inputValue: this.handleInputValueChange(''),
isPseudoFocused: this.state.isFocused,
}, () => {
this.setValue(value);
Expand Down Expand Up @@ -648,7 +654,7 @@ const Select = React.createClass({
this.setValue(this.getResetValue());
this.setState({
isOpen: false,
inputValue: '',
inputValue: this.handleInputValueChange(''),
}, this.focus);
},

Expand Down
29 changes: 25 additions & 4 deletions test/Creatable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var TestUtils = require('react-addons-test-utils');
var Select = require('../src/Select');

describe('Creatable', () => {
let creatableInstance, creatableNode, filterInputNode, innserSelectInstance, renderer;
let creatableInstance, creatableNode, filterInputNode, innerSelectInstance, renderer;

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

Expand All @@ -36,7 +36,7 @@ describe('Creatable', () => {
<Select.Creatable {...props} />
);
creatableNode = ReactDOM.findDOMNode(creatableInstance);
innserSelectInstance = creatableInstance.select;
innerSelectInstance = creatableInstance.select;
findAndFocusInputControl();
};

Expand Down Expand Up @@ -106,7 +106,7 @@ describe('Creatable', () => {
createControl({
filterOptions: () => null
});
typeSearchText('test');;
typeSearchText('test');
});

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

it('should remove the new option after closing on selecting option', () => {
createControl();
typeSearchText('9');
TestUtils.Simulate.keyDown(filterInputNode, { keyCode: 40, key: 'ArrowDown' });
TestUtils.Simulate.keyDown(filterInputNode, { keyCode: 13 });
expect(creatableInstance.inputValue, 'to equal', '');
});

it('should remove the new option after closing on escape', () => {
createControl();
typeSearchText('9');
TestUtils.Simulate.keyDown(filterInputNode, { keyCode: 27 });
expect(creatableInstance.inputValue, 'to equal', '');
});

it('should remove the new option after closing on blur', () => {
createControl();
typeSearchText('9');
TestUtils.Simulate.blur(filterInputNode);
expect(creatableInstance.inputValue, 'to equal', '');
});

it('should allow a custom select type to be rendered via the :children property', () => {
let childProps;
createControl({
Expand Down