All files / src/material-toolbox/image-list Image.js

100% Statements 15/15
100% Branches 8/8
100% Functions 2/2
100% Lines 15/15

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                        4x               62x 62x 62x   62x 1x   61x 61x     62x     62x 31x 31x 1x     31x             31x        
// @flow strict
import React, { useMemo } from 'react';
import cx from 'classnames';
 
type Props = {
  src: string,
  alt: string,
  asDiv?: boolean,
  withContainer?: boolean,
  className?: string,
};
 
const ImageListImage = ({
  className,
  src,
  alt,
  asDiv = false,
  withContainer = false,
  ...rest
}: Props) => {
  const imageClassName = cx('mdc-image-list__image', className);
  const extraProps = useMemo(() => {
    const n = {};
 
    if (asDiv) {
      n.style = { backgroundImage: `url(${src})` };
    } else {
      n.alt = alt;
      n.src = src;
    }
 
    return n;
  }, [alt, asDiv, src]);
 
  if (withContainer) {
    let Component = 'img';
    if (asDiv) {
      Component = 'div';
    }
 
    return (
      <div className="mdc-image-list__image-aspect-container">
        <Component {...rest} {...extraProps} className={imageClassName} />
      </div>
    );
  }
 
  return <img {...rest} src={src} alt={alt} className={imageClassName} />;
};
 
export default ImageListImage;