Skip to content

Commit

Permalink
fix: tooltip defaultIndex can be out of range by 1 (#5054)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckifer authored Sep 28, 2024
1 parent 454530d commit 6126c77
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/chart/generateCategoricalChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ export const generateCategoricalChart = ({
const { defaultIndex } = tooltipElem.props;

// Protect against runtime errors
if (typeof defaultIndex !== 'number' || defaultIndex < 0 || defaultIndex > this.state.tooltipTicks.length) {
if (typeof defaultIndex !== 'number' || defaultIndex < 0 || defaultIndex > this.state.tooltipTicks.length - 1) {
return;
}

Expand Down
15 changes: 15 additions & 0 deletions test/component/Tooltip.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -561,4 +561,19 @@ describe('<Tooltip />', () => {
expect(tooltip).toBeInTheDocument();
expect(tooltip).not.toBeVisible();
});

test('defaultIndex of data.length should be ignored since defaultIndex is 0 based', () => {
const { container } = render(
<div role="main" style={{ width: '400px', height: '400px' }}>
<AreaChart width={400} height={400} data={data}>
<Area dataKey="uv" />
<Tooltip defaultIndex={data.length} />
</AreaChart>
</div>,
);

const tooltip = container.querySelector('.recharts-tooltip-wrapper');
expect(tooltip).toBeInTheDocument();
expect(tooltip).not.toBeVisible();
});
});

0 comments on commit 6126c77

Please sign in to comment.