import React from 'react'
import styles from './_image.module.scss'
import Icon from '../Icons/Icon'
import { type IconSizes } from '../Icons/Icon.models'
export type ImageProps = {
/** text displayed if the image fails to load */
alt?: string
/** custom class for image container */
containerClass?: string
/** change the default size of the no-image icon (in px format, ex: '20px') */
iconSize?:
| string
| {
height: string
width: string
}
/** custom class for image
component */
imageClass?: string
/** object with styles to overwrite image container default styles */
style?: React.CSSProperties
/** object with styles to overwrite image default styles */
imgStyle?: React.CSSProperties
/** link to image */
url?: string
/** Optional prop to add a test id to the Image for QA testing */
qaTestId?: string
}
const Image = ({
alt = 'Product',
containerClass = '',
iconSize = '40px',
imageClass = '',
style = {},
imgStyle = {},
url,
qaTestId = 'image',
}: ImageProps): React.JSX.Element => {
const setFallback = (err: React.SyntheticEvent) => {
err.currentTarget.onerror = null
err.currentTarget.src = 'https://images.pattern.com/library/bad-image.svg'
err.currentTarget.alt = 'No Image'
err.currentTarget.style.height =
typeof iconSize === 'object' ? iconSize.height : iconSize
}
return (
{url ? (

) : (
)}
)
}
export default Image