Reactjs 中的降档 - 菜单下拉仅显示所选项目
Downshift in Reactjs - Menu dropdown only displaying selected item
Im having trouble using Downshift
, Initially I have all the menu
options but once I select one option, thats the only one
that shows in the menu the next time I click on it.
This is my current code, any feedback on whats causing that?
const dropdownItems = [
{ value: 'All' },
{ value: 'Database A' },
{ value: 'Database B' },
{ value: 'Database C' },
{ value: 'Database D' },
];
return (
<Downshift itemToString={item => (item ? item.value : '')}>
{({
getMenuProps,
getItemProps,
getToggleButtonProps,
getRootProps,
isOpen,
inputValue,
selectedItem,
highlightedIndex,
}) => console.log(
selectedItem === null ? dropdownItems[0].value : selectedItem.value,
) || (
<Container {...getRootProps()}>
<button {...getToggleButtonProps()}>
{isOpen
? selectedItem === null
? dropdownItems[0].value
: selectedItem.value
: selectedItem === null
? dropdownItems[0].value
: selectedItem.value}
</button>
{isOpen ? (
<Menu {...getMenuProps()}>
{dropdownItems
.filter(
item => !inputValue || item.value.includes(inputValue),
)
.map((item, index) => (
<Item
{...getItemProps({
key: item.value,
index,
item,
style: {
fontWeight:
index === highlightedIndex
? 'bold'
: null,
},
})}
>
{item.value}
</Item>
))}
</Menu>
) : null}
</Container>
)
}
</Downshift>
);
.filter(item => !inputValue || item.value.includes(inputValue),)
为了解决这个问题,我删除了 .filter
,它导致下拉列表中只显示选定的值。
This is the updated menu tag
<Menu {...getMenuProps()}>
{dropdownItems.map((item, index) => (
<Item
{...getItemProps({
key: item.value,
index,
item,
style: {
fontWeight:
index === highlightedIndex
? 'bold'
: null,
},
})}
>
{item.value}
</Item>
))}
</Menu>
Im having trouble using
Downshift
, Initially I have all the menu options but once I select one option, thats the only one that shows in the menu the next time I click on it.
This is my current code, any feedback on whats causing that?
const dropdownItems = [
{ value: 'All' },
{ value: 'Database A' },
{ value: 'Database B' },
{ value: 'Database C' },
{ value: 'Database D' },
];
return (
<Downshift itemToString={item => (item ? item.value : '')}>
{({
getMenuProps,
getItemProps,
getToggleButtonProps,
getRootProps,
isOpen,
inputValue,
selectedItem,
highlightedIndex,
}) => console.log(
selectedItem === null ? dropdownItems[0].value : selectedItem.value,
) || (
<Container {...getRootProps()}>
<button {...getToggleButtonProps()}>
{isOpen
? selectedItem === null
? dropdownItems[0].value
: selectedItem.value
: selectedItem === null
? dropdownItems[0].value
: selectedItem.value}
</button>
{isOpen ? (
<Menu {...getMenuProps()}>
{dropdownItems
.filter(
item => !inputValue || item.value.includes(inputValue),
)
.map((item, index) => (
<Item
{...getItemProps({
key: item.value,
index,
item,
style: {
fontWeight:
index === highlightedIndex
? 'bold'
: null,
},
})}
>
{item.value}
</Item>
))}
</Menu>
) : null}
</Container>
)
}
</Downshift>
);
.filter(item => !inputValue || item.value.includes(inputValue),)
为了解决这个问题,我删除了 .filter
,它导致下拉列表中只显示选定的值。
This is the updated menu tag
<Menu {...getMenuProps()}>
{dropdownItems.map((item, index) => (
<Item
{...getItemProps({
key: item.value,
index,
item,
style: {
fontWeight:
index === highlightedIndex
? 'bold'
: null,
},
})}
>
{item.value}
</Item>
))}
</Menu>