不确定这是一个错误还是缺乏我的反应知识。我没有错误,但过滤器不起作用。如果我将函数写入同一个文件,过滤器可以工作,我做错了吗?
Filters.js
const filterResults = (searchText, key) => {
if (searchText.length === 0) {
return false;
}
const compareString = key.toLowerCase();
searchText = searchText.toLowerCase();
const subMatchKey = key.substring(0, searchText.length);
const distance = AutoComplete.levenshteinDistance(searchText.toLowerCase(), subMatchKey.toLowerCase());
let searchTextIndex = 0;
for (let index = 0; index < key.length; index++) {
if (compareString[index] === searchText[searchTextIndex]) {
searchTextIndex += 1;
}
}
return searchText.length > 3 ? distance < 2 : distance === 0;
return searchTextIndex === searchText.length;
};
export default filterResults;
Main.js
import filterResults from '../AutoComplete/Filters'
<AutoComplete
dataSource={location}
onUpdateInput={this.onInputChange}
searchText={this.state.city}
filter={this.filterResults}
/>
答案 0 :(得分:0)
尝试使用此
<AutoComplete
dataSource={location}
filter={AutoComplete.caseInsensitiveFilter}
/>
Material-UI有一些内置的过滤功能