Skip to content

Commit

Permalink
fix: treat undefined as false for boolean variants (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton authored Feb 9, 2024
1 parent ed21cdd commit ddc1554
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
72 changes: 72 additions & 0 deletions src/__tests__/tv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,78 @@ describe("Tailwind Variants (TV) - Default", () => {

expect(h1({color: "green", isUnderline: false})).toBe(expectedResult);
});

test("should support boolean variants", () => {
const h1 = tv({
base: "text-3xl",
variants: {
bool: {
true: "underline",
false: "truncate",
},
},
});

expect(h1()).toHaveClass(["text-3xl", "truncate"]);
expect(h1({bool: true})).toHaveClass(["text-3xl", "underline"]);
expect(h1({bool: false})).toHaveClass(["text-3xl", "truncate"]);
expect(h1({bool: undefined})).toHaveClass(["text-3xl", "truncate"]);
});

test("should support boolean variants -- default variants", () => {
const h1 = tv({
base: "text-3xl",
variants: {
bool: {
true: "underline",
false: "truncate",
},
},
defaultVariants: {
bool: true,
},
});

expect(h1()).toHaveClass(["text-3xl", "underline"]);
expect(h1({bool: true})).toHaveClass(["text-3xl", "underline"]);
expect(h1({bool: false})).toHaveClass(["text-3xl", "truncate"]);
expect(h1({bool: undefined})).toHaveClass(["text-3xl", "underline"]);
});

test("should support boolean variants -- missing false variant", () => {
const h1 = tv({
base: "text-3xl",
variants: {
bool: {
true: "underline",
},
},
});

expect(h1()).toHaveClass(["text-3xl"]);
expect(h1({bool: true})).toHaveClass(["text-3xl", "underline"]);
expect(h1({bool: false})).toHaveClass(["text-3xl"]);
expect(h1({bool: undefined})).toHaveClass(["text-3xl"]);
});

test("should support boolean variants -- missing false variant -- default variants", () => {
const h1 = tv({
base: "text-3xl",
variants: {
bool: {
true: "underline",
},
},
defaultVariants: {
bool: true,
},
});

expect(h1()).toHaveClass(["text-3xl", "underline"]);
expect(h1({bool: true})).toHaveClass(["text-3xl", "underline"]);
expect(h1({bool: false})).toHaveClass(["text-3xl"]);
expect(h1({bool: undefined})).toHaveClass(["text-3xl", "underline"]);
});
});

describe("Tailwind Variants (TV) - Slots", () => {
Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,14 @@ export const tv = (options, configProp) => {
}
}

const value = variantObj[variantKey] || variantObj[falsyToString(defaultVariantProp)];
// If there is a variant key and it's not an object (screen variants),
// we use the variant key and ignore the default variant.
const key =
variantKey != null && typeof variantKey != "object"
? variantKey
: falsyToString(defaultVariantProp);

const value = variantObj[key] || variantObj["false"];

if (
typeof screenValues === "object" &&
Expand Down

0 comments on commit ddc1554

Please sign in to comment.