Skip to content

fix: add missing sector index to Pie shape#6683

Merged
ckifer merged 2 commits intomainfrom
fix/add-index-to-pie-shape
Nov 25, 2025
Merged

fix: add missing sector index to Pie shape#6683
ckifer merged 2 commits intomainfrom
fix/add-index-to-pie-shape

Conversation

@ckifer
Copy link
Member

@ckifer ckifer commented Nov 24, 2025

Description

I forgot to pass index to the new Pie.shape prop. It makes the shape prop a million times more useful

I'm using a numeric index for now... I think its easier than strings and we're already passing number in Bar.rectangle and in all of the click handler functions

Related Issue

Fixes #6052 and #5999

Motivation and Context

How Has This Been Tested?

Adjusted storybook, adjusted tests

Screenshots (if appropriate):

recharts-multi-select.mov

Types of changes

Ehh, feature that was intended to be released in previous minor version

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • I have added a storybook story or VR test, or extended an existing story or VR test to show my changes

Summary by CodeRabbit

  • New Features

    • Pie component now supports shape prop for conditional rendering based on active/inactive state.
    • Enhanced shape functions receive richer data including index and sector metadata.
    • Exported Sector component for direct use.
  • Bug Fixes

    • Updated shape function signatures to properly pass index information.

✏️ Tip: You can customize this high-level summary in your review settings.

@ckifer ckifer requested a review from PavelVanecek November 24, 2025 05:56
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 24, 2025

Walkthrough

This PR changes Pie shape APIs to pass an index to shape/active-shape functions, introduces a new shape prop for function-based rendering, exports Sector, and updates tests and storybook examples to use the new signatures and payload shape.

Changes

Cohort / File(s) Summary
Pie core & utilities
src/polar/Pie.tsx, src/util/ActiveShapeUtils.tsx
Added index to public shape types and payloads; Shape/active-shape invocation now supplies index and calls function form as option(props, props.index) (two-arg form).
Examples / Storybook
storybook/stories/Examples/Pie/CustomActiveShapePieChart.stories.tsx, storybook/stories/Examples/Pie/PieWithCells.stories.tsx
Updated examples to use the new shape prop (function receives isActive and index), added interactivity state for active indices, and started importing/exporting Sector.
Tests
test/polar/Pie.spec.tsx
Updated tests to assert the shape callback receives richer payloads (including index, coordinates, percent, start/end angles, tooltip payload) and switched assertions from activeShape to shape.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant Pie as Pie Component
    participant ShapeFn as shape function
    participant Sector as Sector Component

    Note over App,Pie: Render Pie with shape={renderFn}

    App->>Pie: render pie (data, shape=renderFn)
    loop for each sector (i)
        Pie->>ShapeFn: renderFn(props, i)
        Note right of ShapeFn: props includes isActive, payload, index, percent, angles
        alt isActive
            ShapeFn->>Sector: return custom active Sector/group
        else not active
            ShapeFn->>Sector: return plain Sector
        end
        Sector-->>Pie: rendered SVG element
    end
    Pie-->>App: assembled SVG pie
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Areas needing extra attention:
    • src/util/ActiveShapeUtils.tsx — verify invocation change covers all function/JSX variants and typing.
    • src/polar/Pie.tsx — ensure index is consistently forwarded for all sector render paths.
    • test/polar/Pie.spec.tsx — confirm expanded payload assertions match runtime payload shape.

Possibly related PRs

Suggested reviewers

  • PavelVanecek

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: add missing sector index to Pie shape' clearly and concisely describes the main change: adding the missing index parameter to the Pie shape prop.
Description check ✅ Passed The PR description provides sufficient context: explains what was forgotten (index parameter), rationale for using numeric index, links relevant issues, describes testing approach, and marks appropriate checklist items.
Linked Issues check ✅ Passed The PR successfully addresses the linked issue #6052 by restoring the ability to target individual Pie slices with an index-based mechanism, enabling custom rendering per slice and interactive behaviors like click-to-select.
Out of Scope Changes check ✅ Passed All changes are directly focused on adding the missing index parameter to the Pie shape prop and updating relevant files (types, implementations, tests, and stories) to support this feature consistently.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/add-index-to-pie-shape

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 820bb7a and c2b03f4.

