From 18a1a33d7186aec2a7e4729433914ef01b02731f Mon Sep 17 00:00:00 2001 From: Mark Skelton Date: Sun, 14 Jan 2024 15:55:03 -0600 Subject: [PATCH] fix: treat `undefined` as `false` for boolean variants --- src/__tests__/tv.test.ts | 16 ++++++++++++++++ src/index.js | 5 ++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/__tests__/tv.test.ts b/src/__tests__/tv.test.ts index a2cedcf..26195e5 100644 --- a/src/__tests__/tv.test.ts +++ b/src/__tests__/tv.test.ts @@ -314,6 +314,22 @@ describe("Tailwind Variants (TV) - Default", () => { expect(h1({color: "green", isUnderline: false})).toBe(expectedResult); }); + + test("should treat undefined and false identically for boolean variants", () => { + const h1 = tv({ + base: "text-3xl", + variants: { + bool: { + true: "underline", + false: "no-underline", + }, + }, + }); + + expect(h1({bool: true})).toBe("text-3xl underline"); + expect(h1({bool: false})).toBe("text-3xl no-underline"); + expect(h1()).toBe("text-3xl no-underline"); + }); }); describe("Tailwind Variants (TV) - Slots", () => { diff --git a/src/index.js b/src/index.js index 0c90d60..52330fc 100644 --- a/src/index.js +++ b/src/index.js @@ -199,7 +199,10 @@ export const tv = (options, configProp) => { } } - const value = variantObj[variantKey] || variantObj[falsyToString(defaultVariantProp)]; + const value = + variantObj[variantKey] || + variantObj[falsyToString(defaultVariantProp)] || + variantObj["false"]; if ( typeof screenValues === "object" &&