Change autocomplete filter behaviour when multiple=false

Adds a showAllSuggestions state, which is set to true initially.
It is set to false when the input is changed, and is used to control if
the shown suggestions should be filtered based when the multiple prop is false.
Once a value is set, the state will be reset to true.
old
Lucas Correia 2016-04-27 15:50:26 +02:00
parent 5d39761581
commit a4810d8e73
1 changed files with 11 additions and 4 deletions

View File

@ -37,6 +37,7 @@ class Autocomplete extends React.Component {
state = {
direction: this.props.direction,
focus: false,
showAllSuggestions: true,
query: this.query(this.props.value)
};
@ -61,7 +62,10 @@ class Autocomplete extends React.Component {
const key = this.props.multiple ? keys : keys[0];
const query = this.query(key);
if (this.props.onChange) this.props.onChange(key, event);
this.setState({ focus: false, query }, () => { this.refs.input.blur(); });
this.setState(
{focus: false, query, showAllSuggestions: true},
() => { this.refs.input.blur(); }
);
};
handleQueryBlur = () => {
@ -69,7 +73,7 @@ class Autocomplete extends React.Component {
};
handleQueryChange = (value) => {
this.setState({query: value});
this.setState({query: value, showAllSuggestions: false});
};
handleQueryFocus = () => {
@ -113,8 +117,11 @@ class Autocomplete extends React.Component {
const query = this.state.query.toLowerCase().trim() || '';
const values = this.values();
for (const [key, value] of this.source()) {
if (value.toLowerCase().trim().startsWith(query)
&& (!values.has(key) || !this.props.multiple)) {
const valueMatchesQuery = value.toLowerCase().trim().startsWith(query);
if (
(valueMatchesQuery && !values.has(key))
|| (!this.props.multiple && (this.state.showAllSuggestions || valueMatchesQuery))
) {
suggest.set(key, value);
}
}