maputnik/src/components/inputs/AutocompleteInput.jsx

111 lines
2.4 KiB
React
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
2017-01-11 14:03:48 +01:00
import classnames from 'classnames'
import Autocomplete from 'react-autocomplete'
2017-01-11 14:03:48 +01:00
class AutocompleteMenu extends React.Component {
static propTypes = {
keepMenuWithinWindowBounds: PropTypes.bool,
style: PropTypes.object,
children: PropTypes.none
}
calcMaxHeight() {
if(this.props.keepMenuWithinWindowBounds) {
const maxHeight = window.innerHeight - this.autocompleteMenuEl.getBoundingClientRect().top;
if(maxHeight != this.state.maxHeight) {
this.setState({
maxHeight: maxHeight
})
}
}
}
componentDidMount() {
this.calcMaxHeight();
}
componentDidUpdate() {
this.calcMaxHeight();
}
constructor(props) {
super(props);
this.state = {
maxHeight: 90
};
}
static defaultProps = {
style: {}
}
render() {
const maxHeight = this.state.maxHeight - this.props.style.marginBottom || 0;
const style = {
maxHeight: maxHeight+"px"
}
return (
<div
ref={(el) => {
this.autocompleteMenuEl = el;
}}
className={"maputnik-autocomplete-menu"}
style={style}
>
{this.props.children}
</div>
)
}
}
class AutocompleteInput extends React.Component {
static propTypes = {
value: PropTypes.string,
options: PropTypes.array,
onChange: PropTypes.func,
keepMenuWithinWindowBounds: PropTypes.bool
2016-12-31 10:39:30 +01:00
}
static defaultProps = {
onChange: () => {},
options: [],
}
render() {
return <Autocomplete
2017-01-11 13:34:38 +01:00
wrapperProps={{
className: "maputnik-autocomplete",
style: null
}}
renderMenu={(items) => {
return <AutocompleteMenu keepMenuWithinWindowBounds={this.props.keepMenuWithinWindowBounds} style={{marginBottom: 4}}>
{items}
</AutocompleteMenu>
}}
inputProps={{
2017-01-11 13:34:38 +01:00
className: "maputnik-string"
}}
value={this.props.value}
items={this.props.options}
getItemValue={(item) => item[0]}
onSelect={v => this.props.onChange(v)}
onChange={(e, v) => this.props.onChange(v)}
renderItem={(item, isHighlighted) => (
<div
key={item[0]}
2017-01-11 14:03:48 +01:00
className={classnames({
"maputnik-autocomplete-menu-item": true,
"maputnik-autocomplete-menu-item-selected": isHighlighted,
})}
>
{item[1]}
</div>
)}
/>
}
}
export default AutocompleteInput