react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

Can't access item index in the ListBox callback function

Open drewpoling2 opened this issue 1 year ago โ€ข 2 comments

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?

{(listItem, index) => ( {listItem.name} )}

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

drewpoling2 avatar Jan 31 '24 05:01 drewpoling2

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.

reidbarber avatar Jan 31 '24 15:01 reidbarber

You could also use https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child ?

snowystinger avatar Feb 06 '24 00:02 snowystinger