diff --git a/lib/Autocomplete.js b/lib/Autocomplete.js index daceb0ed..60922c35 100644 --- a/lib/Autocomplete.js +++ b/lib/Autocomplete.js @@ -1,5 +1,6 @@ const React = require('react') const scrollIntoView = require('dom-scroll-into-view') +const throttle = require('lodash/throttle') let _debugStates = [] @@ -51,6 +52,12 @@ let Autocomplete = React.createClass({ this._performAutoCompleteOnKeyUp = false }, + componentDidMount () { + this.setMenuPositionsThrottled = throttle(this.setMenuPositions.bind(this), 16); + window.addEventListener('resize', this.setMenuPositionsThrottled); + window.addEventListener('scroll', this.setMenuPositionsThrottled); + }, + componentWillReceiveProps () { this._performAutoCompleteOnUpdate = true }, @@ -67,6 +74,13 @@ let Autocomplete = React.createClass({ this.maybeScrollItemIntoView() }, + componentWillUnmount () { + window.removeEventListener('resize', this.setMenuPositionsThrottled); + window.removeEventListener('scroll', this.setMenuPositionsThrottled); + this.setMenuPositionsThrottled.cancel(); + this.setMenuPositionsThrottled = null; + }, + maybeScrollItemIntoView () { if (this.state.isOpen === true && this.state.highlightedIndex !== null) { var itemNode = React.findDOMNode(this.refs[`item-${this.state.highlightedIndex}`]) diff --git a/package.json b/package.json index a393dcd1..212c2723 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ ], "keywords": [], "dependencies": { - "dom-scroll-into-view": "1.0.1" + "dom-scroll-into-view": "1.0.1", + "lodash": "^4.0.0" } -} \ No newline at end of file +}