Autocomplete fixes #218

This commit is contained in:
orangemug 2018-01-19 11:50:23 +00:00
parent 2b382a9946
commit a33d1b819c
2 changed files with 66 additions and 80 deletions

View file

@ -6,11 +6,24 @@ import Autocomplete from 'react-autocomplete'
const MAX_HEIGHT = 140; const MAX_HEIGHT = 140;
class AutocompleteMenu extends React.Component { class AutocompleteInput extends React.Component {
static propTypes = { static propTypes = {
keepMenuWithinWindowBounds: PropTypes.bool, value: PropTypes.string,
style: PropTypes.object, options: PropTypes.array,
children: PropTypes.node onChange: PropTypes.func,
keepMenuWithinWindowBounds: PropTypes.bool
}
static defaultProps = {
onChange: () => {},
options: [],
}
constructor(props) {
super(props);
this.state = {
maxHeight: MAX_HEIGHT
};
} }
calcMaxHeight() { calcMaxHeight() {
@ -33,81 +46,43 @@ class AutocompleteMenu extends React.Component {
this.calcMaxHeight(); this.calcMaxHeight();
} }
constructor(props) {
super(props);
this.state = {
maxHeight: MAX_HEIGHT
};
}
static defaultProps = {
style: {}
}
render() { render() {
const maxHeight = this.state.maxHeight - this.props.style.marginBottom || 0; return <div
const style = { ref={(el) => {
maxHeight: maxHeight+"px" this.autocompleteMenuEl = el;
} }}
>
return ( <Autocomplete
<div menuStyle={{
ref={(el) => { position: "absolute",
this.autocompleteMenuEl = el; overflow: "auto",
maxHeight: this.state.maxHeight
}} }}
className={"maputnik-autocomplete-menu"} wrapperProps={{
style={style} className: "maputnik-autocomplete",
> style: null
{this.props.children} }}
</div> inputProps={{
) className: "maputnik-string"
} }}
} value={this.props.value}
items={this.props.options}
class AutocompleteInput extends React.Component { getItemValue={(item) => item[0]}
static propTypes = { onSelect={v => this.props.onChange(v)}
value: PropTypes.string, onChange={(e, v) => this.props.onChange(v)}
options: PropTypes.array, renderItem={(item, isHighlighted) => (
onChange: PropTypes.func, <div
keepMenuWithinWindowBounds: PropTypes.bool key={item[0]}
} className={classnames({
"maputnik-autocomplete-menu-item": true,
static defaultProps = { "maputnik-autocomplete-menu-item-selected": isHighlighted,
onChange: () => {}, })}
options: [], >
} {item[1]}
</div>
render() { )}
return <Autocomplete />
wrapperProps={{ </div>
className: "maputnik-autocomplete",
style: null
}}
renderMenu={(items) => {
return <AutocompleteMenu keepMenuWithinWindowBounds={this.props.keepMenuWithinWindowBounds} style={{marginBottom: 4}}>
{items}
</AutocompleteMenu>
}}
inputProps={{
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]}
className={classnames({
"maputnik-autocomplete-menu-item": true,
"maputnik-autocomplete-menu-item-selected": isHighlighted,
})}
>
{item[1]}
</div>
)}
/>
} }
} }

View file

@ -58,11 +58,22 @@ class AddModal extends React.Component {
componentWillUpdate(nextProps, nextState) { componentWillUpdate(nextProps, nextState) {
// Check if source is valid for new type // Check if source is valid for new type
const availableSources = this.getSources(nextState.type); const oldType = this.state.type;
const newType = nextState.type;
const availableSourcesOld = this.getSources(oldType);
const availableSourcesNew = this.getSources(newType);
if( if(
this.state.source !== "" // Type has changed
&& availableSources.indexOf(this.state.source) < 0 oldType !== newType
&& this.state.source !== ""
// Was a valid source previously
&& availableSourcesOld.indexOf(this.state.source) > -1
// And is not a valid source now
&& availableSourcesNew.indexOf(nextState.source) < 0
) { ) {
// Clear the source
this.setState({ this.setState({
source: "" source: ""
}); });