Skip to content

[ActionList] Prevent title="[object Object]" when Description has non-string children #6223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/brown-rules-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Fix ActionList.Description title attribute for non-string children with truncate
2 changes: 1 addition & 1 deletion packages/react/src/ActionList/ActionList.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,4 @@
]
}
]
}
}
12 changes: 12 additions & 0 deletions packages/react/src/ActionList/ActionList.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,18 @@ export const TextWrapAndTruncation = () => (
<ArrowLeftIcon />
</ActionList.TrailingVisual>
</ActionList.Item>
<ActionList.Item>
<ActionList.LeadingVisual>
<ArrowRightIcon />
</ActionList.LeadingVisual>
Description with truncation and complex children
<ActionList.Description truncate>
With <strong>bold</strong> and <em>italic</em> text, and it should truncate if it is too long
</ActionList.Description>
<ActionList.TrailingVisual>
<ArrowLeftIcon />
</ActionList.TrailingVisual>
</ActionList.Item>
<ActionList.Item>
<ActionList.LeadingVisual>
<ArrowRightIcon />
Expand Down
28 changes: 28 additions & 0 deletions packages/react/src/ActionList/ActionList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,32 @@ describe('ActionList', () => {
expect(container.querySelector('li[aria-disabled="true"]')?.nextElementSibling).toHaveTextContent('Option 4')
expect(container.querySelector('li[aria-disabled="true"]')?.nextElementSibling).toHaveAttribute('tabindex', '0')
})

it('sets title correctly for Description component', () => {
const {container} = HTMLRender(
<ActionList>
<ActionList.Item>
Option 1<ActionList.Description truncate>Simple string description</ActionList.Description>
</ActionList.Item>
<ActionList.Item>
Option 2
<ActionList.Description truncate>
<span>Complex</span> content
</ActionList.Description>
</ActionList.Item>
<ActionList.Item>
Option 3
<ActionList.Description>
<span>Non-truncated</span> content
</ActionList.Description>
</ActionList.Item>
</ActionList>,
)

const descriptions = container.querySelectorAll('[data-component="ActionList.Description"]')

expect(descriptions[0]).toHaveAttribute('title', 'Simple string description')
expect(descriptions[1]).toHaveAttribute('title', 'Complex content')
expect(descriptions[2]).not.toHaveAttribute('title')
})
})
16 changes: 15 additions & 1 deletion packages/react/src/ActionList/Description.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type ActionListDescriptionProps = {
* - `"block"` - Secondary text is positioned below primary text.
*/
variant?: 'inline' | 'block'

className?: string
/**
* Whether the inline description should truncate the text on overflow.
Expand All @@ -30,6 +31,18 @@ export const Description: React.FC<React.PropsWithChildren<ActionListDescription
...props
}) => {
const {blockDescriptionId, inlineDescriptionId} = React.useContext(ItemContext)
const containerRef = React.useRef<HTMLDivElement>(null)
const [computedTitle, setComputedTitle] = React.useState<string>('')

// Extract text content from rendered DOM for tooltip
React.useEffect(() => {
if (truncate && containerRef.current) {
const textContent = containerRef.current.textContent || ''
setComputedTitle(textContent)
}
}, [truncate, props.children])

const effectiveTitle = typeof props.children === 'string' ? props.children : computedTitle

if (variant === 'block' || !truncate) {
return (
Expand All @@ -46,10 +59,11 @@ export const Description: React.FC<React.PropsWithChildren<ActionListDescription
} else {
return (
<Truncate
ref={containerRef}
id={inlineDescriptionId}
className={clsx(className, classes.Description)}
sx={sx}
title={props.children as string}
title={effectiveTitle}
inline={true}
maxWidth="100%"
data-component="ActionList.Description"
Expand Down
Loading