2016-12-30 20:38:50 +01:00
|
|
|
import React from 'react'
|
2017-11-06 15:32:04 +00:00
|
|
|
import PropTypes from 'prop-types'
|
2017-01-11 14:03:48 +01:00
|
|
|
import classnames from 'classnames'
|
2016-12-30 20:38:50 +01:00
|
|
|
import Autocomplete from 'react-autocomplete'
|
2017-01-11 14:03:48 +01:00
|
|
|
|
2016-12-30 20:38:50 +01:00
|
|
|
|
2017-11-29 10:29:11 +00:00
|
|
|
const MAX_HEIGHT = 140;
|
|
|
|
|
2018-01-19 11:50:23 +00:00
|
|
|
class AutocompleteInput extends React.Component {
|
2017-11-08 15:44:43 +00:00
|
|
|
static propTypes = {
|
2018-01-19 11:50:23 +00:00
|
|
|
value: PropTypes.string,
|
|
|
|
options: PropTypes.array,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
keepMenuWithinWindowBounds: PropTypes.bool
|
|
|
|
}
|
|
|
|
|
2018-08-22 21:09:37 -04:00
|
|
|
state = {
|
|
|
|
maxHeight: MAX_HEIGHT
|
|
|
|
}
|
|
|
|
|
2018-01-19 11:50:23 +00:00
|
|
|
static defaultProps = {
|
|
|
|
onChange: () => {},
|
|
|
|
options: [],
|
|
|
|
}
|
|
|
|
|
2017-11-08 15:44:43 +00:00
|
|
|
calcMaxHeight() {
|
|
|
|
if(this.props.keepMenuWithinWindowBounds) {
|
|
|
|
const maxHeight = window.innerHeight - this.autocompleteMenuEl.getBoundingClientRect().top;
|
2017-11-29 10:29:11 +00:00
|
|
|
const limitedMaxHeight = Math.min(maxHeight, MAX_HEIGHT);
|
|
|
|
|
|
|
|
if(limitedMaxHeight != this.state.maxHeight) {
|
2017-11-08 15:44:43 +00:00
|
|
|
this.setState({
|
2017-11-29 10:29:11 +00:00
|
|
|
maxHeight: limitedMaxHeight
|
2017-11-08 15:44:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-22 21:09:37 -04:00
|
|
|
|
2017-11-08 15:44:43 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.calcMaxHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
this.calcMaxHeight();
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:34:28 +01:00
|
|
|
onChange (v) {
|
|
|
|
this.props.onChange(v === "" ? undefined : v);
|
|
|
|
}
|
|
|
|
|
2016-12-30 20:38:50 +01:00
|
|
|
render() {
|
2018-01-19 11:50:23 +00:00
|
|
|
return <div
|
|
|
|
ref={(el) => {
|
|
|
|
this.autocompleteMenuEl = el;
|
2016-12-30 20:46:27 +01:00
|
|
|
}}
|
2018-01-19 11:50:23 +00:00
|
|
|
>
|
|
|
|
<Autocomplete
|
|
|
|
menuStyle={{
|
2018-04-13 15:55:16 +02:00
|
|
|
position: "fixed",
|
2018-01-19 11:50:23 +00:00
|
|
|
overflow: "auto",
|
2018-10-31 18:53:15 +01:00
|
|
|
maxHeight: this.state.maxHeight,
|
|
|
|
zIndex: '998'
|
2018-01-19 11:50:23 +00:00
|
|
|
}}
|
|
|
|
wrapperProps={{
|
|
|
|
className: "maputnik-autocomplete",
|
|
|
|
style: null
|
|
|
|
}}
|
|
|
|
inputProps={{
|
2018-05-17 11:43:25 +01:00
|
|
|
className: "maputnik-string",
|
|
|
|
spellCheck: false
|
2018-01-19 11:50:23 +00:00
|
|
|
}}
|
|
|
|
value={this.props.value}
|
|
|
|
items={this.props.options}
|
|
|
|
getItemValue={(item) => item[0]}
|
2019-06-19 20:34:28 +01:00
|
|
|
onSelect={v => this.onChange(v)}
|
|
|
|
onChange={(e, v) => this.onChange(v)}
|
2018-10-09 21:11:47 +01:00
|
|
|
shouldItemRender={(item, value="") => {
|
2018-10-11 21:38:35 +01:00
|
|
|
if (typeof(value) === "string") {
|
|
|
|
return item[0].toLowerCase().indexOf(value.toLowerCase()) > -1
|
|
|
|
}
|
2018-01-19 12:11:34 +00:00
|
|
|
}}
|
2018-01-19 11:50:23 +00:00
|
|
|
renderItem={(item, isHighlighted) => (
|
|
|
|
<div
|
|
|
|
key={item[0]}
|
|
|
|
className={classnames({
|
|
|
|
"maputnik-autocomplete-menu-item": true,
|
|
|
|
"maputnik-autocomplete-menu-item-selected": isHighlighted,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{item[1]}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
2016-12-30 20:38:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AutocompleteInput
|