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

Use 'absolute' positioning for menu instead of 'fixed' #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 26 additions & 10 deletions lib/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ let Autocomplete = React.createClass({
return <div style={{...style, ...this.menuStyle}} children={items}/>
},
shouldItemRender () { return true },
menuMinSize: 100,
menuStyle: {
borderRadius: '3px',
boxShadow: '0 2px 12px rgba(0, 0, 0, 0.1)',
background: 'rgba(255, 255, 255, 0.9)',
padding: '2px 0',
fontSize: '90%',
position: 'fixed',
overflow: 'auto',
maxHeight: '50%', // TODO: don't cheat, let it flow to the bottom
position: 'absolute',
left: '0',
overflow: 'auto'
}
}
},
Expand Down Expand Up @@ -76,6 +77,7 @@ let Autocomplete = React.createClass({
},

handleKeyDown (event) {
console.log(event);
if (this.keyDownHandlers[event.key])
this.keyDownHandlers[event.key].call(this, event)
else {
Expand Down Expand Up @@ -220,11 +222,21 @@ let Autocomplete = React.createClass({
var marginBottom = parseInt(computedStyle.marginBottom, 10)
var marginLeft = parseInt(computedStyle.marginLeft, 10)
var marginRight = parseInt(computedStyle.marginRight, 10)
var marginTop = parseInt(computedStyle.marginTop, 10)

var inputTop = rect.top - marginTop;
var inputBottom = rect.bottom + marginBottom;

var heightBefore = inputTop;
var heightAfter = window.innerHeight - inputBottom;

var displayBefore = heightAfter < (this.props.menuMinSize + 10) && heightBefore > heightAfter;

this.setState({
menuTop: rect.bottom + marginBottom,
menuLeft: rect.left + marginLeft,
menuWidth: rect.width + marginLeft + marginRight
})
menuWidth: rect.width + marginLeft + marginRight,
menuMaxHeight: (displayBefore ? heightBefore : heightAfter) - 10,
menuPosition: displayBefore ? 'before' : 'after'
});
},

highlightItemFromMouse (index) {
Expand Down Expand Up @@ -262,9 +274,13 @@ let Autocomplete = React.createClass({
})
})
var style = {
left: this.state.menuLeft,
top: this.state.menuTop,
minWidth: this.state.menuWidth,
maxHeight: this.state.menuMaxHeight
}
if (this.state.menuPosition === 'before') {
style.bottom = '100%';
} else if (this.state.menuPosition === 'after') {
style.top = '100%';
}
var menu = this.props.renderMenu(items, this.state.value, style)
return React.cloneElement(menu, { ref: 'menu' })
Expand Down Expand Up @@ -298,7 +314,7 @@ let Autocomplete = React.createClass({
})
}
return (
<div style={{display: 'inline-block'}}>
<div style={{display: 'inline-block', position: 'relative'}}>
<input
{...this.props.inputProps}
role="combobox"
Expand Down