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

Don't open drop down menu when clear values #2198

Merged
merged 2 commits into from
Dec 14, 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
25 changes: 22 additions & 3 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ class Select extends React.Component {
isPseudoFocused: false,
});
}

return;
}

Expand All @@ -263,6 +264,8 @@ class Select extends React.Component {
this.focus();

let input = this.input;
let toOpen = true;

if (typeof input.getInput === 'function') {
// Get the actual DOM input if the ref is an <AutosizeInput /> component
input = input.getInput();
Expand All @@ -271,9 +274,14 @@ class Select extends React.Component {
// clears the value so that the cursor will be at the end of input when the component re-renders
input.value = '';

if (this._focusAfterClear) {
toOpen = false;
this._focusAfterClear = false;
}

// if the input is focused, ensure the menu is open
this.setState({
isOpen: true,
isOpen: toOpen,
isPseudoFocused: false,
});
} else {
Expand Down Expand Up @@ -310,6 +318,7 @@ class Select extends React.Component {
if (this.props.disabled || (event.type === 'mousedown' && event.button !== 0)) {
return;
}

event.stopPropagation();
event.preventDefault();

Expand All @@ -335,15 +344,21 @@ class Select extends React.Component {

handleInputFocus (event) {
if (this.props.disabled) return;
var isOpen = this.state.isOpen || this._openAfterFocus || this.props.openOnFocus;

let toOpen = this.state.isOpen || this._openAfterFocus || this.props.openOnFocus;
toOpen = this._focusAfterClear ? false : toOpen; //if focus happens after clear values, don't open dropdown yet.

if (this.props.onFocus) {
this.props.onFocus(event);
}

this.setState({
isFocused: true,
isOpen: isOpen,
isOpen: toOpen,
});

this._openAfterFocus = false;
this._focusAfterClear = false;
}

handleInputBlur (event) {
Expand Down Expand Up @@ -622,12 +637,16 @@ class Select extends React.Component {
if (event && event.type === 'mousedown' && event.button !== 0) {
return;
}

event.preventDefault();

this.setValue(this.getResetValue());
this.setState({
isOpen: false,
inputValue: this.handleInputValueChange(''),
}, this.focus);

this._focusAfterClear = true;
}

getResetValue () {
Expand Down
37 changes: 37 additions & 0 deletions test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2799,6 +2799,7 @@ describe('Select', () => {
expect(options, 'to have length', 2);
});
});

describe('empty filterOptions function', () => {

beforeEach(() => {
Expand Down Expand Up @@ -3267,6 +3268,42 @@ describe('Select', () => {
});
});

describe('clearValues', () => {
let instance = null;
const preventDefault = sinon.spy();
const event = {
'preventDefault': preventDefault,
'type': 'mousedown',
'button': 0
};

beforeEach(() => {
instance = createControl({
options: defaultOptions,
multi: true,
openOnFocus: true,
value: ['two', 'one']
});
});

it('after clearValue called, menu shall remain closed', () => {

instance.clearValue(event);

expect(instance.state.isOpen, 'to be falsy');
expect(instance._focusAfterClear, 'to be true');
expect(preventDefault, 'was called once');
});

it('click on Clear button, menu shall remain closed', () => {

const domNode = ReactDOM.findDOMNode(instance).querySelector('.Select-clear-zone');

TestUtils.Simulate.mouseDown(domNode, event);
expect(instance.state.isOpen, 'to be falsy');
});
});

describe('onValueClick', () => {
var onValueClick;

Expand Down