I have a ListItem in my react website. What event should be triggered to call a function when I press enter key on focused item.
Code for the ListItem is as below:
<ListItem
tabIndex={'0'}
className={classes.listItem}
key={timeline}
value={timeline}
onClick={props.handleChangeTimeline}
onKeyDown={handleChange}>
{timeline}
</ListItem>
The onClick event works fine with mouse click. But I am looking for keyboard accessibilty. So when an element in the dropdown is focused and I press the enter key a function to handle that event will be called.
I tried onKeyDown event. It gets triggered but event.target.value is null. If get this value on some event I think the problem will be solved.
The keydown event would be the way to go. Here's a working example. Perhaps the issue is elsewhere in your code.