Skip to content

Commit

Permalink
fix: treat undefined as false for boolean variants
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton committed Jan 14, 2024
1 parent ed21cdd commit 18a1a33
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/__tests__/tv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" &&
Expand Down

0 comments on commit 18a1a33

Please sign in to comment.