fix(DefaultLegendContent): use entry.value for aria-label when formatter returns React element#7109
Conversation
…ter returns React element When a Legend formatter returns a React element (JSX), the aria-label on each legend icon SVG was set to '[object Object] legend icon' because the formatter's return value was stringified via a template literal. The aria-label should always reflect the human-readable name of the series, not the visual representation. Use entry.value (the raw string passed into formatter) for the aria-label instead. Fixes recharts#7105
WalkthroughThe PR modifies the Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes Possibly related issues
Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/component/DefaultLegendContent.tsx`:
- Line 241: Guard the aria-label on the Legend item to avoid stringifying
undefined: in DefaultLegendContent where the JSX sets
aria-label={`${entry.value} legend icon`}, change it to conditionally use a safe
fallback (e.g. entry.value ?? '' or a more descriptive fallback like
entry.payload?.name ?? 'legend') so you never render "undefined legend icon";
update the aria-label expression (referencing entry.value and
entry.payload/name) to build a meaningful label only when value exists and
otherwise produce a safe default.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: fbe18990-92e1-4890-b4f2-d122b316d10d
📒 Files selected for processing (2)
src/component/DefaultLegendContent.tsxtest/component/Legend.spec.tsx
| viewBox={viewBox} | ||
| style={svgStyle} | ||
| aria-label={`${finalValue} legend icon`} | ||
| aria-label={`${entry.value} legend icon`} |
There was a problem hiding this comment.
Guard the aria-label when entry.value is missing.
Line 241 still stringifies LegendPayload.value, so unnamed legend items end up with aria-label="undefined legend icon". LegendPayload.value is explicitly optional here, and the payload builders shown in src/util/ChartUtils.ts can produce undefined, so this remains a real accessibility bug.
Suggested fix
- aria-label={`${entry.value} legend icon`}
+ aria-label={entry.value == null ? undefined : `${entry.value} legend icon`}Based on learnings: Ensure accessibility is important in Recharts components and implementation.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| aria-label={`${entry.value} legend icon`} | |
| aria-label={entry.value == null ? undefined : `${entry.value} legend icon`} |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/component/DefaultLegendContent.tsx` at line 241, Guard the aria-label on
the Legend item to avoid stringifying undefined: in DefaultLegendContent where
the JSX sets aria-label={`${entry.value} legend icon`}, change it to
conditionally use a safe fallback (e.g. entry.value ?? '' or a more descriptive
fallback like entry.payload?.name ?? 'legend') so you never render "undefined
legend icon"; update the aria-label expression (referencing entry.value and
entry.payload/name) to build a meaningful label only when value exists and
otherwise produce a safe default.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7109 +/- ##
=======================================
Coverage 89.62% 89.62%
=======================================
Files 534 534
Lines 40193 40193
Branches 5465 5465
=======================================
Hits 36024 36024
Misses 4161 4161
Partials 8 8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Problem
When a
Legendformatter returns a React element (JSX), thearia-labelon each legend icon<svg>ends up as"[object Object] legend icon"because the formatter's return value is coerced to a string via a template literal.Repro:
Produces:
aria-label="[object Object] legend icon"Expected:
aria-label="UV legend icon"Fixes #7105.
Root Cause
In
DefaultLegendContent.tsx, the icon surface element usesfinalValue(the formatter's return value) for thearia-label:finalValuemay be a React element, which stringifies to[object Object].Fix
Use
entry.value(the raw string data key/name) for thearia-labelinstead offinalValue. The formatter output is the visual label and may be arbitrary JSX; the accessible name should always reflect the underlying series name.Test
A new test case in
test/component/Legend.spec.tsxverifies thataria-labeluses the raw entry value even when the formatter returns JSX.Summary by CodeRabbit