Skip to content

Commit 8e66a2c

Browse files
ensure the MappedActionListItem component is a ForwardRef Componenet (#7258)
1 parent ef04a80 commit 8e66a2c

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

.changeset/dry-pets-appear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': minor
3+
---
4+
5+
Make MappedActionListItem a forwardRef component

packages/react/src/FilteredActionList/FilteredActionList.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {ScrollIntoViewOptions} from '@primer/behaviors'
22
import {scrollIntoView, FocusKeys} from '@primer/behaviors'
33
import type {KeyboardEventHandler, JSX} from 'react'
44
import type React from 'react'
5-
import {useCallback, useEffect, useRef, useState} from 'react'
5+
import {forwardRef, useCallback, useEffect, useRef, useState} from 'react'
66
import type {TextInputProps} from '../TextInput'
77
import TextInput from '../TextInput'
88
import {ActionList, type ActionListProps} from '../ActionList'
@@ -426,8 +426,7 @@ export function FilteredActionList({
426426
</div>
427427
)
428428
}
429-
430-
function MappedActionListItem(item: ItemInput & {renderItem?: RenderItemFn}) {
429+
const MappedActionListItem = forwardRef<HTMLLIElement, ItemInput & {renderItem?: RenderItemFn}>((item, ref) => {
431430
// keep backward compatibility for renderItem
432431
// escape hatch for custom Item rendering
433432
if (typeof item.renderItem === 'function') return item.renderItem(item)
@@ -455,6 +454,7 @@ function MappedActionListItem(item: ItemInput & {renderItem?: RenderItemFn}) {
455454
onAction(item, e as React.MouseEvent<HTMLDivElement> | React.KeyboardEvent<HTMLDivElement>)
456455
}}
457456
data-id={id}
457+
ref={ref}
458458
{...rest}
459459
>
460460
{LeadingVisual ? (
@@ -481,6 +481,6 @@ function MappedActionListItem(item: ItemInput & {renderItem?: RenderItemFn}) {
481481
) : null}
482482
</ActionList.Item>
483483
)
484-
}
484+
})
485485

486486
FilteredActionList.displayName = 'FilteredActionList'

packages/react/src/SelectPanel/SelectPanel.examples.stories.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,14 @@ export const WithDefaultMessage = () => {
475475

476476
const NUMBER_OF_ITEMS = 1800
477477
const lotsOfItems = Array.from({length: NUMBER_OF_ITEMS}, (_, index) => {
478+
if (index === 5) {
479+
return {
480+
id: index,
481+
text: `This is being used to show what would happen if you returned an item that needed text wrapping`,
482+
description: `Description ${index}`,
483+
leadingVisual: getColorCircle('#a2eeef'),
484+
}
485+
}
478486
return {
479487
id: index,
480488
text: `Item ${index}`,
@@ -620,8 +628,10 @@ export const Virtualized = () => {
620628
getScrollElement: () => scrollContainer ?? null,
621629
estimateSize: () => DEFAULT_VIRTUAL_ITEM_HEIGHT,
622630
overscan: 10,
623-
debug: true,
624631
enabled: renderSubset,
632+
measureElement: el => {
633+
return (el as HTMLElement).scrollHeight
634+
},
625635
})
626636

627637
const virtualizedContainerStyle = useMemo(
@@ -645,6 +655,12 @@ export const Virtualized = () => {
645655
return {
646656
...item,
647657
key: virtualItem.index,
658+
'data-index': virtualItem.index,
659+
ref: (node: Element | null) => {
660+
if (node && node.getAttribute('data-index')) {
661+
virtualizer.measureElement(node)
662+
}
663+
},
648664
style: {
649665
position: 'absolute',
650666
top: 0,

0 commit comments

Comments
 (0)