classNameFactory: Allow (& ignore) all sorts of falsy values (#1427)

This commit is contained in:
Justice Almanzar 2023-07-15 22:50:21 +00:00 committed by GitHub
parent 1340f023a3
commit a7e74ee4d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -141,7 +141,7 @@ export const compileStyle = (style: Style) => {
*/ */
export const classNameToSelector = (name: string, prefix = "") => name.split(" ").map(n => `.${prefix}${n}`).join(""); export const classNameToSelector = (name: string, prefix = "") => name.split(" ").map(n => `.${prefix}${n}`).join("");
type ClassNameFactoryArg = string | string[] | Record<string, unknown>; type ClassNameFactoryArg = string | string[] | Record<string, unknown> | false | null | undefined | 0 | "";
/** /**
* @param prefix The prefix to add to each class, defaults to `""` * @param prefix The prefix to add to each class, defaults to `""`
* @returns A classname generator function * @returns A classname generator function
@ -154,9 +154,9 @@ type ClassNameFactoryArg = string | string[] | Record<string, unknown>;
export const classNameFactory = (prefix: string = "") => (...args: ClassNameFactoryArg[]) => { export const classNameFactory = (prefix: string = "") => (...args: ClassNameFactoryArg[]) => {
const classNames = new Set<string>(); const classNames = new Set<string>();
for (const arg of args) { for (const arg of args) {
if (typeof arg === "string") classNames.add(arg); if (arg && typeof arg === "string") classNames.add(arg);
else if (Array.isArray(arg)) arg.forEach(name => classNames.add(name)); else if (Array.isArray(arg)) arg.forEach(name => classNames.add(name));
else if (typeof arg === "object") Object.entries(arg).forEach(([name, value]) => value && classNames.add(name)); else if (arg && typeof arg === "object") Object.entries(arg).forEach(([name, value]) => value && classNames.add(name));
} }
return Array.from(classNames, name => prefix + name).join(" "); return Array.from(classNames, name => prefix + name).join(" ");
}; };