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

Clee/resolve regression #2259

Merged
merged 5 commits into from
Jan 4, 2018
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
9 changes: 8 additions & 1 deletion examples/src/components/States.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ var StatesField = createClass({
rtl: false,
};
},
clearValue (e) {
this.select.setInputValue('');
},
switchCountry (e) {
var newCountry = e.target.value;
this.setState({
Expand Down Expand Up @@ -54,7 +57,9 @@ var StatesField = createClass({
<h3 className="section-heading">{this.props.label} <a href="https://github.com/JedWatson/react-select/tree/master/examples/src/components/States.js">(Source)</a></h3>
<Select
id="state-select"
ref="stateSelect"
ref={(ref) => { this.select = ref; }}
onBlurResetsInput={false}
onSelectResetsInput={false}
autoFocus
options={options}
simpleValue
Expand All @@ -68,6 +73,8 @@ var StatesField = createClass({
searchable={this.state.searchable}
/>
<button style={{ marginTop: '15px' }} type="button" onClick={this.focusStateSelect}>Focus Select</button>
<button style={{ marginTop: '15px' }} type="button" onClick={this.clearValue}>Clear Value</button>

<div className="checkbox-list">

<label className="checkbox">
Expand Down
28 changes: 22 additions & 6 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@ class Select extends React.Component {
});
}

setInputValue(newValue) {
if (this.props.onInputChange) {
let nextState = this.props.onInputChange(newValue);
if (nextState != null && typeof nextState !== 'object') {
newValue = '' + nextState;
}
}
this.setState({
inputValue: newValue
});
}

handleInputValueChange(newValue) {
if (this.props.onInputChange) {
let nextState = this.props.onInputChange(newValue);
Expand Down Expand Up @@ -589,10 +601,10 @@ class Select extends React.Component {
const required = this.handleRequired(value, this.props.multi);
this.setState({ required });
}
if (this.props.simpleValue && value) {
value = this.props.multi ? value.map(i => i[this.props.valueKey]).join(this.props.delimiter) : value[this.props.valueKey];
}
if (this.props.onChange) {
if (this.props.simpleValue && value) {
value = this.props.multi ? value.map(i => i[this.props.valueKey]).join(this.props.delimiter) : value[this.props.valueKey];
}
this.props.onChange(value);
}
}
Expand Down Expand Up @@ -719,11 +731,15 @@ class Select extends React.Component {
.filter(option => !option.option.disabled);
this._scrollToFocusedOptionOnUpdate = true;
if (!this.state.isOpen) {
this.setState({
const newState = {
...this.state,
isOpen: true,
inputValue: '',
focusedOption: this._focusedOption || (options.length ? options[dir === 'next' ? 0 : options.length - 1].option : null)
});
};
if (this.props.onSelectResetsInput) {
newState.inputValue = '';
}
this.setState(newState);
return;
}
if (!options.length) return;
Expand Down
2 changes: 1 addition & 1 deletion test/Async-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ describe('Async', () => {
});

it('should not update options on componentWillReceiveProps', () => {
const props = {options: []};
const props = { options: [] };
createControl(props);

const setStateStub = sinon.stub(asyncInstance, 'setState');
Expand Down
23 changes: 22 additions & 1 deletion test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2138,6 +2138,24 @@ describe('Select', () => {
];

describe('with single select', () => {
it('should have retained inputValue after accepting selection with onSelectResetsInput=false, when navigating via keyboard', () => {
wrapper = createControlWithWrapper({
value: '',
options: options,
onSelectResetsInput: false,
onCloseResetsInput: false,
onBlurResetsInput: false,
simpleValue: true,
});
clickArrowToOpen();
typeSearchText('tw');
pressEnterToAccept();
setValueProp('two');

expect(instance.state.inputValue, 'to equal', 'tw');
expect(instance, 'to contain', <div><span className="Select-value-label">Two</span></div>);
expect(instance, 'to contain', <input value="tw"/>);
});
it('should have retained inputValue after accepting selection with onSelectResetsInput=false', () => {
// Render an instance of the component
wrapper = createControlWithWrapper({
Expand All @@ -2146,6 +2164,7 @@ describe('Select', () => {
onSelectResetsInput: false,
onCloseResetsInput: false,
onBlurResetsInput: false,
simpleValue: true,
});

clickArrowToOpen();
Expand Down Expand Up @@ -2199,7 +2218,8 @@ describe('Select', () => {
value: '',
options: options,
multi: true,
onSelectResetsInput: false
onSelectResetsInput: false,
simpleValue: true,
});

clickArrowToOpen();
Expand All @@ -2216,6 +2236,7 @@ describe('Select', () => {
value: '',
options: options,
multi: true,
simpleValue: true,
});

clickArrowToOpen();
Expand Down