Skip to content

Commit

Permalink
added tests, adjusted focusAdjacentOption logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gwyneplaine committed Jan 4, 2018
1 parent 6415163 commit c98fdd0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,9 @@ class Select extends React.Component {
// 1. There currently actually is an inputValue in state
// 2. The new value is different to the old value OR the new value is == null
// 3. The new value is not the same as the last set value OR onSelectResetsInput has been enabled
if (this.state.inputValue
&& (this.props.value !== nextProps.value || !nextProps.value)
&& (nextProps.value !== this._lastSetValue || nextProps.onSelectResetsInput)) {
this.setState({
inputValue: this.handleInputValueChange('')
});
// (this is to ensure that the value prop change is not a result of selecting a value)
if (this.state.inputValue) {
this.clearInputValue(nextProps);
}

delete this._lastSetValue;
Expand Down Expand Up @@ -193,6 +190,15 @@ class Select extends React.Component {
this.toggleTouchOutsideEvent(false);
}

clearInputValue (nextProps) {
if ((this.props.value !== nextProps.value || !nextProps.value)
&& (nextProps.value !== this._lastSetValue || nextProps.onSelectResetsInput)) {
this.setState({
inputValue: this.handleInputValueChange(''),
});
}
}

toggleTouchOutsideEvent (enabled) {
if (enabled) {
if (!document.addEventListener && document.attachEvent) {
Expand Down Expand Up @@ -731,10 +737,11 @@ class Select extends React.Component {
this._scrollToFocusedOptionOnUpdate = true;
if (!this.state.isOpen) {
const newState = {
...this.state,
isOpen: true,
focusedOption: this._focusedOption || (options.length ? options[dir === 'next' ? 0 : options.length - 1].option : null)
};
if (!this.props.onSelectResetsInput) {
if (this.props.onSelectResetsInput) {
newState.inputValue = '';
}
this.setState(newState);
Expand Down
36 changes: 35 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 Expand Up @@ -4521,4 +4542,17 @@ describe('Select', () => {
});
});
});
it('should clear the input value, if the value prop is cleared', () => {
wrapper = createControlWithWrapper({
value: '',
options: options,
onSelectResetsInput: false,
onCloseResetsInput: false,
onBlurResetsInput: false,
simpleValue: true,
});
typeSearchText('tw');
setValueProp('');
expect(instance.state.inputValue, 'to equal', '');
});
});

0 comments on commit c98fdd0

Please sign in to comment.