[英]Downshift: The menu should close until results not found
我遇到了Reactjs
前端代碼庫的問題。 我們使用庫名稱react-autoauggest來實現自動完成功能,但領導決定從react-autoauggest 轉移到downshift 。 我通讀了這個文檔並使用useCombobox
鈎子實現了這個,但他提出了一些問題並告訴我在沒有任何指導的情況下解決這些問題。 我創建了這些問題的干凈版本。
首先我要解決issue-4
,清除按鈕應該清除輸入字段並關閉菜單。 但是當我單擊清除按鈕時,輸入字段為空,但菜單仍處於打開狀態。 你能給我一些關於如何在降檔時做這些事情的指導嗎?
這是我從對象數組中過濾數據的代碼沙箱鏈接:在此處查看代碼沙箱
應用程序.js :
const App = () => {
// Array of objects
const [inputItems, setInputItems] = useState(data);
// State for all primitive types
const [value, setValue] = useState('');
/**
* It will returns the new filtered array.
* @param data Array<Object> - The array to iterate over
* @param inputValue {string} - Your input value
* @return Array<Object> - Returns the new filtered array
*/
const filterByName = (data, inputValue) => {
return data.filter(item => {
return item.name.toLowerCase().indexOf(inputValue.toLowerCase()) !== -1;
});
};
// props for the combobox
const comboboxProps = {
className: 'search has-icons-left has-buttons-right'
};
// props for the input
const inputProps = {
type: 'text',
className: 'form-control',
placeholder: 'Enter the state'
};
// props for the menu
const menuProps = {
className: 'menu'
};
// useComboBox
const {
isOpen,
getComboboxProps,
getInputProps,
getMenuProps,
getItemProps,
highlightedIndex,
selectItem,
} = useCombobox({
items: inputItems,
onInputValueChange: ({inputValue}) => {
setValue(inputValue);
setInputItems(filterByName(data, inputValue));
},
itemToString: (item) => item ? item.name : '',
});
return (
<div className="app">
<div {...getComboboxProps(comboboxProps)}>
<input {...getInputProps(inputProps)} />
<span className="icon is-left"><MarkerIcon/></span>
{(typeof value === 'string' && value.length > 0) ?
(<span className="button is-right">
<button className="btn btn-clear" onClick={() => selectItem(null)}>Clear</button>
</span>) : null}
{/* Suggestions */}
<ul {...getMenuProps(menuProps)}>
{isOpen && inputItems.map((item, index) => (
<li key={index} {...getItemProps({item, index})}
style={highlightedIndex === index ? {backgroundColor: "#f5f5f5"} : null}>
{item.name}
</li>
))}
</ul>
</div>
</div>
);
};
export default App;
#1,如果沒有輸入值,防止打開:
<input {
...getInputProps({
...inputProps,
onKeyDown: e => {
if(!value) {
return e.nativeEvent.preventDownshiftDefault = true
}
}
})
} />
#2 可能很棘手,因為如果你想修改 state,過濾器將被激活。 我建議對他們的輸入進行一些布局,如果highlightedIndex > -1
會顯示inputItems[highlightedIndex]
const completeValue =
highlightedIndex > -1 ? inputItems[highlightedIndex].name : null;
return (
<div className="app">
...
{
completeValue
? <input {...getInputProps(inputProps)} value={completeValue} />
: (
<input
{...getInputProps({
...inputProps,
onKeyDown: e => {
if (!value) {
return (e.nativeEvent.preventDownshiftDefault = true);
}
}
})}
/>
)
}
...
</div>
)
#3,關閉推薦框:
const {
isOpen,
getComboboxProps,
getInputProps,
getMenuProps,
getItemProps,
highlightedIndex,
closeMenu, // <= use this inbuilt functionality
selectItem
} = useCombobox({
And at the button click just call it by manually:
<button
className="btn btn-clear"
onClick={() => {
selectItem(null);
setValue("");
closeMenu();
}}
>
Clear
</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.