📒 Files selected for processing (2)
  • src/polar/Pie.tsx (2 hunks)
  • test/polar/Pie.spec.tsx (2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-11-23T13:30:10.388Z
Learnt from: PavelVanecek
Repo: recharts/recharts PR: 6669
File: www/src/docs/exampleComponents/ScatterChart/ScatterChartWithLabels.tsx:2-2
Timestamp: 2025-11-23T13:30:10.388Z
Learning: The `TooltipIndex` type from recharts is defined in `src/state/tooltipSlice.ts` but is not currently exported from the public API entry points. It should not be imported from `recharts/types/state/tooltipSlice` as this is an internal implementation path. An ESLint rule is needed to prevent regressions.

Applied to files:

  • src/polar/Pie.tsx
🧬 Code graph analysis (2)
src/polar/Pie.tsx (2)
src/index.ts (1)
  • PieSectorDataItem (56-56)
src/util/ActiveShapeUtils.tsx (1)
  • Shape (81-108)
test/polar/Pie.spec.tsx (2)
src/shape/Sector.tsx (1)
  • Sector (213-254)
src/util/Constants.ts (2)
  • DATA_ITEM_INDEX_ATTRIBUTE_NAME (33-33)
  • DATA_ITEM_DATAKEY_ATTRIBUTE_NAME (56-56)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Build, Test, Pack
🔇 Additional comments (4)
src/polar/Pie.tsx (2)

159-160: LGTM! Index parameter added to enable per-slice customization.

The addition of index to both PieSectorShapeProps and as a second parameter to the PieShape function signature successfully enables developers to apply different styles or behaviors to individual slices. The index appears in both locations intentionally: within the props object for destructuring convenience and as a separate parameter for consistency with existing patterns like Bar.rectangle.


571-571: No changes needed—index prop is properly typed.

The index prop in the Shape component is correctly defined as index?: string | number in ShapeProps (src/util/ActiveShapeUtils.tsx:31), and passing the numeric loop counter i is fully compatible with this type signature.

test/polar/Pie.spec.tsx (2)

159-181: LGTM! Test demonstrates the new shape prop API.

This test correctly demonstrates the new shape prop with a function that receives { isActive, ...props } and conditionally applies a className based on the active state. This validates the new API for per-slice customization.


357-410: LGTM! Test expectations updated for two-argument shape function.

The test correctly verifies that the shape/activeShape function is called with two arguments:

  1. First argument (lines 358-408): The complete sector props object including index: 0 at line 366
  2. Second argument (line 409): The index value 0 passed separately

This validates both the backward compatibility with deprecated activeShape and the new dual-parameter signature. The redundancy of having index in both places is intentional for developer convenience.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/polar/Pie.tsx (1)

537-538: Remove leftover debug console.log

The commented // console.log(onClickFromContext); appears to be debugging residue and can be removed to keep the render path clean.

test/polar/Pie.spec.tsx (1)

138-160: Test description now mismatches the prop under test

This test now exercises shape={...} rather than activeShape, but the description still says “when activeShape is set to be an element”; consider renaming to mention shape so it’s clear which API the test is covering.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 10d58e9 and 820bb7a.

📒 Files selected for processing (5)
  • src/polar/Pie.tsx (3 hunks)
  • src/util/ActiveShapeUtils.tsx (2 hunks)
  • storybook/stories/Examples/Pie/CustomActiveShapePieChart.stories.tsx (3 hunks)
  • storybook/stories/Examples/Pie/PieWithCells.stories.tsx (2 hunks)
  • test/polar/Pie.spec.tsx (2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-11-23T13:30:10.388Z
Learnt from: PavelVanecek
Repo: recharts/recharts PR: 6669
File: www/src/docs/exampleComponents/ScatterChart/ScatterChartWithLabels.tsx:2-2
Timestamp: 2025-11-23T13:30:10.388Z
Learning: The `TooltipIndex` type from recharts is defined in `src/state/tooltipSlice.ts` but is not currently exported from the public API entry points. It should not be imported from `recharts/types/state/tooltipSlice` as this is an internal implementation path. An ESLint rule is needed to prevent regressions.

Applied to files:

  • src/polar/Pie.tsx
  • storybook/stories/Examples/Pie/PieWithCells.stories.tsx
🧬 Code graph analysis (3)
src/polar/Pie.tsx (2)
src/index.ts (1)
  • PieSectorDataItem (56-56)
src/util/ActiveShapeUtils.tsx (1)
  • Shape (81-108)
storybook/stories/Examples/Pie/PieWithCells.stories.tsx (1)
src/polar/Pie.tsx (1)
  • Pie (873-913)
test/polar/Pie.spec.tsx (3)
src/shape/Sector.tsx (1)
  • Sector (213-254)
src/index.ts (1)
  • Sector (32-32)
src/util/Constants.ts (2)
  • DATA_ITEM_INDEX_ATTRIBUTE_NAME (33-33)
  • DATA_ITEM_DATAKEY_ATTRIBUTE_NAME (56-56)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Build, Test, Pack
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (7)
src/util/ActiveShapeUtils.tsx (2)

27-34: Index added to ShapeProps looks consistent and safe

Adding index?: string | number to ShapeProps matches how Shape is now used in Pie and other charts and stays backward‑compatible since it’s optional and intersected with ExtraProps. No issues here.


90-95: Passing props.index into function shape is the right extension

Calling function options as option(props, props.index) correctly exposes the index to shape/activeShape callbacks while remaining non‑breaking for existing single‑argument handlers that will simply ignore the second parameter.

src/polar/Pie.tsx (2)

159-160: PieSectorShapeProps/PieShape definitions align with Shape usage

Defining PieSectorShapeProps as PieSectorDataItem & { isActive: boolean; index: number } and PieShape as a two‑argument function matches what PieSectors passes into Shape and what Shape forwards to user callbacks; this gives both props.index and the numeric index parameter reliably.


545-573: Index forwarding into Shape is correct and matches the new API

Passing index={i} into <Shape option={shape ?? sectorOptions} ...> ensures both shape and activeShape/inactiveShape functions can receive a numeric index via the second argument and via props.index, which matches the updated tests and story usage.

storybook/stories/Examples/Pie/CustomActiveShapePieChart.stories.tsx (1)

22-80: Story correctly demonstrates new Pie.shape behavior

Using renderActiveShape as (props: PieSectorDataItem & { isActive: boolean }) => ReactElement and wiring it via shape={renderActiveShape} matches the new Pie shape API; the isActive-based branch plus the plain <Sector {...props} /> fallback is a good, minimal example.

Also applies to: 85-85

storybook/stories/Examples/Pie/PieWithCells.stories.tsx (1)

3-3: Interactive highlighting via shape and click index looks solid

Tracking activeIndices driven by onClick={(_, index) => { ... }} and using shape={(props, index) => ...} to conditionally render <Sector> with a different fill is a clean demonstration of the new numeric index being passed through; the toggle logic on activeIndices is straightforward and correct.

Also applies to: 21-42

test/polar/Pie.spec.tsx (1)

335-412: Expectation on activeShape call correctly asserts index propagation

Updating expect(activeShape).toHaveBeenCalledWith(...) to include the index: 0 field in the props object and the explicit second argument 0 matches the new behavior where Shape forwards both props.index and the numeric index parameter to activeShape. This provides good coverage that the bug fix (missing index) is actually wired through.

@codecov
Copy link

codecov bot commented Nov 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.02%. Comparing base (10d58e9) to head (c2b03f4).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6683   +/-   ##
=======================================
  Coverage   94.02%   94.02%           
=======================================
  Files         499      499           
  Lines       42547    42547           
  Branches     4873     4873           
=======================================
  Hits        40004    40004           
  Misses       2538     2538           
  Partials        5        5           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codecov
Copy link

codecov bot commented Nov 24, 2025

Bundle Report

Changes will increase total bundle size by 74 bytes (0.0%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
recharts/bundle-cjs 1.14MB 29 bytes (0.0%) ⬆️
recharts/bundle-es6 987.65kB 29 bytes (0.0%) ⬆️
recharts/bundle-umd 518.5kB 16 bytes (0.0%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: recharts/bundle-umd

Assets Changed:

Asset Name Size Change Total Size Change (%)
Recharts.js 16 bytes 518.5kB 0.0%
view changes for bundle: recharts/bundle-es6

Assets Changed:

Asset Name Size Change Total Size Change (%)
polar/Pie.js 16 bytes 23.54kB 0.07%
util/ActiveShapeUtils.js 13 bytes 4.92kB 0.27%
view changes for bundle: recharts/bundle-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
polar/Pie.js 16 bytes 25.29kB 0.06%
util/ActiveShapeUtils.js 13 bytes 5.91kB 0.22%

@ckifer ckifer merged commit 0710526 into main Nov 25, 2025
27 of 29 checks passed
@ckifer ckifer deleted the fix/add-index-to-pie-shape branch November 25, 2025 02:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v3: Pie chart no longer supports Slices

2 participants