# All files / button button.tsx

100% Statements 20/20
100% Branches 12/12
100% Functions 6/6
100% Lines 20/20

    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
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    781x

    1x
    1x

    1x

    1x
    1x
    1x

    1x

    8x

    8x

    8x

    8x
    2x

    8x

    8x
    2x

    10x

    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 { ButtonHandledProps, ButtonUnhandledProps } from "./button.props";
    import { ButtonClassNameContract, ManagedClasses } from "@microsoft/fast-components-class-name-contracts-base";

    /**
     * Button HTML tags
     */
    export enum ButtonHTMLTags {
        a = "a",
        button = "button"
    }

    class Button extends Foundation<
        ButtonHandledProps,
        ButtonUnhandledProps,
        {}
    > {
        public static displayName: string = "Button";

        protected handledProps: HandledProps<ButtonHandledProps> = {
            disabled: void 0,
            href: void 0,
            managedClasses: void 0
        };

        /**
         * Renders the component
         */
        public render(): React.ReactElement<HTMLButtonElement | HTMLAnchorElement> {
            return (
                <this.tag
                    {...this.unhandledProps()}
                    className={this.generateClassNames()}
                    href={this.props.href || null}
                    {...this.renderDisabledAttribute()}
                >
                    {this.props.children}
                </this.tag>
            );
        }

        /**
         * Generates class names
         */
        protected generateClassNames(): string {
            let classNames: string = get(this.props, "managedClasses.button");

            if (this.props.disabled) {
                classNames = `${classNames} ${get(this.props, "managedClasses.button__disabled")}`;
            }

            return super.generateClassNames(classNames);
        }

        /**
         * Stores HTML tag for use in render
         */
        private renderDisabledAttribute(): object {
            if (this.props.disabled === true) {
                return this.tag === ButtonHTMLTags.a ? {"aria-disabled": true} : {"disabled": true};
            }
        }

        /**
         * Stores HTML tag for use in render
         */
        private get tag(): string {
            return typeof this.props.href === "string" ? ButtonHTMLTags.a : ButtonHTMLTags.button;
        }
    }

    export default Button;
    export * from "./button.props";
    export { ButtonClassNameContract };