Another attempt at fixing autosuggest behavior

This commit is contained in:
Rainer Simon 2020-10-13 14:04:11 +02:00
parent e44f404e28
commit 75feeb2959
1 changed files with 86 additions and 72 deletions

View File

@ -1,23 +1,38 @@
import React, { useState } from 'react' import React, { Component, createRef } from 'react'
import { useCombobox } from 'downshift' import { useCombobox } from 'downshift'
const Autocomplete = props => { export default class Autocomplete extends Component {
const [ inputItems, setInputItems ] = useState(props.vocabulary); constructor(props) {
super(props);
const onInputValueChange = ({ inputValue }) => { this.element = createRef();
this.state = {
inputItems: props.vocabulary || []
}
}
componentDidMount() {
if (this.props.initialValue && this.element.current)
this.element.current.querySelector('input').value = this.props.initialValue;
}
onInputValueChange = ({ inputValue }) => {
if (inputValue.length > 0) { if (inputValue.length > 0) {
const prefixMatches = props.vocabulary.filter(item => { const prefixMatches = this.props.vocabulary.filter(item => {
return item.toLowerCase().startsWith(inputValue.toLowerCase()); return item.toLowerCase().startsWith(inputValue.toLowerCase());
}); });
setInputItems(prefixMatches); this.setState({ inputItems: prefixMatches });
} else { } else {
// ...or none, if the input is empty // ...or none, if the input is empty
setInputItems([]); this.setState({ inputItems: [] });
} }
} }
render() {
const { const {
isOpen, isOpen,
getMenuProps, getMenuProps,
@ -27,8 +42,8 @@ const Autocomplete = props => {
getItemProps, getItemProps,
setInputValue setInputValue
} = useCombobox({ } = useCombobox({
items: inputItems, items: this.state.inputItems,
onInputValueChange, onInputValueChange: this.onInputValueChange,
onSelectedItemChange: ({ inputValue }) => { onSelectedItemChange: ({ inputValue }) => {
onSubmit(inputValue); onSubmit(inputValue);
setInputValue(''); setInputValue('');
@ -38,7 +53,7 @@ const Autocomplete = props => {
const onSubmit = inputValue => { const onSubmit = inputValue => {
setInputValue(''); setInputValue('');
if (inputValue.trim().length > 0) if (inputValue.trim().length > 0)
props.onSubmit(inputValue); this.props.onSubmit(inputValue);
} }
const onKeyUp = evt => { const onKeyUp = evt => {
@ -47,23 +62,23 @@ const Autocomplete = props => {
if (evt.which == 13 && highlightedIndex == -1) { if (evt.which == 13 && highlightedIndex == -1) {
onSubmit(value); onSubmit(value);
} else if (evt.which == 40 && value.length == 0) { } else if (evt.which == 40 && value.length == 0) {
setInputItems(props.vocabulary); // Show all options on key down setInputItems(this.props.vocabulary); // Show all options on key down
} else if (evt.which == 27) { } else if (evt.which == 27) {
props.onCancel && props.onCancel(); this.props.onCancel && this.props.onCancel();
} else { } else {
props.onChange && props.onChange(value); this.props.onChange && this.props.onChange(value);
} }
} }
return ( return (
<div className="r6o-autocomplete"> <div className="r6o-autocomplete" ref={this.element}>
<div {...getComboboxProps()}> <div {...getComboboxProps()}>
<input <input
{...getInputProps({ onKeyUp })} {...getInputProps({ onKeyUp })}
placeholder={props.placeholder} /> placeholder={this.props.placeholder} />
</div> </div>
<ul {...getMenuProps()}> <ul {...getMenuProps()}>
{isOpen && inputItems.map((item, index) => ( {isOpen && this.state.inputItems.map((item, index) => (
<li style={ <li style={
highlightedIndex === index highlightedIndex === index
? { backgroundColor: '#bde4ff' } ? { backgroundColor: '#bde4ff' }
@ -77,7 +92,6 @@ const Autocomplete = props => {
</ul> </ul>
</div> </div>
) )
} }
export default Autocomplete; }