All files / components/Icon index.ts

88.88% Statements 16/18
100% Branches 7/7
75% Functions 3/4
88.88% Lines 16/18

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 587x   7x 7x 7x                                 7x 29x     7x 37x     7x   66x 66x   66x 29x           37x 37x                       7x      
import Icon from "./Icon"
import { IconType } from "react-icons"
import fa6Icons from "./fa6Icons"
import ionIcons from "./ionIcons"
import { prepareComponent } from "@bluframe/grapple"
 
export type IconGroupName = "fa6" | "io5"
 
export interface Props {
  ariaHidden?: boolean
  color?: string
  group?: IconGroupName
  name: keyof typeof fa6Icons | keyof typeof ionIcons
  size?: string
  title?: string
}
 
export interface ComponentProps extends Omit<Props, "group" | "name"> {
  SelectedIcon: IconType | (() => null)
}
 
const isIonIcon = (name: Props["name"]): name is keyof typeof ionIcons => {
  return name in ionIcons
}
 
const isFaIcon = (name: Props["name"]): name is keyof typeof fa6Icons => {
  return name in fa6Icons
}
 
const usePrepareComponent = ({
  group = "io5",
  name,
  ...props
}: Props): ComponentProps => {
  if (group === "io5" && isIonIcon(name)) {
    return {
      ...props,
      SelectedIcon: ionIcons[name]
    }
  }
 
  if (group === "fa6" && isFaIcon(name)) {
    return {
      ...props,
      SelectedIcon: fa6Icons[name]
    }
  }
 
  return {
    ...props,
    SelectedIcon: () => null
  }
}
 
export default prepareComponent<Props, ComponentProps>(usePrepareComponent)(
  Icon
)