Skip to content

Commit

Permalink
Merge pull request #1189 from jooj123/patch/handle-touch-outside
Browse files Browse the repository at this point in the history
Handle touch outside to dismiss menu
  • Loading branch information
JedWatson authored Sep 4, 2016
2 parents 00dd299 + 9267932 commit 15d6373
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ const Select = React.createClass({

componentWillUpdate (nextProps, nextState) {
if (nextState.isOpen !== this.state.isOpen) {
this.toggleTouchOutsideEvent(nextState.isOpen);
const handler = nextState.isOpen ? nextProps.onOpen : nextProps.onClose;
handler && handler();
}
Expand Down Expand Up @@ -224,6 +225,25 @@ const Select = React.createClass({
}
},

componentWillUnmount() {
document.removeEventListener('touchstart', this.handleTouchOutside);
},

toggleTouchOutsideEvent(enabled) {
if (enabled) {
document.addEventListener('touchstart', this.handleTouchOutside);
} else {
document.removeEventListener('touchstart', this.handleTouchOutside);
}
},

handleTouchOutside(event) {
// handle touch outside on ios to dismiss menu
if (this.wrapper && !this.wrapper.contains(event.target)) {
this.closeMenu();
}
},

focus () {
if (!this.input) return;
this.input.focus();
Expand Down
17 changes: 11 additions & 6 deletions test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3324,24 +3324,29 @@ describe('Select', () => {
});
});

describe('clicking outside', () => {
describe('outside event', () => {

beforeEach(() => {

instance = createControl({
options: defaultOptions
});
});

it('closes the menu', () => {

TestUtils.Simulate.mouseDown(getSelectControl(instance), { button: 0 });
expect(ReactDOM.findDOMNode(instance), 'queried for', '.Select-option',
'to have length', 4);
});

it('click closes the menu', () => {
TestUtils.Simulate.blur(searchInputNode);
expect(ReactDOM.findDOMNode(instance), 'to contain no elements matching', '.Select-option');
});

it('touch closes the menu', () => {
const evt = document.createEvent('Event');
evt.initEvent('touchstart', true, true);
document.querySelector('body').dispatchEvent(evt);
expect(ReactDOM.findDOMNode(instance), 'to contain no elements matching', '.Select-option');
});

});

describe('with autosize=false', () => {
Expand Down

0 comments on commit 15d6373

Please sign in to comment.