# All files / image image.tsx

100% Statements 16/16
100% Branches 8/8
100% Functions 3/3
100% Lines 16/16

    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
    58
    59
    60
    61
    62
    63
    64
    65
    66
    671x

    1x
    1x

    1x

    1x
    1x

    1x

    10x

    10x

    10x
    7x

    3x

    3x

    1x
    1x
    1x
     import * as React from "react";
    import * as ReactDOM from "react-dom";
    import { get } from "lodash-es";
    import Foundation, { HandledProps } from "@microsoft/fast-components-foundation-react";
    import { ImageHandledProps, ImageManagedClasses, ImageUnhandledProps } from "./image.props";
    import { ImageClassNameContract, ManagedClasses } from "@microsoft/fast-components-class-name-contracts-base";

    export enum ImageSlot {
        source = "source"
    }

    class Image extends Foundation<
        ImageHandledProps,
        ImageUnhandledProps,
        {}
    > {
        public static displayName: string = "Image";

        protected handledProps: HandledProps<ImageHandledProps> = {
            managedClasses: void 0,
            alt: void(0),
            sizes: void(0),
            src: void(0),
            srcSet: void(0)
        };

        /**
         * Renders the component
         */
        public render(): React.ReactElement<HTMLImageElement | HTMLPictureElement> {
            let className: string = get(this.props, "managedClasses.image");

            if (!this.props.children) {
                return (
                    <img
                        {...this.unhandledProps()}
                        className={super.generateClassNames(className)}
                        alt={this.props.alt}
                        sizes={this.props.sizes ? this.props.sizes : null}
                        src={this.props.src}
                        srcSet={this.props.srcSet ? this.props.srcSet : null}
                    />
                );
            } else {
                className = `${className} ${get(this.props, "managedClasses.image__picture")}`;

                return (
                    <picture
                        {...this.unhandledProps()}
                        className={super.generateClassNames(className)}
                    >
                        {this.withSlot(ImageSlot.source)}
                        <img
                            src={this.props.src}
                            alt={this.props.alt}
                            className={get(this.props, "managedClasses.image_img")}
                        />
                    </picture>
                );
            }
        }
    }

    export default Image;
    export * from "./image.props";
    export { ImageClassNameContract };