diff --git a/src/Option.js b/src/Option.js index 4eb85818ad..0f4c45678a 100644 --- a/src/Option.js +++ b/src/Option.js @@ -1,19 +1,7 @@ import classNames from 'classnames'; import PropTypes from 'prop-types'; import React from 'react'; - -const blockEvent = event => { - event.preventDefault(); - event.stopPropagation(); - if ((event.target.tagName !== 'A') || !('href' in event.target)) { - return; - } - if (event.target.target) { - window.open(event.target.href, event.target.target); - } else { - window.location.href = event.target.href; - } -}; +import { blockEvent } from './utils/blockEvent'; class Option extends React.Component { diff --git a/src/Select.js b/src/Select.js index f378a5a986..2fd19d209b 100644 --- a/src/Select.js +++ b/src/Select.js @@ -733,7 +733,6 @@ class Select extends React.Component { this._scrollToFocusedOptionOnUpdate = true; if (!this.state.isOpen) { const newState = { - ...this.state, focusedOption: this._focusedOption || (options.length ? options[dir === 'next' ? 0 : options.length - 1].option : null), isOpen: true, }; diff --git a/src/utils/blockEvent.js b/src/utils/blockEvent.js new file mode 100644 index 0000000000..0c97c30b42 --- /dev/null +++ b/src/utils/blockEvent.js @@ -0,0 +1,12 @@ +export default event => { + event.preventDefault(); + event.stopPropagation(); + if ((event.target.tagName !== 'A') || !('href' in event.target)) { + return; + } + if (event.target.target) { + window.open(event.target.href, event.target.target); + } else { + window.location.href = event.target.href; + } +}; diff --git a/test/Option-test.js b/test/Option-test.js index 9fa91468ed..f16904709d 100644 --- a/test/Option-test.js +++ b/test/Option-test.js @@ -9,6 +9,8 @@ import unexpected from 'unexpected'; import unexpectedDom from 'unexpected-dom'; import unexpectedSinon from 'unexpected-sinon'; +import blockEvent from '../src/utils/blockEvent'; + import Option from '../src/Option'; helper(); @@ -123,7 +125,7 @@ describe('Option component', () => { stopPropagation, }; - instance.blockEvent(event); + blockEvent(event); expect(openStub, 'was called once'); expect(openStub, 'was called with', event.target.href, event.target.target); @@ -146,7 +148,7 @@ describe('Option component', () => { expect(window.location.href, 'not to equal', event.target.href); - instance.blockEvent(event); + blockEvent(event); expect(window.location.href, 'to equal', event.target.href); expect(openStub, 'was not called'); @@ -169,7 +171,7 @@ describe('Option component', () => { expect(window.location.href, 'to equal', 'url'); - instance.blockEvent(event); + blockEvent(event); expect(window.location.href, 'to equal', 'url'); expect(openStub, 'was not called'); @@ -191,7 +193,7 @@ describe('Option component', () => { expect(window.location.href, 'to equal', 'url'); - instance.blockEvent(event); + blockEvent(event); expect(window.location.href, 'to equal', 'url'); expect(openStub, 'was not called');