https://codesandbox.io/s/x9q44520oo
Autocomplete.jsx
onChange = e => {
const { suggestions } = this.props;
const userInput = e.currentTarget.value;
// Filter our suggestions that don't contain the user's input
const filteredSuggestions = suggestions.filter(
suggestion =>
suggestion.toLowerCase().indexOf(userInput.toLowerCase()) > -1
);
console.log("filteredSuggestions", filteredSuggestions);
this.setState({
activeSuggestion: 0,
filteredSuggestions,
showSuggestions: true,
userInput: e.currentTarget.value
});
};
index.js
<Autocomplete
suggestions={[
"Alligator",
"america",
"Bask",
"Crocodilian",
"Death Roll",
"california",
"miami",
"NEPHROLOGY",
"PEDIATRIC NEPHROLOGY",
"PEDIATRIC CARDIOLOGY CARDIOVASCULAR DISEASE",
"PEDIATRIC BEHAVIOR & DEVELOPMENT",
"PEDIATRIC INTENSIVE CARE",
"PORTABLE X RAY",
"PEDIATRICIAN",
"Eggs",
"Jaws",
"Reptile",
"Solitary",
"Tail",
"Wetlands"
]}
/>
答案 0 :(得分:1)
indexOf
)的任何位置, > -1
将返回值suggestion
。听起来您似乎只想包含以输入开头的建议-有一个.startsWith
函数可以做到这一点,即:
const filteredSuggestions = suggestions.filter(
suggestion =>
suggestion.toLowerCase().startsWith(userInput.toLowerCase())
);