Skip to content

Commit

Permalink
fix(donut-chart): reset the active index when the items change
Browse files Browse the repository at this point in the history
  • Loading branch information
yomatters committed Aug 21, 2024
1 parent 5c7ef8a commit 70ec29d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/donut-chart.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
display: flex;
flex-direction: row;
font-size: 1rem;
line-height: 1.2;
margin-bottom: 5px;
}
.donut-chart .symbol {
Expand Down
6 changes: 5 additions & 1 deletion src/donut-chart.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "preact/hooks";
import { useEffect, useState } from "preact/hooks";

const circleCoords = (pct, radius) =>
`${Math.cos(2 * Math.PI * pct) * radius} ${
Expand Down Expand Up @@ -55,6 +55,10 @@ export default function DonutChart({
topItemsHeading = null,
}) {
const [activeIdx, setActiveIdx] = useState(null);

// Reset the active index when the items change.
useEffect(() => setActiveIdx(null), [items]);

const sortedItems = items.toSorted((a, b) =>
a.count < b.count || (a.count == b.count && a.name < b.name) ? 1 : -1
);
Expand Down

0 comments on commit 70ec29d

Please sign in to comment.