Skip to content
This repository has been archived by the owner on Jul 19, 2019. It is now read-only.

Call setMenuPositions() on scroll & resize #69

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 14 additions & 0 deletions lib/Autocomplete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const React = require('react')
const scrollIntoView = require('dom-scroll-into-view')
const throttle = require('lodash/throttle')

let _debugStates = []

Expand Down Expand Up @@ -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
},
Expand All @@ -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}`])
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
],
"keywords": [],
"dependencies": {
"dom-scroll-into-view": "1.0.1"
"dom-scroll-into-view": "1.0.1",
"lodash": "^4.0.0"
}
}
}