-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from pyyding/kp-chart-label
Chart Label component
- Loading branch information
Showing
5 changed files
with
155 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ViewBox } from "recharts/types/util/types"; | ||
import React from "react"; | ||
import { cx } from "../common/utils.ts"; | ||
|
||
export type ChartLabelTextProps = { | ||
children: React.ReactNode; | ||
dy?: number; | ||
dx?: number; | ||
viewBox?: ViewBox; | ||
className?: string; | ||
}; | ||
|
||
export function ChartLabelText({ | ||
children, | ||
dy = -5, | ||
dx = 0, | ||
viewBox, | ||
className, | ||
}: ChartLabelTextProps) { | ||
if (!viewBox) { | ||
console.warn("ChartLabelText requires a viewBox prop"); | ||
return null; | ||
} | ||
|
||
const isCartesianViewBox = "x" in viewBox && "y" in viewBox; | ||
|
||
if (!isCartesianViewBox) { | ||
console.warn("ChartLabelText currently only accepts CartesianViewBox"); | ||
return null; | ||
} | ||
|
||
return ( | ||
<text | ||
fontSize="10px" | ||
x={viewBox.x} | ||
y={viewBox.y} | ||
dy={dy} | ||
dx={dx} | ||
stroke="none" | ||
className={cx(className, "fill-purple-500")} | ||
> | ||
{children} | ||
</text> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { | ||
Label as ChartLabel, | ||
type LabelProps as ChartLabelProps, | ||
} from "recharts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { render, screen } from "../test-utils.ts"; | ||
import { ChartLabelText } from "../../../lib/main.ts"; | ||
|
||
describe("Component ChartLabelText", () => { | ||
it("should render when correct viewBox is provided", async () => { | ||
render( | ||
<svg> | ||
<ChartLabelText viewBox={{ x: 0, y: 0 }}>test-label</ChartLabelText>, | ||
</svg>, | ||
); | ||
expect(await screen.findByText("test-label")).toBeInTheDocument(); | ||
}); | ||
|
||
it("should not render when no viewBox", () => { | ||
render( | ||
<svg> | ||
<ChartLabelText>test-label</ChartLabelText> | ||
</svg>, | ||
); | ||
expect(screen.queryByText("test-label")).not.toBeInTheDocument(); | ||
}); | ||
|
||
it("should not render when viewBox is PolarViewBox", () => { | ||
render( | ||
<svg> | ||
<ChartLabelText viewBox={{ cx: 0, cy: 0 }}>test-label</ChartLabelText> | ||
</svg>, | ||
); | ||
expect(screen.queryByText("test-label")).not.toBeInTheDocument(); | ||
}); | ||
|
||
it("should render with custom classNames", async () => { | ||
render( | ||
<svg> | ||
<ChartLabelText viewBox={{ x: 0, y: 0 }} className="fill-red-500"> | ||
test-label | ||
</ChartLabelText> | ||
</svg>, | ||
); | ||
expect(await screen.findByText("test-label")).toHaveClass("fill-red-500"); | ||
}); | ||
|
||
it("should override default dy and dx", async () => { | ||
render( | ||
<svg> | ||
<ChartLabelText viewBox={{ x: 0, y: 0 }} dy={12} dx={34}> | ||
test-label | ||
</ChartLabelText> | ||
</svg>, | ||
); | ||
|
||
const element = await screen.findByText("test-label"); | ||
|
||
expect(element).toHaveAttribute("dy", "12"); | ||
expect(element).toHaveAttribute("dx", "34"); | ||
}); | ||
}); |