All files / src/util/classnames useClassNames.ts

100% Statements 11/11
83.33% Branches 5/6
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29              22x 22x 122x 20x   122x 102x       22x       2x 2x 2x     2x    
import { useMemo } from "react";
import { useConfig } from "../../_util/config-context";
import { TeaClassNames } from "../../_classnames";
 
type ClassNamesType = typeof TeaClassNames;
 
function replace<T>(current: T, prefix: string): T {
  Eif (typeof current === "object") {
    Object.entries(current).forEach(([key, value]) => {
      if (typeof value === "object") {
        current[key] = replace(value, prefix);
      }
      if (typeof value === "string") {
        current[key] = value.replace(/^tea-/, `${prefix}-`);
      }
    });
  }
  return current;
}
 
export function useClassNames() {
  const { classPrefix } = useConfig();
  const classNames = useMemo(
    () => replace<ClassNamesType>(TeaClassNames, classPrefix),
    [classPrefix]
  );
  return classNames;
}