Skip to content

Commit

Permalink
Merge pull request #159 from lrholmes/fix-false-variant-case
Browse files Browse the repository at this point in the history
fix: Support false-only variant with fallback behaviour
  • Loading branch information
mskelton authored Feb 12, 2024
2 parents a26d801 + a3c2bd3 commit 183f21d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/__tests__/tv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,42 @@ describe("Tailwind Variants (TV) - Default", () => {
expect(h1({bool: undefined})).toHaveClass(["text-3xl", "truncate"]);
});

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

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


Check warning on line 353 in src/__tests__/tv.test.ts

View workflow job for this annotation

GitHub Actions / pnpm (lint)

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

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

test("should support boolean variants -- default variants", () => {
const h1 = tv({
base: "text-3xl",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export const tv = (options, configProp) => {
? variantKey
: falsyToString(defaultVariantProp);

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

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

0 comments on commit 183f21d

Please sign in to comment.