react-spectrum
react-spectrum copied to clipboard
Can't access item index in the ListBox callback function
Provide a general summary of the feature here
I've made a custom ListBox component using the hooks React Aria offers (useListState, useListBox). When I use my ListBox component, I would like to be able to access the index in the callback function for each item. Similar to a map in javascript, however when I try to access index it returns undefined.
๐ค Expected Behavior?
I expected similar behavior to a .map function in javascript where I could access the index on each item.
๐ฏ Current Behavior
The index returns undefined when I try to access it.
๐ Possible Solution
No response
๐ฆ Context
I am trying to accomplish conditional styling for the last item in the listItem array set. So for every other item I will have a border on the bottom but the last item will have no border on the bottom. So I need to access the index in order to know if the item is the last node.
๐ป Examples
No response
๐งข Your Company/Team
PSU
๐ท Tracking Issue
No response
You can do state.collection.getLastKey() to get the last key, and compare it to the current key. See the Collection interface for more methods like this.
You could also use https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child ?