From a3c2bd398f3c9ee72d9e64bd930df7737f170144 Mon Sep 17 00:00:00 2001 From: Lawrence Holmes Date: Mon, 12 Feb 2024 18:18:29 +0000 Subject: [PATCH] fix: support false-only variant with fallback behaviour --- src/__tests__/tv.test.ts | 36 ++++++++++++++++++++++++++++++++++++ src/index.js | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/__tests__/tv.test.ts b/src/__tests__/tv.test.ts index 641f612..3e84406 100644 --- a/src/__tests__/tv.test.ts +++ b/src/__tests__/tv.test.ts @@ -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"]); + }); + + + 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", diff --git a/src/index.js b/src/index.js index 920dc2b..8b0e806 100644 --- a/src/index.js +++ b/src/index.js @@ -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" &&