# All files / src/rotate rotate.base.tsx

95.83% Statements 23/24
100% Branches 20/20
88.89% Functions 8/9
95.83% Lines 23/24

    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
    133
    134
    135
    136
    137
    138
    139
    1401x
    1x
    1x
    1x

    1x

    6x

    6x

    12x

    6x

    6x

    12x

    12x

    6x

    6x

    6x

    6x

    12x

    6x
    1x

    6x
    1x

    6x

    1x
     import * as React from "react";
    import Foundation, { HandledProps } from "@microsoft/fast-components-foundation-react";
    import { get } from "lodash-es";
    import { Orientation, RotateHandledProps, RotateUnhandledProps } from "./rotate.props";

    class Rotate extends Foundation<RotateHandledProps, RotateUnhandledProps, {}> {
        public static displayName: string = "Rotate";

        protected handledProps: HandledProps<RotateHandledProps> = {
            onUpdateOrientation: void 0,
            landscapeDisabled: void 0,
            portraitDisabled: void 0,
            orientation: void 0,
        };

        public render(): JSX.Element {
            return (
                <div className={this.props.managedClasses.rotate}>
                    <input
                        type="radio"
                        aria-label={this.props.landscapeLabel || "landscape view"}
                        className={this.props.managedClasses.rotate_input}
                        disabled={this.props.landscapeDisabled}
                        onChange={this.handleLandscapeClick}
                        checked={this.props.orientation === Orientation.landscape}
                        tabIndex={
                            this.props.orientation !== Orientation.landscape ? -1 : undefined
                        }
                    />
                    <span
                        className={this.getStateIndicatorClassNames(Orientation.landscape)}
                        onClick={this.handleLandscapeClick}
                    >
                        {this.renderSVG(this.renderLandscapePath())}
                    </span>
                    <input
                        type="radio"
                        aria-label={this.props.portraitLabel || "portrait view"}
                        className={this.props.managedClasses.rotate_input}
                        disabled={this.props.portraitDisabled}
                        onChange={this.handlePortraitClick}
                        checked={this.props.orientation === Orientation.portrait}
                        tabIndex={
                            this.props.orientation !== Orientation.portrait ? -1 : undefined
                        }
                    />
                    <span
                        className={this.getStateIndicatorClassNames(Orientation.portrait)}
                        onClick={this.handlePortraitClick}
                    >
                        {this.renderSVG(this.renderPortraitPath())}
                    </span>
                </div>
            );
        }

        private renderSVG(path: React.ReactNode): React.ReactNode {
            return (
                <svg
                    width="16"
                    height="16"
                    viewBox="0 0 50 50"
                    xmlns="http://www.w3.org/2000/svg"
                >
                    {path}
                </svg>
            );
        }

        private renderLandscapePath(): React.ReactNode {
            return (
                <React.Fragment>
                    <title>{this.props.landscapeLabel || "landscape view"}</title>
                    <g>
                        <path d="M50 43.75V3.125H0V43.75H50ZM46.875 40.625H3.125V6.25H46.875V40.625ZM15.625 34.375H6.25V37.5H15.625V34.375ZM43.75 34.375H34.375V37.5H43.75V34.375Z" />
                    </g>
                </React.Fragment>
            );
        }

        private renderPortraitPath(): React.ReactNode {
            return (
                <React.Fragment>
                    <title>{this.props.portraitLabel || "portrait view"}</title>
                    <g>
                        <path d="M43.75 50V0H3.125V50H43.75ZM40.625 46.875H6.25V3.125H40.625V46.875ZM18.75 40.625H9.375V43.75H18.75V40.625ZM37.5 40.625H28.125V43.75H37.5V40.625Z" />
                    </g>
                </React.Fragment>
            );
        }

        private getStateIndicatorClassNames(orientation: Orientation): string {
            let classes: string =
                get(this.props.managedClasses, "rotate_stateIndicator") || "";

            switch (orientation) {
                case Orientation.landscape:
                    classes = classes.concat(
                        " ",
                        get(this.props.managedClasses, "rotate_stateIndicator__landscape"),
                        this.props.landscapeDisabled
                            ? ` ${get(
                                  this.props.managedClasses,
                                  "rotate_stateIndicator__disabled"
                              )}`
                            : ""
                    );
                    break;
                case Orientation.portrait:
                    classes = classes.concat(
                        " ",
                        get(this.props.managedClasses, "rotate_stateIndicator__portrait"),
                        this.props.portraitDisabled
                            ? ` ${get(
                                  this.props.managedClasses,
                                  "rotate_stateIndicator__disabled"
                              )}`
                            : ""
                    );
                    break;
            }

            return classes;
        }

        private handleOrientationUpdate = (orientation: Orientation): void => {
            this.props.onUpdateOrientation(orientation);
        };

        private handleLandscapeClick = (): void => {
            this.handleOrientationUpdate(Orientation.landscape);
        };

        private handlePortraitClick = (): void => {
            this.handleOrientationUpdate(Orientation.portrait);
        };
    }

    export default Rotate;