Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: treat undefined value for compoundVariants as false #210

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/__tests__/tv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,33 @@ describe("Tailwind Variants (TV) - Default", () => {
color: "red",
class: "bg-red-500",
},
{
isBig: false,
color: "red",
class: "underline",
},
],
});

const result = h1({
isBig: true,
color: "red",
});
expect(
h1({
isBig: true,
color: "red",
}),
).toHaveClass(["text-5xl", "font-bold", "text-red-500", "bg-red-500"]);

const expectedResult = ["text-5xl", "font-bold", "text-red-500", "bg-red-500"];
expect(
h1({
isBig: false,
color: "red",
}),
).toHaveClass(["text-2xl", "font-bold", "text-red-500", "underline"]);

expect(result).toHaveClass(expectedResult);
expect(
h1({
color: "red",
}),
).toHaveClass(["text-2xl", "font-bold", "text-red-500", "underline"]);
});

test("should throw error if the compoundVariants is not an array", () => {
Expand Down
10 changes: 7 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,19 @@ export const tv = (options, configProp) => {
let isValid = true;

for (const [key, value] of Object.entries(compoundVariantOptions)) {
const completeProps = getCompleteProps(key, slotProps);
const completePropsValue = getCompleteProps(key, slotProps)[key];

if (Array.isArray(value)) {
if (!value.includes(completeProps[key])) {
if (!value.includes(completePropsValue)) {
isValid = false;
break;
}
} else {
if (completeProps[key] !== value) {
const isBlankOrFalse = (v) => v == null || v === false;

if (isBlankOrFalse(value) && isBlankOrFalse(completePropsValue)) continue;

if (completePropsValue !== value) {
isValid = false;
break;
}
Expand Down