clava 0.4.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/README.md +26 -15
- package/dist/index.d.ts +10 -3
- package/dist/index.js +190 -115
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +376 -214
- package/src/refine-warning.ts +2 -2
- package/src/types.ts +24 -2
- package/tests/component-api.test.ts +81 -55
- package/tests/extend.test.ts +44 -10
- package/tests/prototype-pollution.test.ts +3 -4
- package/tests/refine.test.ts +152 -210
- package/tests/variants-inference.test.ts +81 -0
package/tests/refine.test.ts
CHANGED
|
@@ -71,7 +71,7 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
71
71
|
});
|
|
72
72
|
});
|
|
73
73
|
|
|
74
|
-
test("refine re-runs when
|
|
74
|
+
test("refine re-runs when computed defaultVariants change variants", () => {
|
|
75
75
|
const component = getModeComponent(
|
|
76
76
|
mode,
|
|
77
77
|
cv({
|
|
@@ -79,11 +79,12 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
79
79
|
size: { sm: "sm", lg: "lg" },
|
|
80
80
|
color: { red: "red", blue: "blue" },
|
|
81
81
|
},
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
82
|
+
defaultVariants: {
|
|
83
|
+
color: () => "red" as const,
|
|
84
|
+
size: ({ defaultValue, variants }) =>
|
|
85
|
+
variants.color === "red" ? "lg" : defaultValue,
|
|
86
|
+
},
|
|
87
|
+
refine: ({ variants, addClass }) => {
|
|
87
88
|
if (variants.size === "lg") {
|
|
88
89
|
addClass("refine-lg");
|
|
89
90
|
}
|
|
@@ -96,7 +97,7 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
96
97
|
});
|
|
97
98
|
});
|
|
98
99
|
|
|
99
|
-
test("refine converges with NaN
|
|
100
|
+
test("refine converges with NaN computed defaultVariants", () => {
|
|
100
101
|
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
101
102
|
const component = getModeComponent(
|
|
102
103
|
mode,
|
|
@@ -104,8 +105,10 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
104
105
|
variants: {
|
|
105
106
|
value: (value: number) => (Number.isNaN(value) ? "nan" : null),
|
|
106
107
|
},
|
|
107
|
-
|
|
108
|
-
|
|
108
|
+
defaultVariants: {
|
|
109
|
+
value: () => Number.NaN,
|
|
110
|
+
},
|
|
111
|
+
refine: ({ variants, addClass }) => {
|
|
109
112
|
if (Number.isNaN(variants.value)) {
|
|
110
113
|
addClass("refine-nan");
|
|
111
114
|
}
|
|
@@ -125,7 +128,7 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
125
128
|
}
|
|
126
129
|
});
|
|
127
130
|
|
|
128
|
-
test("
|
|
131
|
+
test("computed defaultVariants can clear inherited defaults", () => {
|
|
129
132
|
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
130
133
|
const base = cv({
|
|
131
134
|
variants: {
|
|
@@ -135,11 +138,9 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
135
138
|
push: (value: number | undefined) =>
|
|
136
139
|
value === undefined ? undefined : `push-${value}`,
|
|
137
140
|
},
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
push: variants.invert ? 20 : undefined,
|
|
142
|
-
});
|
|
141
|
+
defaultVariants: {
|
|
142
|
+
offset: ({ variants }) => !variants.invert,
|
|
143
|
+
push: ({ variants }) => (variants.invert ? 20 : undefined),
|
|
143
144
|
},
|
|
144
145
|
});
|
|
145
146
|
const component = getModeComponent(mode, cv({ extend: [base] }));
|
|
@@ -151,7 +152,6 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
151
152
|
});
|
|
152
153
|
expect(component.getVariants()).toEqual({
|
|
153
154
|
offset: true,
|
|
154
|
-
push: undefined,
|
|
155
155
|
});
|
|
156
156
|
expect(warn).not.toHaveBeenCalled();
|
|
157
157
|
} finally {
|
|
@@ -159,7 +159,7 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
159
159
|
}
|
|
160
160
|
});
|
|
161
161
|
|
|
162
|
-
test("
|
|
162
|
+
test("computed defaultVariants use inherited defaults as defaultValue", () => {
|
|
163
163
|
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
164
164
|
const calls = {
|
|
165
165
|
layer: 0,
|
|
@@ -177,13 +177,11 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
177
177
|
},
|
|
178
178
|
defaultVariants: {
|
|
179
179
|
layer: true,
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
push: variants.invert ? 20 : undefined,
|
|
186
|
-
});
|
|
180
|
+
offset: ({ defaultValue, variants }) => {
|
|
181
|
+
calls.layer += 1;
|
|
182
|
+
return variants.invert ? false : defaultValue;
|
|
183
|
+
},
|
|
184
|
+
push: ({ variants }) => (variants.invert ? 20 : undefined),
|
|
187
185
|
},
|
|
188
186
|
});
|
|
189
187
|
const frame = cv({
|
|
@@ -218,33 +216,22 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
218
216
|
expect(getStyleClass(props)).toEqual({
|
|
219
217
|
class: cls("layer offset frame control"),
|
|
220
218
|
});
|
|
221
|
-
expect(calls).toEqual({
|
|
222
|
-
layer: 2,
|
|
223
|
-
frame: 2,
|
|
224
|
-
control: 2,
|
|
225
|
-
});
|
|
226
|
-
calls.layer = 0;
|
|
227
|
-
calls.frame = 0;
|
|
228
|
-
calls.control = 0;
|
|
229
219
|
expect(component.getVariants()).toEqual({
|
|
230
220
|
layer: true,
|
|
231
221
|
offset: true,
|
|
232
|
-
push: undefined,
|
|
233
222
|
frame: true,
|
|
234
223
|
control: true,
|
|
235
224
|
});
|
|
236
|
-
expect(calls).
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
control: 2,
|
|
240
|
-
});
|
|
225
|
+
expect(calls.layer).toBeGreaterThan(0);
|
|
226
|
+
expect(calls.frame).toBeGreaterThan(0);
|
|
227
|
+
expect(calls.control).toBeGreaterThan(0);
|
|
241
228
|
expect(warn).not.toHaveBeenCalled();
|
|
242
229
|
} finally {
|
|
243
230
|
warn.mockRestore();
|
|
244
231
|
}
|
|
245
232
|
});
|
|
246
233
|
|
|
247
|
-
test("
|
|
234
|
+
test("computed defaultVariants", () => {
|
|
248
235
|
const component = getModeComponent(
|
|
249
236
|
mode,
|
|
250
237
|
cv({
|
|
@@ -252,10 +239,9 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
252
239
|
size: { sm: "sm", lg: "lg" },
|
|
253
240
|
color: { red: "red", blue: "blue" },
|
|
254
241
|
},
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
}
|
|
242
|
+
defaultVariants: {
|
|
243
|
+
color: ({ variants, defaultValue }) =>
|
|
244
|
+
variants.size === "lg" ? "red" : defaultValue,
|
|
259
245
|
},
|
|
260
246
|
}),
|
|
261
247
|
);
|
|
@@ -263,7 +249,7 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
263
249
|
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
264
250
|
});
|
|
265
251
|
|
|
266
|
-
test("
|
|
252
|
+
test("computed defaultVariants do not override props", () => {
|
|
267
253
|
const component = getModeComponent(
|
|
268
254
|
mode,
|
|
269
255
|
cv({
|
|
@@ -271,8 +257,8 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
271
257
|
size: { sm: "sm", lg: "lg" },
|
|
272
258
|
color: { red: "red", blue: "blue" },
|
|
273
259
|
},
|
|
274
|
-
|
|
275
|
-
|
|
260
|
+
defaultVariants: {
|
|
261
|
+
color: () => "red" as const,
|
|
276
262
|
},
|
|
277
263
|
}),
|
|
278
264
|
);
|
|
@@ -280,25 +266,7 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
280
266
|
expect(getStyleClass(props)).toEqual({ class: cls("lg blue") });
|
|
281
267
|
});
|
|
282
268
|
|
|
283
|
-
test("
|
|
284
|
-
const component = getModeComponent(
|
|
285
|
-
mode,
|
|
286
|
-
cv({
|
|
287
|
-
variants: {
|
|
288
|
-
size: { sm: "sm", lg: "lg" },
|
|
289
|
-
color: { red: "red", blue: "blue" },
|
|
290
|
-
},
|
|
291
|
-
defaultVariants: { size: "sm", color: "red" },
|
|
292
|
-
refine: ({ setDefaultVariants }) => {
|
|
293
|
-
setDefaultVariants({ color: "blue" });
|
|
294
|
-
},
|
|
295
|
-
}),
|
|
296
|
-
);
|
|
297
|
-
const props = component();
|
|
298
|
-
expect(getStyleClass(props)).toEqual({ class: cls("sm blue") });
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
test("refine setDefaultVariants overrides extended defaultVariants", () => {
|
|
269
|
+
test("computed defaultVariants override extended defaultVariants", () => {
|
|
302
270
|
const base = cv({
|
|
303
271
|
variants: { color: { red: "red", blue: "blue" } },
|
|
304
272
|
defaultVariants: { color: "red" },
|
|
@@ -308,9 +276,9 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
308
276
|
cv({
|
|
309
277
|
extend: [base],
|
|
310
278
|
variants: { size: { sm: "sm", lg: "lg" } },
|
|
311
|
-
defaultVariants: {
|
|
312
|
-
|
|
313
|
-
|
|
279
|
+
defaultVariants: {
|
|
280
|
+
size: "sm",
|
|
281
|
+
color: () => "blue" as const,
|
|
314
282
|
},
|
|
315
283
|
}),
|
|
316
284
|
);
|
|
@@ -318,31 +286,11 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
318
286
|
expect(getStyleClass(props)).toEqual({ class: cls("blue sm") });
|
|
319
287
|
});
|
|
320
288
|
|
|
321
|
-
test("
|
|
289
|
+
test("parent computed defaultVariants can override child defaultVariants", () => {
|
|
322
290
|
const base = cv({
|
|
323
291
|
variants: { size: { sm: "sm", lg: "lg" } },
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
mode,
|
|
327
|
-
cv({
|
|
328
|
-
extend: [base],
|
|
329
|
-
variants: { color: { red: "red", blue: "blue" } },
|
|
330
|
-
defaultVariants: { size: "sm", color: "red" },
|
|
331
|
-
refine: ({ setDefaultVariants }) => {
|
|
332
|
-
setDefaultVariants({ size: "lg" });
|
|
333
|
-
},
|
|
334
|
-
}),
|
|
335
|
-
);
|
|
336
|
-
const props = component();
|
|
337
|
-
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
test("refine setDefaultVariants from parent overrides child defaultVariants", () => {
|
|
341
|
-
const base = cv({
|
|
342
|
-
variants: { size: { sm: "sm", lg: "lg" } },
|
|
343
|
-
defaultVariants: { size: "sm" },
|
|
344
|
-
refine: ({ setDefaultVariants }) => {
|
|
345
|
-
setDefaultVariants({ size: "lg" });
|
|
292
|
+
defaultVariants: {
|
|
293
|
+
size: () => "lg" as const,
|
|
346
294
|
},
|
|
347
295
|
});
|
|
348
296
|
const component = getModeComponent(
|
|
@@ -357,13 +305,12 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
357
305
|
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
358
306
|
});
|
|
359
307
|
|
|
360
|
-
test("
|
|
308
|
+
test("parent computed defaultVariants can depend on child defaults", () => {
|
|
361
309
|
const base = cv({
|
|
362
|
-
variants: { size: { sm: "sm", lg: "lg" },
|
|
363
|
-
defaultVariants: {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
setDefaultVariants({ size: "lg" });
|
|
310
|
+
variants: { size: { sm: "sm", lg: "lg" }, large: "" },
|
|
311
|
+
defaultVariants: {
|
|
312
|
+
size: ({ defaultValue, variants }) =>
|
|
313
|
+
variants.large ? "lg" : defaultValue,
|
|
367
314
|
},
|
|
368
315
|
});
|
|
369
316
|
const component = getModeComponent(
|
|
@@ -371,97 +318,68 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
371
318
|
cv({
|
|
372
319
|
extend: [base],
|
|
373
320
|
variants: { color: { red: "red", blue: "blue" } },
|
|
374
|
-
defaultVariants: { size: "sm", color: "red" },
|
|
321
|
+
defaultVariants: { size: "sm", color: "red", large: true },
|
|
375
322
|
}),
|
|
376
323
|
);
|
|
377
|
-
const props = component(
|
|
324
|
+
const props = component();
|
|
378
325
|
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
379
326
|
});
|
|
380
327
|
|
|
381
|
-
test("
|
|
382
|
-
const
|
|
383
|
-
variants: { size: { sm: "sm", lg: "lg" }
|
|
384
|
-
defaultVariants: {
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
setDefaultVariants({ size: "lg" });
|
|
388
|
-
}
|
|
328
|
+
test("computed defaultVariants preserve intermediate defaults", () => {
|
|
329
|
+
const parent = cv({
|
|
330
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
331
|
+
defaultVariants: {
|
|
332
|
+
size: ({ defaultValue, variants }) =>
|
|
333
|
+
variants.size ? defaultValue : "lg",
|
|
389
334
|
},
|
|
390
335
|
});
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
cv({
|
|
394
|
-
extend: [base],
|
|
395
|
-
variants: { color: { red: "red", blue: "blue" } },
|
|
396
|
-
defaultVariants: { size: "sm", color: "red", large: true },
|
|
397
|
-
}),
|
|
398
|
-
);
|
|
336
|
+
const child = cv({ extend: [parent], defaultVariants: { size: "sm" } });
|
|
337
|
+
const component = getModeComponent(mode, cv({ extend: [child] }));
|
|
399
338
|
const props = component();
|
|
400
|
-
expect(getStyleClass(props)).toEqual({ class: cls("
|
|
339
|
+
expect(getStyleClass(props)).toEqual({ class: cls("sm") });
|
|
401
340
|
});
|
|
402
341
|
|
|
403
|
-
test("
|
|
342
|
+
test("child computed defaultVariants override parent computed defaultVariants", () => {
|
|
404
343
|
const base = cv({
|
|
405
|
-
variants: { size: { sm: "sm", lg: "lg" }
|
|
406
|
-
defaultVariants: {
|
|
407
|
-
|
|
408
|
-
if (variants.large) {
|
|
409
|
-
setDefaultVariants({ size: "lg" });
|
|
410
|
-
}
|
|
344
|
+
variants: { size: { sm: "sm", lg: "lg" } },
|
|
345
|
+
defaultVariants: {
|
|
346
|
+
size: () => "lg" as const,
|
|
411
347
|
},
|
|
412
348
|
});
|
|
413
|
-
const base2 = cv({ extend: [base] });
|
|
414
349
|
const component = getModeComponent(
|
|
415
350
|
mode,
|
|
416
351
|
cv({
|
|
417
|
-
extend: [
|
|
352
|
+
extend: [base],
|
|
418
353
|
variants: { color: { red: "red", blue: "blue" } },
|
|
419
|
-
defaultVariants: {
|
|
354
|
+
defaultVariants: {
|
|
355
|
+
size: () => "sm" as const,
|
|
356
|
+
color: "red",
|
|
357
|
+
},
|
|
420
358
|
}),
|
|
421
359
|
);
|
|
422
360
|
const props = component();
|
|
423
|
-
expect(getStyleClass(props)).toEqual({ class: cls("
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
test("refine receives default variants from intermediate component", () => {
|
|
427
|
-
const parent = cv({
|
|
428
|
-
variants: { size: { sm: "sm", lg: "lg" } },
|
|
429
|
-
refine: ({ variants, setDefaultVariants }) => {
|
|
430
|
-
if (!variants.size) {
|
|
431
|
-
setDefaultVariants({ size: "lg" });
|
|
432
|
-
}
|
|
433
|
-
},
|
|
434
|
-
});
|
|
435
|
-
const child = cv({ extend: [parent], defaultVariants: { size: "sm" } });
|
|
436
|
-
const component = getModeComponent(mode, cv({ extend: [child] }));
|
|
437
|
-
const props = component();
|
|
438
|
-
expect(getStyleClass(props)).toEqual({ class: cls("sm") });
|
|
361
|
+
expect(getStyleClass(props)).toEqual({ class: cls("sm red") });
|
|
439
362
|
});
|
|
440
363
|
|
|
441
|
-
test("child
|
|
364
|
+
test("child computed defaultVariants can preserve parent computed defaultVariants", () => {
|
|
442
365
|
const base = cv({
|
|
443
366
|
variants: { size: { sm: "sm", lg: "lg" } },
|
|
444
|
-
defaultVariants: {
|
|
445
|
-
|
|
446
|
-
setDefaultVariants({ size: "lg" });
|
|
367
|
+
defaultVariants: {
|
|
368
|
+
size: () => "lg" as const,
|
|
447
369
|
},
|
|
448
370
|
});
|
|
449
371
|
const component = getModeComponent(
|
|
450
372
|
mode,
|
|
451
373
|
cv({
|
|
452
374
|
extend: [base],
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
refine: ({ setDefaultVariants }) => {
|
|
456
|
-
setDefaultVariants({ size: "sm" });
|
|
375
|
+
defaultVariants: {
|
|
376
|
+
size: ({ defaultValue }) => defaultValue,
|
|
457
377
|
},
|
|
458
378
|
}),
|
|
459
379
|
);
|
|
460
380
|
const props = component();
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
// -> child refine.setDefaultVariants (sm)
|
|
464
|
-
expect(getStyleClass(props)).toEqual({ class: cls("sm red") });
|
|
381
|
+
expect(getStyleClass(props)).toEqual({ class: cls("lg") });
|
|
382
|
+
expect(component.getVariants()).toEqual({ size: "lg" });
|
|
465
383
|
});
|
|
466
384
|
|
|
467
385
|
test("refine in extended component does not see foreign variant keys", () => {
|
|
@@ -484,6 +402,7 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
484
402
|
cv({
|
|
485
403
|
extend: [base],
|
|
486
404
|
variants: { color: { red: "red", blue: "blue" } },
|
|
405
|
+
defaultVariants: { size: "sm" },
|
|
487
406
|
}),
|
|
488
407
|
);
|
|
489
408
|
const props = component({ color: "red" });
|
|
@@ -492,18 +411,14 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
492
411
|
});
|
|
493
412
|
});
|
|
494
413
|
|
|
495
|
-
test("
|
|
496
|
-
// Same shape as the test above, but the base's `refine` reaches
|
|
497
|
-
// `setDefaultVariants` — covers the resolveDefaults pass (driving
|
|
498
|
-
// both class output and `getVariants`) rather than the render-time
|
|
499
|
-
// compute path.
|
|
414
|
+
test("computed defaultVariants in extended component do not branch on foreign variant keys", () => {
|
|
500
415
|
const base = cv({
|
|
501
416
|
variants: { size: { sm: "sm", lg: "lg" } },
|
|
502
|
-
defaultVariants: {
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
417
|
+
defaultVariants: {
|
|
418
|
+
size: ({ defaultValue, variants }) =>
|
|
419
|
+
"color" in (variants as Record<string, unknown>)
|
|
420
|
+
? "lg"
|
|
421
|
+
: defaultValue,
|
|
507
422
|
},
|
|
508
423
|
});
|
|
509
424
|
const component = getModeComponent(
|
|
@@ -511,6 +426,7 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
511
426
|
cv({
|
|
512
427
|
extend: [base],
|
|
513
428
|
variants: { color: { red: "red", blue: "blue" } },
|
|
429
|
+
defaultVariants: { size: "sm" },
|
|
514
430
|
}),
|
|
515
431
|
);
|
|
516
432
|
const props = component({ color: "red" });
|
|
@@ -521,11 +437,11 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
521
437
|
});
|
|
522
438
|
});
|
|
523
439
|
|
|
524
|
-
test("child
|
|
440
|
+
test("child computed defaultVariants receive refined variants from parent", () => {
|
|
525
441
|
const base = cv({
|
|
526
442
|
variants: { size: { sm: "sm", lg: "lg" }, small: "" },
|
|
527
|
-
refine: ({
|
|
528
|
-
|
|
443
|
+
refine: ({ setVariants }) => {
|
|
444
|
+
setVariants({ small: true });
|
|
529
445
|
},
|
|
530
446
|
});
|
|
531
447
|
const component = getModeComponent(
|
|
@@ -533,11 +449,10 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
533
449
|
cv({
|
|
534
450
|
extend: [base],
|
|
535
451
|
variants: { color: { red: "red", blue: "blue" } },
|
|
536
|
-
defaultVariants: {
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
}
|
|
452
|
+
defaultVariants: {
|
|
453
|
+
size: ({ defaultValue, variants }) =>
|
|
454
|
+
variants.small ? "sm" : defaultValue,
|
|
455
|
+
color: "red",
|
|
541
456
|
},
|
|
542
457
|
}),
|
|
543
458
|
);
|
|
@@ -571,21 +486,21 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
571
486
|
expect(getStyleClass(props)).toEqual({ class: cls("lg red") });
|
|
572
487
|
});
|
|
573
488
|
|
|
574
|
-
test("
|
|
489
|
+
test("computed defaultVariants work after a setVariants re-run", () => {
|
|
575
490
|
const base = cv({
|
|
576
491
|
variants: {
|
|
577
492
|
size: { sm: "sm", lg: "lg" },
|
|
578
493
|
active: "",
|
|
579
494
|
mode: { on: "on" },
|
|
580
495
|
},
|
|
581
|
-
defaultVariants: {
|
|
582
|
-
|
|
496
|
+
defaultVariants: {
|
|
497
|
+
size: ({ defaultValue, variants }) =>
|
|
498
|
+
variants.mode === "on" ? "lg" : defaultValue,
|
|
499
|
+
},
|
|
500
|
+
refine: ({ variants, setVariants }) => {
|
|
583
501
|
if (variants.active) {
|
|
584
502
|
setVariants({ mode: "on" });
|
|
585
503
|
}
|
|
586
|
-
if (variants.mode === "on") {
|
|
587
|
-
setDefaultVariants({ size: "lg" });
|
|
588
|
-
}
|
|
589
504
|
},
|
|
590
505
|
});
|
|
591
506
|
const component = getModeComponent(mode, cv({ extend: [base] }));
|
|
@@ -635,11 +550,11 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
635
550
|
expect(getStyleClass(getProps())).toEqual({ class: cls("red") });
|
|
636
551
|
});
|
|
637
552
|
|
|
638
|
-
test("child setVariants keeps overriding base
|
|
553
|
+
test("child setVariants keeps overriding base computed defaultVariants across re-runs", () => {
|
|
639
554
|
const base = cv({
|
|
640
555
|
variants: { color: { red: "red", blue: "blue" } },
|
|
641
|
-
|
|
642
|
-
|
|
556
|
+
defaultVariants: {
|
|
557
|
+
color: () => "blue" as const,
|
|
643
558
|
},
|
|
644
559
|
});
|
|
645
560
|
const component = getModeComponent(
|
|
@@ -659,11 +574,11 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
659
574
|
expect(getStyleClass(props)).toEqual({ class: cls("red sm") });
|
|
660
575
|
});
|
|
661
576
|
|
|
662
|
-
test("refine setVariants sticks across re-runs", () => {
|
|
577
|
+
test("refine setVariants sticks across computed default re-runs", () => {
|
|
663
578
|
const base = cv({
|
|
664
579
|
variants: { color: { red: "red", blue: "blue" } },
|
|
665
|
-
|
|
666
|
-
|
|
580
|
+
defaultVariants: {
|
|
581
|
+
color: () => "blue" as const,
|
|
667
582
|
},
|
|
668
583
|
});
|
|
669
584
|
const component = getModeComponent(
|
|
@@ -682,20 +597,21 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
682
597
|
expect(getStyleClass(props)).toEqual({ class: cls("red") });
|
|
683
598
|
});
|
|
684
599
|
|
|
685
|
-
test("base
|
|
600
|
+
test("base computed defaultVariants can override child static defaults after a re-run", () => {
|
|
686
601
|
const base = cv({
|
|
687
602
|
variants: {
|
|
688
603
|
size: { sm: "sm", lg: "lg" },
|
|
689
604
|
active: "",
|
|
690
605
|
mode: { on: "on" },
|
|
691
606
|
},
|
|
692
|
-
|
|
607
|
+
defaultVariants: {
|
|
608
|
+
size: ({ defaultValue, variants }) =>
|
|
609
|
+
variants.mode === "on" ? "lg" : defaultValue,
|
|
610
|
+
},
|
|
611
|
+
refine: ({ variants, setVariants }) => {
|
|
693
612
|
if (variants.active) {
|
|
694
613
|
setVariants({ mode: "on" });
|
|
695
614
|
}
|
|
696
|
-
if (variants.mode === "on") {
|
|
697
|
-
setDefaultVariants({ size: "lg" });
|
|
698
|
-
}
|
|
699
615
|
},
|
|
700
616
|
});
|
|
701
617
|
const component = getModeComponent(
|
|
@@ -706,7 +622,33 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
706
622
|
expect(getStyleClass(props)).toEqual({ class: cls("lg on") });
|
|
707
623
|
});
|
|
708
624
|
|
|
709
|
-
test("
|
|
625
|
+
test("parent computed defaultVariants fall back to child defaults after a dependency changes", () => {
|
|
626
|
+
const layer = cv({
|
|
627
|
+
variants: {
|
|
628
|
+
a: { one: "one", two: "two" },
|
|
629
|
+
b: { true: "b-true", false: "b-false" },
|
|
630
|
+
},
|
|
631
|
+
defaultVariants: {
|
|
632
|
+
b: true,
|
|
633
|
+
a: ({ defaultValue, variants }) =>
|
|
634
|
+
variants.b ? "one" : defaultValue,
|
|
635
|
+
},
|
|
636
|
+
refine: ({ variants, setVariants }) => {
|
|
637
|
+
if (variants.b) {
|
|
638
|
+
setVariants({ b: false });
|
|
639
|
+
}
|
|
640
|
+
},
|
|
641
|
+
});
|
|
642
|
+
const component = getModeComponent(
|
|
643
|
+
mode,
|
|
644
|
+
cv({ extend: [layer], defaultVariants: { a: "two" } }),
|
|
645
|
+
);
|
|
646
|
+
const props = component();
|
|
647
|
+
expect(getStyleClass(props)).toEqual({ class: cls("two b-false") });
|
|
648
|
+
expect(component.getVariants()).toEqual({ a: "two", b: false });
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
test("setVariants from earlier extends overrides computed defaultVariants from later extends", () => {
|
|
710
652
|
const first = cv({
|
|
711
653
|
variants: { color: { red: "first-red", blue: "first-blue" } },
|
|
712
654
|
refine: ({ setVariants }) => {
|
|
@@ -715,8 +657,8 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
715
657
|
});
|
|
716
658
|
const second = cv({
|
|
717
659
|
variants: { color: { red: "second-red", blue: "second-blue" } },
|
|
718
|
-
|
|
719
|
-
|
|
660
|
+
defaultVariants: {
|
|
661
|
+
color: () => "blue" as const,
|
|
720
662
|
},
|
|
721
663
|
});
|
|
722
664
|
const component = getModeComponent(mode, cv({ extend: [first, second] }));
|
|
@@ -726,17 +668,17 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
726
668
|
});
|
|
727
669
|
});
|
|
728
670
|
|
|
729
|
-
test("
|
|
671
|
+
test("computed defaultVariants from later extends apply to final output", () => {
|
|
730
672
|
const first = cv({
|
|
731
673
|
variants: { color: { red: "first-red", blue: "first-blue" } },
|
|
732
|
-
|
|
733
|
-
|
|
674
|
+
defaultVariants: {
|
|
675
|
+
color: () => "red" as const,
|
|
734
676
|
},
|
|
735
677
|
});
|
|
736
678
|
const second = cv({
|
|
737
679
|
variants: { color: { red: "second-red", blue: "second-blue" } },
|
|
738
|
-
|
|
739
|
-
|
|
680
|
+
defaultVariants: {
|
|
681
|
+
color: () => "blue" as const,
|
|
740
682
|
},
|
|
741
683
|
});
|
|
742
684
|
const component = getModeComponent(mode, cv({ extend: [first, second] }));
|
|
@@ -746,7 +688,7 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
746
688
|
});
|
|
747
689
|
});
|
|
748
690
|
|
|
749
|
-
test("
|
|
691
|
+
test("computed defaultVariants do not override stable setVariants on later passes", () => {
|
|
750
692
|
const base = cv({
|
|
751
693
|
variants: { color: { red: "base-red", blue: "base-blue" } },
|
|
752
694
|
refine: ({ setVariants }) => {
|
|
@@ -758,10 +700,9 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
758
700
|
cv({
|
|
759
701
|
extend: [base],
|
|
760
702
|
variants: { color: { red: "child-red", blue: "child-blue" } },
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
}
|
|
703
|
+
defaultVariants: {
|
|
704
|
+
color: ({ defaultValue, variants }) =>
|
|
705
|
+
variants.color === "red" ? "blue" : defaultValue,
|
|
765
706
|
},
|
|
766
707
|
}),
|
|
767
708
|
);
|
|
@@ -771,7 +712,7 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
771
712
|
});
|
|
772
713
|
});
|
|
773
714
|
|
|
774
|
-
test("
|
|
715
|
+
test("computed defaultVariants do not override setVariants from a previous pass", () => {
|
|
775
716
|
const component = getModeComponent(
|
|
776
717
|
mode,
|
|
777
718
|
cv({
|
|
@@ -779,13 +720,14 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
779
720
|
color: { red: "red", blue: "blue" },
|
|
780
721
|
done: "",
|
|
781
722
|
},
|
|
782
|
-
|
|
723
|
+
defaultVariants: {
|
|
724
|
+
color: ({ defaultValue, variants }) =>
|
|
725
|
+
variants.done ? "blue" : defaultValue,
|
|
726
|
+
},
|
|
727
|
+
refine: ({ variants, setVariants }) => {
|
|
783
728
|
if (!variants.done) {
|
|
784
729
|
setVariants({ color: "red", done: true });
|
|
785
730
|
}
|
|
786
|
-
if (variants.done) {
|
|
787
|
-
setDefaultVariants({ color: "blue" });
|
|
788
|
-
}
|
|
789
731
|
},
|
|
790
732
|
}),
|
|
791
733
|
);
|
|
@@ -1091,7 +1033,7 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
1091
1033
|
}
|
|
1092
1034
|
});
|
|
1093
1035
|
|
|
1094
|
-
test("
|
|
1036
|
+
test("computed defaultVariants run when explicitly passing undefined", () => {
|
|
1095
1037
|
const component = getModeComponent(
|
|
1096
1038
|
mode,
|
|
1097
1039
|
cv({
|
|
@@ -1099,8 +1041,8 @@ for (const config of Object.values(CONFIGS)) {
|
|
|
1099
1041
|
size: { sm: "sm", lg: "lg" },
|
|
1100
1042
|
color: { red: "red", blue: "blue" },
|
|
1101
1043
|
},
|
|
1102
|
-
|
|
1103
|
-
|
|
1044
|
+
defaultVariants: {
|
|
1045
|
+
color: () => "red" as const,
|
|
1104
1046
|
},
|
|
1105
1047
|
}),
|
|
1106
1048
|
);
|