1- import React , { useMemo } from 'react'
1+ import React from 'react'
22import type { AriaRole } from '../utils/types'
33import { Group , GroupProps } from './Group'
44import { Item , ItemProps } from './Item'
55import { Divider } from './Divider'
66import styled from 'styled-components'
77import { get } from '../constants'
88import { SystemCssProperties } from '@styled-system/css'
9- import { uniqueId } from '../utils/uniqueId'
109
1110export type ItemInput = ItemProps | ( Partial < ItemProps > & { renderItem : typeof Item } )
1211
@@ -156,10 +155,10 @@ export function List(props: ListProps): JSX.Element {
156155 * An `Item`-level, `Group`-level, or `List`-level custom `Item` renderer,
157156 * or the default `Item` renderer.
158157 */
159- const renderItem = ( itemProps : ItemInput , item : ItemInput ) => {
158+ const renderItem = ( itemProps : ItemInput , item : ItemInput , itemIndex : number ) => {
160159 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
161160 const ItemComponent = ( 'renderItem' in itemProps && itemProps . renderItem ) || props . renderItem || Item
162- const key = itemProps . key ?? itemProps . id ?. toString ( ) ?? uniqueId ( )
161+ const key = itemProps . key ?? itemProps . id ?. toString ( ) ?? itemIndex . toString ( )
163162 return (
164163 < ItemComponent
165164 showDivider = { props . showItemDividers }
@@ -178,11 +177,9 @@ export function List(props: ListProps): JSX.Element {
178177 let groups : ( GroupProps | ( Partial < GroupProps > & { renderItem ?: typeof Item ; renderGroup ?: typeof Group } ) ) [ ] = [ ]
179178
180179 // Collect rendered `Item`s into `Group`s, avoiding excess iteration over the lists of `items` and `groupMetadata`:
181- const singleGroupId = useMemo ( uniqueId , [ ] )
182-
183180 if ( ! isGroupedListProps ( props ) ) {
184181 // When no `groupMetadata`s is provided, collect rendered `Item`s into a single anonymous `Group`.
185- groups = [ { items : props . items . map ( item => renderItem ( item , item ) ) , groupId : singleGroupId } ]
182+ groups = [ { items : props . items . map ( ( item , index ) => renderItem ( item , item , index ) ) , groupId : '0' } ]
186183 } else {
187184 // When `groupMetadata` is provided, collect rendered `Item`s into their associated `Group`s.
188185
@@ -197,6 +194,7 @@ export function List(props: ListProps): JSX.Element {
197194 for ( const itemProps of props . items ) {
198195 // Look up the group associated with the current item.
199196 const group = groupMap . get ( itemProps . groupId )
197+ const itemIndex = group ?. items ?. length ?? 0
200198
201199 // Upsert the group to include the current item (rendered).
202200 groupMap . set ( itemProps . groupId , {
@@ -209,7 +207,8 @@ export function List(props: ListProps): JSX.Element {
209207 ...( group && 'renderItem' in group && { renderItem : group . renderItem } ) ,
210208 ...itemProps
211209 } ,
212- itemProps
210+ itemProps ,
211+ itemIndex
213212 )
214213 ]
215214 } )
0 commit comments