# All files / dialog dialog.tsx

93.75% Statements 30/32
83.87% Branches 26/31
100% Functions 9/9
93.75% Lines 30/32

    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
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    1331x

    1x
    1x

    1x
    1x
    1x

    1x

    1x

    8x

    10x

    8x
    3x

    2x
    2x

    2x

    1x
    1x

    10x

    10x
    3x

    7x

    8x
    2x
    1x

    8x
    3x
    2x

    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 { DialogHandledProps, DialogManagedClasses, DialogProps, DialogUnhandledProps } from "./dialog.props";
    import { DialogClassNameContract, ManagedClasses } from "@microsoft/fast-components-class-name-contracts-base";
    import { canUseDOM } from "exenv-es6";
    import { KeyCodes } from "@microsoft/fast-web-utilities";

    class Dialog extends Foundation<
        DialogHandledProps,
        DialogUnhandledProps,
        {}
    > {
        public static defaultProps: Partial<DialogProps> = {
            contentHeight: "480px",
            contentWidth: "640px",
            visible: false
        };

        public static displayName: string = "Dialog";

        protected handledProps: HandledProps<DialogHandledProps> = {
            describedBy: void 0,
            label: void 0,
            labelledBy: void 0,
            contentWidth: void 0,
            contentHeight: void 0,
            modal: void 0,
            managedClasses: void 0,
            onDismiss: void 0,
            visible: void 0
        };

        /**
         * Renders the component
         */
        public render(): React.ReactElement<HTMLDivElement> {
            return (
                <div
                    {...this.unhandledProps()}
                    className={this.generateClassNames()}
                    aria-hidden={!this.props.visible}
                >
                    {this.renderModalOverlay()}
                    <div
                        role="dialog"
                        tabIndex={-1}
                        className={get(this.props, "managedClasses.dialog_contentRegion")}
                        style={{height: this.props.contentHeight, width: this.props.contentWidth}}
                        aria-describedby={this.props.describedBy}
                        aria-labelledby={this.props.labelledBy}
                        aria-label={this.props.label}
                    >
                        {this.props.children}
                    </div>
                </div>
            );
        }

        /**
         * React life-cycle method
         */
        public componentDidMount(): void {
            if (canUseDOM() && this.props.onDismiss) {
                window.addEventListener("keydown", this.handleWindowKeyDown);
            }
        }

        /**
         * React life-cycle method
         */
        public componentDidUpdate(prevProps: Partial<DialogHandledProps>): void {
            Eif (canUseDOM()) {
                Iif (!prevProps.onDismiss && this.props.onDismiss) {
                    window.addEventListener("keydown", this.handleWindowKeyDown);
                } else Iif (prevProps.onDismiss && !this.props.onDismiss) {
                    window.removeEventListener("keydown", this.handleWindowKeyDown);
                }
            }
        }

        /**
         * React life-cycle method
         */
        public componentWillUnmount(): void {
            Eif (canUseDOM() && this.props.onDismiss) {
                window.removeEventListener("keydown", this.handleWindowKeyDown);
            }
        }

        /**
         * Generates class names
         */
        protected generateClassNames(): string {
            return super.generateClassNames(get(this.props, "managedClasses.dialog"));
        }

        /**
         * Renders the modal overlay
         */
        private renderModalOverlay(): React.ReactElement<HTMLDivElement> {
            if (!this.props.modal) {
                return;
            }

            return (
                <div
                    className={get(this.props, "managedClasses.dialog_modalOverlay")}
                    onClick={this.handleOverlayClick}
                    role={"presentation"}
                    tabIndex={-1}
                />
            );
        }

        private handleOverlayClick = (event: React.MouseEvent): void => {
            if (this.props.onDismiss && typeof this.props.onDismiss === "function" && this.props.visible) {
                this.props.onDismiss(event);
            }
        }

        private handleWindowKeyDown = (event: KeyboardEvent): void => {
            if (this.props.onDismiss && typeof this.props.onDismiss === "function" && this.props.visible && event.keyCode === KeyCodes.escape) {
                this.props.onDismiss(event);
            }
        }
    }

    export default Dialog;
    export * from "./dialog.props";
    export { DialogClassNameContract };