Skip to content

Commit

Permalink
Merge pull request #33 from pyyding/kp-tailwind-class-overrides
Browse files Browse the repository at this point in the history
make className prop overridable
  • Loading branch information
ferrucc-io authored May 17, 2024
2 parents 108b8db + 95877f8 commit 0f65ac9
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/components/Button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const Button = React.forwardRef<
if (rest.href) {
return (
<a
className={cx(className, buttonStyle)}
className={cx(buttonStyle, className)}
{...(rest as React.AnchorHTMLAttributes<HTMLAnchorElement>)}
ref={ref as React.RefObject<HTMLAnchorElement>}
>
Expand All @@ -133,7 +133,7 @@ export const Button = React.forwardRef<
<button
disabled={isDisabled}
aria-label={loading && !loadingText ? "Loading, please wait" : undefined}
className={cx(className, buttonStyle)}
className={cx(buttonStyle, className)}
{...(buttonProps as React.ButtonHTMLAttributes<HTMLButtonElement>)}
ref={ref as React.RefObject<HTMLButtonElement>}
>
Expand Down
2 changes: 1 addition & 1 deletion lib/components/ChartLabel/ChartLabelText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function ChartLabelText({
dy={dy}
dx={dx}
stroke="none"
className={cx(className, "fill-purple-500")}
className={cx("fill-purple-500", className)}
>
{children}
</text>
Expand Down
2 changes: 1 addition & 1 deletion lib/components/LineChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const LineChart: React.FC<LineChartProps> = ({
...rest
}) => (
<div
className={cx(className, "h-80 w-full")}
className={cx("h-80 w-full", className)}
data-testid="line-chart-wrapper"
>
<RechartsResponsiveContainer width="100%" height="100%">
Expand Down
26 changes: 16 additions & 10 deletions lib/components/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ValueFormatter } from "../../configurationTypes";
import { twMerge } from "tailwind-merge";

export const getYAxisDomain = (
autoMinValue: boolean,
Expand All @@ -10,7 +11,6 @@ export const getYAxisDomain = (
return [minDomain, maxDomain];
};


export const constructCategoryColors = (
categories: string[],
colors: string[],
Expand All @@ -22,11 +22,15 @@ export const constructCategoryColors = (
return categoryColors;
};


export function deepEqual(obj1: any, obj2: any) {
if (obj1 === obj2) return true;

if (typeof obj1 !== "object" || typeof obj2 !== "object" || obj1 === null || obj2 === null)
if (
typeof obj1 !== "object" ||
typeof obj2 !== "object" ||
obj1 === null ||
obj2 === null
)
return false;

const keys1 = Object.keys(obj1);
Expand All @@ -44,14 +48,16 @@ export function deepEqual(obj1: any, obj2: any) {
export function cx(
...args: Array<undefined | null | string | boolean>
): string {
return args
.flat()
.filter((x) => typeof x === "string")
.join(" ");
return twMerge(
args
.flat()
.filter((x) => typeof x === "string")
.join(" "),
);
}


export const defaultValueFormatter: ValueFormatter = (value: number) => value.toString();
export const defaultValueFormatter: ValueFormatter = (value: number) =>
value.toString();

// We use this internally at June to track the loaded state of the chart when taking screenshots for things like Email & Slack digests
export function addLoadedIdToElement() {
Expand All @@ -61,4 +67,4 @@ export function addLoadedIdToElement() {
if (elements.length > 0 && elements[0]) {
elements[0].id = "loaded";
}
}
}
15 changes: 14 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
},
"dependencies": {
"@headlessui/react": "^1.7.19",
"recharts": "^2.12.5"
"recharts": "^2.12.5",
"tailwind-merge": "^2.3.0"
},
"devDependencies": {
"@storybook/addon-essentials": "^8.0.8",
Expand Down
13 changes: 11 additions & 2 deletions src/tests/common/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
describe("utils", () => {
describe("getYAxisDomain", () => {
it("should return auto for both min and max when autoMinValue is true", () => {
expect(getYAxisDomain(true, undefined, undefined)).toEqual(["auto", "auto"]);
expect(getYAxisDomain(true, undefined, undefined)).toEqual([
"auto",
"auto",
]);
});

it("should return provided minValue and auto for max when autoMinValue is false", () => {
Expand Down Expand Up @@ -54,7 +57,13 @@ describe("utils", () => {

describe("cx", () => {
it("should concatenate class names into a single string", () => {
expect(cx("class1", null, "class2", undefined, false, "class3")).toBe("class1 class2 class3");
expect(cx("class1", null, "class2", undefined, false, "class3")).toBe(
"class1 class2 class3",
);
});

it("should override tailwind classes", () => {
expect(cx("text-red-500", "text-blue-500")).toBe("text-blue-500");
});
});

Expand Down

0 comments on commit 0f65ac9

Please sign in to comment.