这是关于:
https://github.com/paypal/downshift
我将onKeyDown
处理程序设置为识别 Tab 键按下,并且我能够highlightedIndex
正确获取该值,但是如何使用它来更改inputValue
?
import React from 'react'
import Downshift from 'downshift'
import {
Label,
Menu,
ControllerButton,
Input,
Item,
ArrowIcon,
XIcon,
css,
itemToString,
getItems,
} from '../shared'
class App extends React.Component {
render() {
return (
<div
{...css({
display: 'flex',
flexDirection: 'column',
marginTop: 50,
})}
>
<Downshift
onChange={selection =>
alert(
selection
? `You selected ${itemToString(selection)}`
: 'selection cleared',
)
}
itemToString={itemToString}
>
{({
getLabelProps,
getInputProps,
getToggleButtonProps,
getMenuProps,
getItemProps,
isOpen,
clearSelection,
selectedItem,
inputValue,
highlightedIndex,
}) => (
<div {...css({width: 250, margin: 'auto'})}>
<Label {...getLabelProps()}>Find a Star Wars character</Label>
<div {...css({position: 'relative'})}>
<Input
{...getInputProps({
isOpen,
placeholder: 'Enter a name',
**onKeyDown: event => {
console.log(event.key)
if (event.key === 'Tab') {
console.log(highlightedIndex)
}
},**
})}
/>
{selectedItem ? (
<ControllerButton
onClick={clearSelection}
aria-label="clear selection"
>
<XIcon />
</ControllerButton>
) : (
<ControllerButton {...getToggleButtonProps()}>
<ArrowIcon isOpen={isOpen} />
</ControllerButton>
)}
</div>
<div {...css({position: 'relative'})}>
<Menu {...getMenuProps({isOpen})}>
{isOpen
? getItems(inputValue).map((item, index) => (
<Item
key={item.id}
{...getItemProps({
item,
index,
isActive: highlightedIndex === index,
isSelected: selectedItem === item,
})}
>
{itemToString(item)}
</Item>
))
: null}
</Menu>
</div>
</div>
)}
</Downshift>
</div>
)
}
}
export default App