import React from "react"; import { ComponentType, FunctionComponent } from "react"; import { IntersectionFn, KeepNeversFn, ReplaceFn, ToSchema } from "../types/Fn"; import { Hoc } from "../types/Hoc"; import { newHoc } from "../utils/newHoc"; type ClassNameArg = | string | ((props: DependencyProps) => string | string[]) | (string | ((props: DependencyProps) => string | string[]))[]; interface WithClassNameHoc { ( className?: ClassNameArg, ): Hoc< [ IntersectionFn>, KeepNeversFn>, ] >; } export const withClassName = newHoc(function withClassName( Component: ComponentType, classNameArg: any = [], ): FunctionComponent { return function WithClassName(props: any): JSX.Element { if (typeof classNameArg === "function") { classNameArg = classNameArg(props); } if (Array.isArray(classNameArg)) { classNameArg = classNameArg .map((c) => (typeof c === "function" ? c(props) : c)) .flat(); } const className = new Set(); for (const prop of [classNameArg, props.className].filter( (c) => c?.length, )) { if (typeof prop === "string" && prop) { className.add(prop); } else if (Array.isArray(prop)) { for (const p of prop) { if (p) { className.add(p); } } } } return ; }; });