refactor: allow arrays and falsy values in cn util

This commit is contained in:
Javi Aguilar
2026-05-21 16:27:51 +02:00
parent c8b0bb2225
commit 11b4d34b79
2 changed files with 16 additions and 2 deletions
+8
View File
@@ -552,4 +552,12 @@ describe("cn", () => {
test("handles all undefined", () => {
expect(cn(undefined, undefined)).toBe("");
});
test("handles nested arrays of classes", () => {
expect(cn(["foo", ["foo", ["foo", "bar"]]])).toBe("foo foo foo bar");
});
test("handles nulls, booleans and undefined values", () => {
expect(cn(null, true, false, undefined, [null, true, false, undefined])).toBe("");
});
});
+8 -2
View File
@@ -12,8 +12,14 @@ import { type TSurveyElement, type TSurveyElementChoice } from "@formbricks/type
import { type TShuffleOption } from "@formbricks/types/surveys/types";
import { ApiResponse, ApiSuccessResponse } from "@/types/api";
export const cn = (...classes: (string | undefined)[]) => {
return twMerge(classes.filter(Boolean).join(" "));
type ClassValue = string | boolean | null | undefined | ClassValue[];
export const cn = (...classes: ClassValue[]): string => {
return twMerge(
classes
.map((c) => (Array.isArray(c) ? cn(...c) : c))
.filter((c): c is string => typeof c === "string" && c.length > 0)
.join(" ")
);
};
export const getSecureRandom = (): number => {