import React, { Children, ReactElement, cloneElement, useState, useEffect, } from "react"; import classNames from "classnames"; import { bem } from "../../utilities/bem"; import { Box, BoxProps } from "../Box"; import { Icon, ICON_TYPE } from "../Icon"; const cn = "VendorLogo"; export type VendorLogoSize = 4 | 6 | 10 | 14 | 20 | 40; interface VendorLogoBadgeProps extends BoxProps { icon: ICON_TYPE; size?: VendorLogoSize; } interface VendorLogoProps extends BoxProps { /** * Name of the vendor. If a logo image src is not provided, a tile with the first letter of the `vendorName` will be shown instead */ vendorName: string; /** * Specifies the path to the logo image */ src?: string; /** * Specifies an alternate label for the image */ alt?: string; /** * Sets the size of logo. Uses select tailwind height tokens. Valid values are 4, 6, 10, 14, 20, 40. */ size?: VendorLogoSize; /** * Optional `VendorLogo.Badge` */ children?: ReactElement; } const VendorLogoBadge = ({ icon, size = 10, className, ...rest }: VendorLogoBadgeProps) => { return ( ); }; export const VendorLogo = ({ src, vendorName, alt, className, size = 10, children, }: VendorLogoProps) => { const [loadSuccess, setLoadSuccess] = useState(!!src); const firstLetter = vendorName && vendorName.length > 0 ? vendorName[0] : ""; useEffect(() => { setLoadSuccess(!!src); }, [src]); return ( {loadSuccess ? ( {alt { setLoadSuccess(false); }} /> ) : ( {firstLetter.toUpperCase()} )} {Children.map(children, (child) => { return cloneElement(child as ReactElement, { size, }); })} ); }; VendorLogo.Badge = VendorLogoBadge;