Skip to content

Commit bfb5d70

Browse files
authored
fix: treat undefined value for compoundVariants as false (#210)
* fix: treat undefined value for compoundVariants as false * refactor: refactor variable name
1 parent 02eb717 commit bfb5d70

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/__tests__/tv.test.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,33 @@ describe("Tailwind Variants (TV) - Default", () => {
151151
color: "red",
152152
class: "bg-red-500",
153153
},
154+
{
155+
isBig: false,
156+
color: "red",
157+
class: "underline",
158+
},
154159
],
155160
});
156161

157-
const result = h1({
158-
isBig: true,
159-
color: "red",
160-
});
162+
expect(
163+
h1({
164+
isBig: true,
165+
color: "red",
166+
}),
167+
).toHaveClass(["text-5xl", "font-bold", "text-red-500", "bg-red-500"]);
161168

162-
const expectedResult = ["text-5xl", "font-bold", "text-red-500", "bg-red-500"];
169+
expect(
170+
h1({
171+
isBig: false,
172+
color: "red",
173+
}),
174+
).toHaveClass(["text-2xl", "font-bold", "text-red-500", "underline"]);
163175

164-
expect(result).toHaveClass(expectedResult);
176+
expect(
177+
h1({
178+
color: "red",
179+
}),
180+
).toHaveClass(["text-2xl", "font-bold", "text-red-500", "underline"]);
165181
});
166182

167183
test("should throw error if the compoundVariants is not an array", () => {

src/index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,19 @@ export const tv = (options, configProp) => {
307307
let isValid = true;
308308

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

312312
if (Array.isArray(value)) {
313-
if (!value.includes(completeProps[key])) {
313+
if (!value.includes(completePropsValue)) {
314314
isValid = false;
315315
break;
316316
}
317317
} else {
318-
if (completeProps[key] !== value) {
318+
const isBlankOrFalse = (v) => v == null || v === false;
319+
320+
if (isBlankOrFalse(value) && isBlankOrFalse(completePropsValue)) continue;
321+
322+
if (completePropsValue !== value) {
319323
isValid = false;
320324
break;
321325
}

0 commit comments

Comments
 (0)